📄 ---gaslab gas in a box.nlogo
字号:
globals [ fast medium slow ;; current counts avg-speed avg-energy ;; current averages avg-speed-init avg-energy-init ;; initial averages clock vsplit vclock ;; clock variables box-edge ;; patch coords of box's edge fade-needed?]turtles-own [ speed mass energy ;; turtle info v1t v1l tmp-turtle ;; collision info (turtle 1) heading2 mass2 speed2 v2t v2l turtle2 ;; collision info (turtle 2) theta ;; collision info (both turtles)]to setup ca set box-edge (round (screen-edge-x * box-size / 100)) make-box set fade-needed? false set clock 0 set vclock 0 cct number [ set speed initspeed set mass initmass if who != 0 [ random-position ] rt random-float 360 set shape "circle" recolor ] update-variables set avg-speed-init avg-speed set avg-energy-init avg-energy setup-plots setup-histograms do-plotting do-histogramsendto update-variables ask turtles [ set energy (0.5 * speed * speed * mass) ] set medium count turtles with [color = green] set slow count turtles with [color = blue] set fast count turtles with [color = red] set avg-speed mean values-from turtles [speed] set avg-energy mean values-from turtles [energy] set vsplit ceiling max values-from turtles [speed]endto recalculate-vsplit ;; this needs to be done without-interruption to be sure that nothing tries to ;; use vsplit or vclock before they've been recalculated without-interruption [ set vsplit 2 * vsplit set vclock 2 * vclock ]endto go ask turtles [ bounce ] ask turtles [ move ] if trace? [ ask turtle 0 [ stamp gray set fade-needed? true ] ] set vclock (vclock + 1) if (vclock = vsplit) [ set clock (clock + 1) set vclock 0 update-variables do-plotting do-histograms if fade-needed? [ fade-patches ] ]endto bounce ;; turtle procedure locals [new-px new-py] ; if we're not about to hit a wall (yellow patch), ; we don't need to do any further checks if pcolor-of patch-ahead 1 != yellow [ stop ] ; get the coordinates of the patch we'll be on if we go forward 1 set new-px pxcor-of patch-ahead 1 set new-py pycor-of patch-ahead 1 ; check: hitting left or right wall? if (abs new-px = box-edge) ; if so, reflect heading around x axis [ set heading (- heading) ] ; check: hitting top or bottom wall? if (abs new-py = box-edge) ; if so, reflect heading around y axis [ set heading (180 - heading) ]endto move ;; turtle procedure while [(speed / vsplit) >= 1.0] [ recalculate-vsplit ] jump (speed / vsplit) check-for-collisionendto check-for-collision ;; turtle procedure if count other-turtles-here = 1 [ set tmp-turtle random-one-of other-turtles-here if ((who > who-of tmp-turtle) and (turtle2 != tmp-turtle)) [ collide ] ]endto collide ;; turtle procedure get-turtle2-info calculate-velocity-components set-new-speed-and-headingsendto get-turtle2-info ;; turtle procedure set turtle2 tmp-turtle set mass2 mass-of turtle2 set speed2 speed-of turtle2 set heading2 heading-of turtle2endto calculate-velocity-components locals [vcm] ;; CM vel. along dir. theta set theta (random-float 360) set v1l (speed * sin (theta - heading)) set v1t (speed * cos (theta - heading)) set v2l (speed2 * sin (theta - heading2)) set v2t (speed2 * cos (theta - heading2)) set vcm (((mass * v1t) + (mass2 * v2t)) / (mass + mass2)) set v1t (vcm + vcm - v1t) set v2t (vcm + vcm - v2t)endto set-new-speed-and-headings ;; turtle procedure set speed sqrt ((v1t * v1t) + (v1l * v1l)) set heading (theta - (atan v1l v1t)) set speed-of turtle2 sqrt ((v2t * v2t) + (v2l * v2l)) set heading-of turtle2 (theta - (atan v2l v2t)) recolor ask turtle2 [ recolor ]endto recolor ;; turtle procedure ifelse speed < (0.5 * initspeed) [ set color blue ] [ ifelse speed > (1.5 * initspeed) [ set color red ] [ set color green ] ]endto fade-patches locals [trace-patches] set trace-patches patches with [(pcolor != yellow) and (pcolor != black)] ifelse any? trace-patches [ ask trace-patches [ set pcolor ( pcolor - 0.4 ) if (not trace?) or (round pcolor = black) [ set pcolor black ] ] ] [ set fade-needed? false ]endto make-box ask patches with [((abs pxcor = box-edge) and (abs pycor <= box-edge)) or ((abs pycor = box-edge) and (abs pxcor <= box-edge))] [ set pcolor yellow ]endto random-position ;; turtle procedure setxy ((1 - box-edge) + random-float (2 * box-edge - 2)) ((1 - box-edge) + random-float (2 * box-edge - 2))end;;; plotting proceduresto setup-plots set-current-plot "Speed Counts" set-plot-y-range 0 numberendto do-plotting set-current-plot "Speed Counts" set-current-plot-pen "fast" plot fast set-current-plot-pen "medium" plot medium set-current-plot-pen "slow" plot slowendto setup-histograms set-current-plot "Speed Histogram" set-plot-x-range 0 (initspeed * 2) set-plot-y-range 0 ceiling (number / 6) set-current-plot-pen "medium" set-histogram-num-bars 40 set-current-plot-pen "slow" set-histogram-num-bars 40 set-current-plot-pen "fast" set-histogram-num-bars 40 set-current-plot-pen "init-avg-speed" draw-vert-line avg-speed-init set-current-plot "Energy Histogram" set-plot-x-range 0 (0.5 * (initspeed * 2) * (initspeed * 2) * initmass) set-plot-y-range 0 ceiling (number / 6) set-current-plot-pen "medium" set-histogram-num-bars 40 set-current-plot-pen "slow" set-histogram-num-bars 40 set-current-plot-pen "fast" set-histogram-num-bars 40 set-current-plot-pen "init-avg-energy" draw-vert-line avg-energy-initendto do-histograms set-current-plot "Speed Histogram" set-current-plot-pen "medium" histogram-from turtles with [color = green] [speed] set-current-plot-pen "slow" histogram-from turtles with [color = blue] [speed] set-current-plot-pen "fast" histogram-from turtles with [color = red] [speed] set-current-plot-pen "average-speed" plot-pen-reset draw-vert-line avg-speed set-current-plot "Energy Histogram" set-current-plot-pen "medium" histogram-from turtles with [color = green] [energy] set-current-plot-pen "slow" histogram-from turtles with [color = blue] [energy] set-current-plot-pen "fast" histogram-from turtles with [color = red] [energy] set-current-plot-pen "average-energy" plot-pen-reset draw-vert-line avg-energyend; draws a vertical line at xval on the current-plot with the current plot-pento draw-vert-line [xval] plotxy xval plot-y-min plot-pen-down plotxy xval plot-y-max plot-pen-upend; *** NetLogo Model Copyright Notice ***;; This model was created as part of the project: CONNECTED MATHEMATICS:; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL; MODELS (OBPML). The project gratefully acknowledges the support of the; National Science Foundation (Applications of Advanced Technologies; Program) -- grant numbers RED #9552950 and REC #9632612.;; Copyright 1998 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.;; This model was converted to NetLogo as part of the project:; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN; CLASSROOMS. The project gratefully acknowledges the support of the; National Science Foundation (REPP program) -- grant number REC #9814682.; Converted from StarLogoT to NetLogo, 2001. Updated 2002.;; To refer to this model in academic publications, please use:; Wilensky, U. (1998). NetLogo GasLab Gas in a Box model.; http://ccl.northwestern.edu/netlogo/models/GasLabGasinaBox.; Center for Connected Learning and Computer-Based Modeling,; Northwestern University, Evanston, IL.;; In other publications, please use:; Copyright 1998 by Uri Wilensky. All rights reserved. See; http://ccl.northwestern.edu/netlogo/models/GasLabGasinaBox; for terms of use.;; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW2691068845049494.13131313131313151101110CC-WINDOW5402798497Command CenterMONITOR18853269102clockclock01MONITOR188171269220avg-speedavg-speed2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -