Optimizing my 2D Ising model code in Julia The 2019 Stack Overflow Developer Survey Results...

What do you call a plan that's an alternative plan in case your initial plan fails?

How do I add random spotting to the same face in cycles?

What's the point in a preamp?

How does ice melt when immersed in water?

I could not break this equation. Please help me

Do warforged have souls?

Relations between two reciprocal partial derivatives?

Keeping a retro style to sci-fi spaceships?

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

Was credit for the black hole image misattributed?

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

Can a 1st-level character have an ability score above 18?

Can withdrawing asylum be illegal?

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

Can undead you have reanimated wait inside a portable hole?

Would an alien lifeform be able to achieve space travel if lacking in vision?

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

Take groceries in checked luggage

What are these Gizmos at Izaña Atmospheric Research Center in Spain?

What can I do if neighbor is blocking my solar panels intentionally?

Finding the path in a graph from A to B then back to A with a minimum of shared edges

Semisimplicity of the category of coherent sheaves?

Python - Fishing Simulator

Are my PIs rude or am I just being too sensitive?



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

          Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

          Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...