Optimizing my 2D Ising model code 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







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







    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







      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







      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







      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



      share






      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 3 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

          Terni

          A new problem with tex4ht and tikz

          Sun Ra