---p1_particle_behavior_3.nlogo
来自「NETLOGO」· NLOGO 代码 · 共 471 行
NLOGO
471 行
globals [ clock tick-length ;; clock variables. box-edge ;; distance of box edge from axes. tracked-particle ;; particle we're currently tracking. duration ;; set from Pedagogica. avg-speed ;; average current speed of all the particles. avg-speed-history ;; history of average speed over the model run. avg-avg-speed ;; average of avg-speed over time. num-speed-changes ;; how many times the tracked-particle has changed ;; speed.]breeds [ particles ]particles-own [ speed mass ;; particle info last-collision]to setup ca set-default-shape particles "circle" set clock 0 set duration 0 ;; box has constant size... set box-edge (screen-edge-x - 1) make-box make-particles set tracked-particle nobody set avg-speed-history [] update-avg-speedendto go locals [old-clock] ;; the model stops after DURATION clock ticks. if (clock >= duration) [ set avg-avg-speed (mean avg-speed-history) stop ] ask particles [ bounce ] ask particles [ move ] ask particles [ ;; without-interruption is needed here so one collision is ;; happening at a time without-interruption [ check-for-collision ] ] set old-clock clock set clock clock + tick-length if (floor clock) != (floor old-clock) [ update-avg-speed ] set tick-length 1 / (ceiling max values-from particles [speed])endto update-avg-speed set avg-speed (mean values-from particles [speed]) set avg-speed-history (lput avg-speed avg-speed-history)endto bounce ;; particle procedure locals [new-px new-py] ;; if we're not about to hit a wall (yellow patch), or if we're already on a ;; wall, we don't need to do any further checks if pcolor = yellow or pcolor-of patch-at dx dy != yellow [ stop ] ;; get the coordinates of the patch we'll be on if we go forward 1 set new-px round (xcor + dx) set new-py round (ycor + dy) ;; if hitting left or right wall, reflect heading around x axis if (abs new-px = box-edge) [ set heading (- heading) ] ;; if hitting top or bottom wall, reflect heading around y axis if (abs new-py = box-edge) [ set heading (180 - heading) ]endto move ;; particle procedure if patch-ahead (speed * tick-length) != patch-here [ set last-collision nobody ] jump (speed * tick-length)endto check-for-collision ;; particle procedure locals [ candidate ] if count other-particles-here >= 1 [ ;; the following conditions are imposed on collision candidates: ;; 1. they must have a lower who number than my own, because collision ;; code is asymmetrical: it must always happen from the point of view ;; of just one particle. ;; 2. they must not be the same particle that we last collided with on ;; this patch, so that we have a chance to leave the patch after we've ;; collided with someone. set candidate random-one-of other-particles-here with [who < who-of myself and myself != last-collision] ;; we also only collide if one of us has non-zero speed. It's useless ;; (and incorrect, actually) for two particles with zero speed to collide. if (candidate != nobody) and (speed > 0 or speed-of candidate > 0) [ collide-with candidate set last-collision candidate set last-collision-of candidate self ;; we want to keep track of how many times the tracked-particle ;; changes speed. only collisions change the speed of a particle. if self = tracked-particle or candidate = tracked-particle [ set num-speed-changes num-speed-changes + 1 ] ] ]end;; tracks the specified particle, if no particle is currently being tracked.;; if there's no such particle, it just prints a warning to the command;; center.to track-particle [n] ifelse tracked-particle != nobody [ print "You are already tracking particle " + who-of tracked-particle ] [ ifelse (turtle n) = nobody [ print "There is no such particle!" ] [ set tracked-particle (turtle n) ask tracked-particle [ pen-down ] print "Now tracking particle " + who-of tracked-particle ] ]end;; implements a collision with another particle.;;;; THIS IS THE HEART OF THE PARTICLE SIMULATION, AND YOU ARE STRONGLY ADVISED;; NOT TO CHANGE IT UNLESS YOU REALLY UNDERSTAND WHAT YOU'RE DOING!;;;; The two particles colliding are self and other-particle, and while the;; collision is performed from the point of view of self, both particles are;; modified to reflect its effects. This is somewhat complicated, so I'll;; give a general outline here: ;; 1. Do initial setup, and determine the heading between particle centers;; (call it theta).;; 2. Convert the representation of the velocity of each particle from;; speed/heading to a theta-based vector whose first component is the;; particle's speed along theta, and whose second compenent is the speed;; perpendicular to theta.;; 3. Modify the velocity vectors to reflect the effects of the collision.;; This involves:;; a. computing the velocity of the center of mass of the whole system;; along direction theta;; b. updating the along-theta components of the two velocity vectors.;; 4. Convert from the theta-based vector representation of velocity back to;; the usual speed/heading representation for each particle.;; 5. Perform final cleanup and update derived quantities.to collide-with [ other-particle ] ;; particle procedure locals [ ;; local copies of other-particle's relevant quantities mass2 speed2 heading2 ;; quantities used in the collision itself theta ;; heading of vector from my center to the center of other-particle. v1t ;; velocity of self along direction theta v1l ;; velocity of self perpendicular to theta v2t v2l ;; velocity of other-particle, represented in the same way vcm ;; velocity of the center of mass of the colliding particles, ;; along direction theta ] ;;; PHASE 1: initial setup ;; for convenience, grab some quantities from other-particle set mass2 mass-of other-particle set speed2 speed-of other-particle set heading2 heading-of other-particle ;; since particles are modeled as zero-size points, theta isn't meaningfully ;; defined. we can assign it randomly without affecting the model's outcome. set theta (random-float 360) ;;; PHASE 2: convert velocities to theta-based vector representation ;; now convert my velocity from speed/heading representation to components ;; along theta and perpendicular to theta set v1t (speed * cos (theta - heading)) set v1l (speed * sin (theta - heading)) ;; do the same for other-particle set v2t (speed2 * cos (theta - heading2)) set v2l (speed2 * sin (theta - heading2)) ;;; PHASE 3: manipulate vectors to implement collision ;; compute the velocity of the system's center of mass along theta set vcm (((mass * v1t) + (mass2 * v2t)) / (mass + mass2) ) ;; now compute the new velocity for each particle along direction theta. ;; velocity perpendicular to theta is unaffected by a collision along theta, ;; so the next two lines actually implement the collision itself, in the ;; sense that the effects of the collision are exactly the following changes ;; in particle velocity. set v1t (2 * vcm - v1t) set v2t (2 * vcm - v2t) ;;; PHASE 4: convert back to normal speed/heading ;; now convert my velocity vector into my new speed and heading set speed sqrt ((v1t * v1t) + (v1l * v1l)) ;; if the magnitude of the velocity vector is 0, atan is undefined. but ;; speed will be 0, so heading is irrelevant anyway. therefore, in that ;; case we'll just leave it unmodified. if v1l != 0 or v1t != 0 [ set heading (theta - (atan v1l v1t)) ] ;; and do the same for other-particle set speed-of other-particle sqrt ((v2t * v2t) + (v2l * v2l)) if v2l != 0 or v2t != 0 [ set heading-of other-particle (theta - (atan v2l v2t)) ] ;; PHASE 5: final updates ;; now recolor, since color is based on quantities that may have changed recolor ask other-particle [ recolor ]endto recolor ;; particle procedure ifelse speed < (0.5 * 10) [ set color blue ] [ ifelse speed > (1.5 * 10) [ set color red ] [ set color green ] ]end;;;;;; drawing procedures;;;;; draws the boxto 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 ]end;; creates some particlesto make-particles create-custom-particles number [ set speed random-float 20 set mass 1.0 set last-collision nobody random-position recolor ] set tick-length 1 / (ceiling max values-from particles [speed])end;; place particle at random location inside the box.to random-position ;; particle procedure setxy ((1 - box-edge) + random-float ((2 * box-edge) - 2)) ((1 - box-edge) + random-float ((2 * box-edge) - 2)) set heading random-float 360end;; make turtles appear more like particlesto-report particle [n] report turtle nend; *** 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-WINDOW2381149428835353.465110111CC-WINDOW7232180314Command CenterBUTTON651014243go/stopgoT1TOBSERVERTBUTTON7106243NILsetupNIL1TOBSERVERTMONITOR1511822067Numbercount particles01SLIDER74514278numbernumber130010011NILMONITOR15179221128clockfloor clock01MONITOR780140129tracked part. speedspeed-of tracked-particle31MONITOR7181119230average speedavg-speed31MONITOR7130212179tracked part. speed changesnum-speed-changes01@#$#@#$#@(in this section of the interface, information about the model is usually provided) You may freely download NetLogo at your home!Go to our site: http://ccl.northwestern.edu/netlogo/You can view models through a web browser as well:http://ccl.northwestern.edu/netlogo/models/ This model is part of the Connected Chemistry high school curriculum. Connected Chemistry is currently under development; for more information please refer to:http://ccl.northwestern.edu/curriculum/chemistry/ CREDITS AND REFERENCES----------------------To refer to this model in academic publications, please use: Wilensky, U. (2002). NetLogo P1_Particle_Behavior_3 model. http://ccl.northwestern.edu/netlogo/models/P1_Particle_Behavior_3. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. In other publications, please use: Copyright 2002 by Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/P1_Particle_Behavior_3 for terms of use.@#$#@#$#@defaulttrue0Polygon -7566196 true true 150 5 40 250 150 205 260 250circlefalse0Circle -7566196 true true 35 35 230@#$#@#$#@NetLogo 2.0.0@#$#@#$#@@#$#@#$#@@#$#@#$#@
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?