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

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          波兰旗帜列表 目录 国旗 军旗 其他制服部门旗帜 特别国家机构船只 参考文献 外部链接 导航菜单Polskie flagi, chorągwie, bandery... [波兰旗帜、条幅、船旗等]原始内容Ustawa z dnia 31 stycznia 1980 r. o godle, barwach i hymnie Rzeczypospolitej Polskiej oraz o pieczęciach państwowychZarządzenie Ministra Obrony Narodowej z dnia 14 grudnia 2005 r. zmieniające zarządzenie w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejZarządzenie Ministra Obrony Narodowej z dnia 29 stycznia 1996 r. w sprawie szczegółowych zasad używania znaków Sił Zbrojnych Rzeczypospolitej Polskiej oraz ustalenia innych znaków używanych w Siłach Zbrojnych Rzeczypospolitej PolskiejUstawa z dnia 19 lutego 1993 r. o znakach Sił Zbrojnych Rzeczypospolitej PolskiejHistoria Marynarki Wojennej RP [波兰海军史]Rozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 12 kwietnia 2002 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych Straży GranicznejRozporządzenie Ministra Spraw Wewnętrznych i Administracji z dnia 18 kwietnia 2005 r. w sprawie wzoru flagi oraz oznakowania jednostek pływających i statków powietrznych PolicjiRozporządzenie Ministra Infrastruktury z dnia 21 października 2005 r. w sprawie wzorów flag dla statków morskich na oznaczenie pełnionej specjalnej służby państwowej oraz okoliczności i warunków ich podnoszenia波兰旗帜波兰

          Indenting and Dedenting ASP code with Python