Optimizing my 2D Ising model code in Julia The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Neural Network in JuliaJulia RPN calculator algorithm ported from Python versionKernel density regression in JuliaImplementing cat function in JuliaComputing the double Integral using MonteCarlo techniques using JuliaSearch algorithms in juliaSort Algorithms in JuliaParsing simple log files in Julia vs PythonWeighted logistic regression in JuliaWebSocket based API library in Julia

The variadic template constructor of my class cannot modify my class members, why is that so?

Single author papers against my advisor's will?

Problems with Ubuntu mount /tmp

how can a perfect fourth interval be considered either consonant or dissonant?

I could not break this equation. Please help me

Can smartphones with the same camera sensor have different image quality?

Road tyres vs "Street" tyres for charity ride on MTB Tandem

Wall plug outlet change

How to remove this toilet supply line that seems to have no nut?

How to pronounce 1ターン?

If the empty set is a subset of every set, why write ... ∪ ∅?

How to delete random line from file using Unix command?

system() function string length limit

Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?

In horse breeding, what is the female equivalent of putting a horse out "to stud"?

Can the DM override racial traits?

Sort a list of pairs representing an acyclic, partial automorphism

How to copy the contents of all files with a certain name into a new file?

What is special about square numbers here?

Segmentation fault output is suppressed when piping stdin into a function. Why?

Can withdrawing asylum be illegal?

How did passengers keep warm on sail ships?

Why can't devices on different VLANs, but on the same subnet, communicate?

Why can't wing-mounted spoilers be used to steepen approaches?



Optimizing my 2D Ising model code in Julia



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Neural Network in JuliaJulia RPN calculator algorithm ported from Python versionKernel density regression in JuliaImplementing cat function in JuliaComputing the double Integral using MonteCarlo techniques using JuliaSearch algorithms in juliaSort Algorithms in JuliaParsing simple log files in Julia vs PythonWeighted logistic regression in JuliaWebSocket based API library in Julia



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0












$begingroup$


I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.



using Printf
using Plots

L = 20
n_sweep = 20
n_therm = 1000
n_data = 100
temps = 4.0:-0.3:0.1
e1 = Array(1:n_data)
m1 = Array(1:n_data)
et = []
mt = []

energy = e1
magnetization = m1

s = ones(Int32,L,L)


function measure(i)
en = 0
m = 0
for x = 1:L
for y = 1:L
u = 1+mod(y,L)
r = 1+mod(x,L)
en -= s[x,y]*(s[x,u]+s[r,y])
m += s[x,y]
end
end
energy[i] = en
magnetization[i] = abs(m)
end

function sweep(n,T)
for i = 1:n
for x = 1:L
for y = 1:L
flip(x,y,T)
end
end
end
end

function flip(x,y,T)
u = 1+mod(y,L)
d = 1+mod(y-2,L)
r = 1+mod(x,L)
l = 1+mod(x-2,L)
de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
if (de < 0)
s[x,y] = -s[x,y]
else
p = rand()
if (p < exp(-de/T))
s[x,y] = -s[x,y]
end
end
end

for T in temps
sweep(n_therm, T)
energy = e1
magnetization = m1
for i = 1:n_data
sweep(n_sweep, T)
measure(i)
end
en1 = sum(energy)/n_data
ma1 = sum(magnetization)/n_data
push!(et,en1/(L*L))
push!(mt,ma1/(L*L))
@printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
end

plot(temps,mt)









share|improve this question







