---materialsim-grain-growth.nlogo

来自「NETLOGO」· NLOGO 代码 · 共 1,163 行 · 第 1/3 页

NLOGO
1,163
字号
breeds [element1 element2] ;; two different materials or phases                           ;; element1 is the main material                           ;; element2 is the materials which is                           ;; dispersed inside element1 (second-phase particles)                       element1-own   [    neighbors-6   ;; agentset of 6 neighboring cells    n             ;; used to store a count of second-phase neighbors   ]patches-own   [    border?       ;; this indicates that a patch is located on the view's border   ]turtles-own   [    temp                 ;; atom's temperature    neighboring-turtles  ;; agentset of surrounding atoms    sides-exposed        ;; number of sides exposed to walls  (between 0 and 4)   ]globals   [    time                    ;; keeps track of simulation time    logtime                 ;; log of time    colors                  ;; used both to color turtles, and for histogram    xmax                    ;; max x size    ymax                    ;; max y size    intercepts              ;; used to calculate grain size (1/intercepts = average grain size)    average-grain-size      ;; average grain size    logaverage-grain-size   ;; log of average grain size (for plotting)    orientation-for-incercept-count  ;; for grain size calculation (normally 90 degrees)    MCS                     ;; Number of Monte Carlo Steps (simulation steps)    initial-loggrain-size   ;; For grain growth exponent calculation and graphing    initial-logtime         ;; For grain growth exponent calculation and graphing    grain-growth-exponent   ;; Grain growth exponent    total-atoms            ;; Total number of atoms in the system   ]to fill-outer-area-with-white;; fills outer white box  set ymax (height + 1) / 2  set xmax (width + 1) / 2    ask patches with [(abs pycor >= ymax) or                      (abs pxcor >= xmax)]                    [set pcolor white                     set border? true ]endto setup-hex-grid;; setup the hexagonal grid in which atoms will be placed;; and creates turtles  set-default-shape element2 "square"  ask patches    [ ;; to avoid wrapping around the edge of the screen,      ;; we do not create atoms on the screen's edge      if border? != true        [ sprout 1        [            ifelse (fraction-element2 > random-float 100)            ;; if there is a second element, create the corresponding atoms             [              ;; element2 is the fixed second-phase particle              set breed element2              set color white              set heading 360             ]             [              ;; element1 is the main material, which grains growth              set breed element1               set shape atom-shape              set color random 139              set heading color             ]                     ;; shift even columns down              if pxcor mod 2 = 0                [ set ycor ycor - 0.5 ] ] ] ]             ;; now set up the neighbors6 agentsets; ask element1 [ set shape3d "sphere" ]; ask element2 [ set shape3d "cube" ]; the two above lines are for NetLogo 3D. Uncomment them if you use that version.   ask element1  ;; define neighborhood of atoms    [ ifelse pxcor mod 2 = 0        [ set neighbors-6 element1-on patches at-points [[0 1] [1 0] [1 -1] [0 -1] [-1 -1] [-1 0]] ]        [ set neighbors-6 element1-on patches at-points [[0 1] [1 1] [1  0] [0 -1] [-1  0] [-1 1]] ] ]end;; makes initial box for image importto makes-initial-box  resets-time  fill-outer-area-with-white  setup-hex-grid  end;; makes initial box for random arrangementto makes-initial-box-random  ca  resets-time  fill-outer-area-with-white  setup-hex-grid  endto resets-time;; resets time    set time 1  set MCS 0end;; import image into turtlesto import-imagecalet file user-choose-fileif file = false [ stop ];; imports image into patchesimport-pcolors file;; generates a white border around the image to avoid wrapping;; converts the square grid to an hex gridmakes-initial-box;; transfers the image to the turtles. Rounds the color values to be integers.ask turtles   [   set color round (pcolor-of patch-here)   set heading color  ];; erases the patches (sets their color back to black),;; except the border patches, which will remain whiteask patches [if border? != true               [                set pcolor black               ]            ]endto define-neighboring-turtles;; defines neighboring turtlesask turtles    [     set neighboring-turtles (turtles at-points [[-1  1] [ 0  1] [1  1]                                                 [-1  0] [ 0  0] [1  0]                                                 [-1 -1] [ 0 -1] [1 -1]])                                                    ]endto grain-count;; count number of grains based on the number of linear interceptsset orientation-for-incercept-count 90 ;; direction of intercepts countset intercepts 0set total-atoms count turtles  ;;  ask patches    ask turtles    [      ;; checks if turtle is before the last 'x' column and 'y' row       if  ((xcor != (xmax - 1)) and (who < ((width * height) - 1)) and (who < total-atoms))        [         ;; checks if there is a turtle to the right for the intercept calculation         ifelse any? turtles-on (patch-at-heading-and-distance orientation-for-incercept-count 1)           [            ;; If there is a turtle, checks if the heading is different.            let right-neighbor-heading heading-of                                         (random-one-of                                            turtles-on (patch-at-heading-and-distance                                                          orientation-for-incercept-count                                                          1))            if (heading != (right-neighbor-heading))               [                ;; If heading is different, add 1 to 'intercepts'.                set intercepts (intercepts + 1)               ]           ]           [            ;; if there is no turtle, simply add 1 to 'intercepts'.            ;; A turtle/nothing interface is considered as grain boundary.            set intercepts (intercepts + 1)           ]      ]    ]     ifelse intercepts = 0       [set average-grain-size (total-atoms)] ;; grain size = area of the whole sample (to avoid division by zero)       [set average-grain-size ((total-atoms) / intercepts)] ;; grain size = area / grain-grain interfaceend                           to do-plots  set-current-plot "Grain Size (log-log)"  plot average-grain-sizeendto go;;initiates grain growthset total-atoms count turtlesif average-grain-size >= total-atoms [stop];; stops when there is just one grainrepeat (total-atoms)    [    ;;limits grain growth to element1, element2 represent the stationary second-phase particles    ask random-one-of element1 [grain-growth]   ]set MCS (MCS + 1);; advances Monte Carlo Steps (simulation time);; one Monte Carlo Step represents 'n' reorientation attemps,;; where 'n' is the total number of atoms       if remainder MCS measurement-frequency = 0;; calculates grain size at a given frequency   [    set logtime (log time 10)    grain-count    if average-grain-size != 0      [       set logaverage-grain-size (log (average-grain-size) 10)      ]      ;; grain growth is better plotted on log-log scale    do-plots    if MCS = 20 [                 set initial-logtime logtime                 set initial-loggrain-size logaverage-grain-size                ]    ;; only initiates grain size calculation after MCS = 20    if MCS > 20 [                 ;; calculate the angular coeficient of the grain growth curve                 ;; since it is a log-log plot, it's the grain growth exponent                 set grain-growth-exponent (-1 * ((logaverage-grain-size - initial-loggrain-size) /                                                  (initial-logtime - logtime)))                ]   ]  end;; Grain growth procedure - free energy minimization;; if another random crystalographic heading minimizes energy, switches headings, otherwise keeps the same.to grain-growth  ;; increases time - divided by the area, gives the Monte Carlo Steps  set time (time + 1);; calculates the PRESENT free energy    let present-heading (heading)  let present-free-energy count neighbors-6 with [heading != present-heading] ;; chooses a random orientation  let future-heading (heading-of (random-one-of neighbors-6));; calculates the FUTURE free energy, with the random orientation just chosen  let future-free-energy count neighbors-6 with [heading != future-heading] ;; compares PRESENT and FUTURE free-energies; the lower value "wins"  ifelse future-free-energy <= present-free-energy        [set heading (future-heading)]        [if (annealing-temperature > random-float 100) [set heading (future-heading)]]        ;; this last line simulates thermal agitation (adds more randomness to the simulation)  set color heading  ;;update the color of the atoms   end;; drawing procedureto turtle-draw  if mouse-down?     ;; reports true or false to indicate whether mouse button is down    [     ask turtles-at mouse-xcor mouse-ycor        [ask turtles in-radius brush-size [set color draw-color set heading color]]    ] end;; in the drawing mode, erases the whole "canvas" with redto erase-all  ask turtles [if pcolor != white [set color red set heading color]]end; *** NetLogo Model Copyright Notice ***; ;; Copyright 2005 by Uri Wilensky. All rights reserved.;; Permission to use, modify or redistribute this model is hereby granted,; provided that both of the following requirements are followed:; a) this copyright notice is included.; b) this model will not be redistributed for profit without permission;    from Uri Wilensky.; Contact Uri Wilensky for appropriate licenses for redistribution for; profit.;; To refer to this model in academic publications, please use:; Blikstein, P. and Wilensky, U. (2005).  NetLogo MaterialSim Grain Growth model.; http://ccl.northwestern.edu/netlogo/models/MaterialSimGrainGrowth.; Center for Connected Learning and Computer-Based Modeling,; Northwestern University, Evanston, IL.;; In other publications, please use:; Copyright 2005 by Uri Wilensky.  All rights reserved.; See http://ccl.northwestern.edu/netlogo/models/MaterialSimGrainGrowth; for terms of use.;; We gratefully acknowledge the support of the; National Science Foundation (REPP, ROLE & ITR programs);; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW4761087843324248.01101110111CC-WINDOW5580887675Command Center0SLIDER2343746270widthwidth35014521atomsSLIDER23573462106

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?