New contributor




Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$


















    0












    $begingroup$


    I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.



    using Printf
    using Plots

    L = 20
    n_sweep = 20
    n_therm = 1000
    n_data = 100
    temps = 4.0:-0.3:0.1
    e1 = Array(1:n_data)
    m1 = Array(1:n_data)
    et = []
    mt = []

    energy = e1
    magnetization = m1

    s = ones(Int32,L,L)


    function measure(i)
    en = 0
    m = 0
    for x = 1:L
    for y = 1:L
    u = 1+mod(y,L)
    r = 1+mod(x,L)
    en -= s[x,y]*(s[x,u]+s[r,y])
    m += s[x,y]
    end
    end
    energy[i] = en
    magnetization[i] = abs(m)
    end

    function sweep(n,T)
    for i = 1:n
    for x = 1:L
    for y = 1:L
    flip(x,y,T)
    end
    end
    end
    end

    function flip(x,y,T)
    u = 1+mod(y,L)
    d = 1+mod(y-2,L)
    r = 1+mod(x,L)
    l = 1+mod(x-2,L)
    de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
    if (de < 0)
    s[x,y] = -s[x,y]
    else
    p = rand()
    if (p < exp(-de/T))
    s[x,y] = -s[x,y]
    end
    end
    end

    for T in temps
    sweep(n_therm, T)
    energy = e1
    magnetization = m1
    for i = 1:n_data
    sweep(n_sweep, T)
    measure(i)
    end
    en1 = sum(energy)/n_data
    ma1 = sum(magnetization)/n_data
    push!(et,en1/(L*L))
    push!(mt,ma1/(L*L))
    @printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
    end

    plot(temps,mt)









    share|improve this question







    New contributor




    Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $begingroup$


      I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.



      using Printf
      using Plots

      L = 20
      n_sweep = 20
      n_therm = 1000
      n_data = 100
      temps = 4.0:-0.3:0.1
      e1 = Array(1:n_data)
      m1 = Array(1:n_data)
      et = []
      mt = []

      energy = e1
      magnetization = m1

      s = ones(Int32,L,L)


      function measure(i)
      en = 0
      m = 0
      for x = 1:L
      for y = 1:L
      u = 1+mod(y,L)
      r = 1+mod(x,L)
      en -= s[x,y]*(s[x,u]+s[r,y])
      m += s[x,y]
      end
      end
      energy[i] = en
      magnetization[i] = abs(m)
      end

      function sweep(n,T)
      for i = 1:n
      for x = 1:L
      for y = 1:L
      flip(x,y,T)
      end
      end
      end
      end

      function flip(x,y,T)
      u = 1+mod(y,L)
      d = 1+mod(y-2,L)
      r = 1+mod(x,L)
      l = 1+mod(x-2,L)
      de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
      if (de < 0)
      s[x,y] = -s[x,y]
      else
      p = rand()
      if (p < exp(-de/T))
      s[x,y] = -s[x,y]
      end
      end
      end

      for T in temps
      sweep(n_therm, T)
      energy = e1
      magnetization = m1
      for i = 1:n_data
      sweep(n_sweep, T)
      measure(i)
      end
      en1 = sum(energy)/n_data
      ma1 = sum(magnetization)/n_data
      push!(et,en1/(L*L))
      push!(mt,ma1/(L*L))
      @printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
      end

      plot(temps,mt)









      share|improve this question







      New contributor




      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.



      using Printf
      using Plots

      L = 20
      n_sweep = 20
      n_therm = 1000
      n_data = 100
      temps = 4.0:-0.3:0.1
      e1 = Array(1:n_data)
      m1 = Array(1:n_data)
      et = []
      mt = []

      energy = e1
      magnetization = m1

      s = ones(Int32,L,L)


      function measure(i)
      en = 0
      m = 0
      for x = 1:L
      for y = 1:L
      u = 1+mod(y,L)
      r = 1+mod(x,L)
      en -= s[x,y]*(s[x,u]+s[r,y])
      m += s[x,y]
      end
      end
      energy[i] = en
      magnetization[i] = abs(m)
      end

      function sweep(n,T)
      for i = 1:n
      for x = 1:L
      for y = 1:L
      flip(x,y,T)
      end
      end
      end
      end

      function flip(x,y,T)
      u = 1+mod(y,L)
      d = 1+mod(y-2,L)
      r = 1+mod(x,L)
      l = 1+mod(x-2,L)
      de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
      if (de < 0)
      s[x,y] = -s[x,y]
      else
      p = rand()
      if (p < exp(-de/T))
      s[x,y] = -s[x,y]
      end
      end
      end

      for T in temps
      sweep(n_therm, T)
      energy = e1
      magnetization = m1
      for i = 1:n_data
      sweep(n_sweep, T)
      measure(i)
      end
      en1 = sum(energy)/n_data
      ma1 = sum(magnetization)/n_data
      push!(et,en1/(L*L))
      push!(mt,ma1/(L*L))
      @printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
      end

      plot(temps,mt)






      performance beginner julia






      share|improve this question







      New contributor




      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 12 mins ago









      KaiKai

      1012




      1012




      New contributor




      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Kai is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217421%2foptimizing-my-2d-ising-model-code-in-julia%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Kai is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Kai is a new contributor. Be nice, and check out our Code of Conduct.












          Kai is a new contributor. Be nice, and check out our Code of Conduct.











          Kai is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217421%2foptimizing-my-2d-ising-model-code-in-julia%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar