Liquid Difusion - Bifocal Modeling Curriculum

Liquid Difusion - Bifocal Modeling Curriculum preview image

1 collaborator

Default-person Paulo Blikstein (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.2 • Viewed 182 times • Downloaded 14 times • Run 0 times
Download the 'Liquid Difusion - Bifocal Modeling Curriculum' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  tick-delta                                  ;; how much we advance the tick counter this time through
  max-tick-delta                              ;; the largest tick-delta is allowed to be
  drop-counter
  limit
  mouse-up?
  bored
  so-so
  excited
  something-cool
  found-error
  experiments
  left-temperature
  right-temperature
  temperature
]

breed [ erasers eraser ]
breed [ walls wall ]
breed [ particles particle ]

particles-own [
  speed mass energy          ;; particle info
  last-collision
  first-time
]

walls-own
[
  energy
  valve-1?
  valve-2?
  pressure?
  surface-energy
]

to setup
  ;; clear all
  ca
  set limit 50
  create-erasers 1 [set hidden? true set size 3 set color white]
  ;; import the bitmap
  ask patches [set pcolor white]
  ;;import-pcolors "becker.png"
  draw-beaker
  ask patches with [pycor < 70 and pycor > -80 and pxcor > 20 and pxcor < 59][set pcolor white]
  ;;
  set-default-shape particles "circle"
  ;; initialize drop counter
  set drop-counter 10000
  ;;
  set max-tick-delta 0.1073
  ;;
  make-particles-within-box
  set experiments "add ink"
  reset-ticks
  set left-temperature mean [speed] of particles with [xcor < 0]
  set right-temperature mean [speed] of particles with [xcor > 0]
  set temperature mean [speed] of particles
end 

;; creates initial particles

to make-particles-within-box
  create-particles 500 [
    setup-particle
    random-position
  ]
  calculate-tick-delta
end 

to calculate-tick-delta
  ;; tick-delta is calculated in such way that even the fastest
  ;; particle will jump at most 1 patch length in a tick. As
  ;; particles jump (speed * tick-delta) at every tick, making
  ;; tick length the inverse of the speed of the fastest particle
  ;; (1/max speed) assures that. Having each particle advance at most
  ;; one patch-length is necessary for it not to "jump over" a wall
  ;; or another particle.
  ifelse any? particles with [speed > 0]
    [ set tick-delta min list (1 / (ceiling max [speed] of particles)) max-tick-delta ]
    [ set tick-delta max-tick-delta ]
end 

to setup-particle  ;; particle procedure
  set speed temperatura-inicial
  set mass 3
  set energy (.5 * mass * speed * speed)
  set color cyan
  set size 4
  set first-time 1 ; was 0
  set last-collision nobody
end 

;; place particle at random location inside the box.

to random-position ;; particle procedure
  setxy (-72 + random-float (142)) -77 + random-float (120)
  set heading random-float 360
end 

to draw-beaker

  ask patches with [pxcor = -74 and (pycor >= -90 and pycor <= 80)] [set pcolor 0]
  ask patches with [pxcor = 73 and (pycor >= -90 and pycor <= 80)] [set pcolor 0]
  ask patches with [pycor = -90 and (pxcor >= -74 and pxcor <= 73)] [set pcolor 0]
end 

;; step the model

to go
  mouseme
  ifelse mouse-up? != false [
    if mouse-down? and drop-counter > 0 and experiments = "add ink" and mouse-ycor < 70 and mouse-xcor > -60 and mouse-xcor < 40[
      set mouse-up? false
      drop-ink
      set drop-counter drop-counter - 1
    ]
    if mouse-down? and experiments = "draw wall" [
      ask patch round mouse-xcor round mouse-ycor [
        set pcolor grey
        ask neighbors [set pcolor grey ask neighbors [set pcolor grey]]

      ]
    ]
    if mouse-down? and experiments = "heat up water" [
      set mouse-up? false
      drop-hot-water
    ]
    if mouse-down? and experiments = "cool down water" [
      set mouse-up? false
      drop-cold-water
    ]
  ]
  [
    if not mouse-down? [
      set mouse-up? true
    ]
  ]
  ask particles [ bounce ]
  ask particles [ move ]
  ask particles [ check-for-collision ]
  set left-temperature mean [speed] of particles with [xcor < 0]
  set right-temperature mean [speed] of particles with [xcor > 0]
  set temperature mean [speed] of particles
  tick-advance tick-delta
  calculate-tick-delta
  display
end 

to bounce  ;; particle procedure  ;; get the coordinates of the patch we will be on if we go forward 1
  if [pcolor ] of patch-here != white [die ask one-of particles with [color = cyan][hatch 1 [setup-particle] ]]
  let new-patch patch-ahead 1
  let new-px [pxcor] of new-patch
  let new-py [pycor] of new-patch
  ifelse [pcolor] of new-patch = white and new-py < limit [
    ;; if we're not about to hit a wall, we don't need to do any further checks
    stop
  ]
  [
    ;; a drop has to fall
    if (color = blue and ycor >= limit)[stop]
    ;; if hitting the top or bottom, reflect heading around y axis
    if (ycor < new-py - 1 or ycor > new-py - 1) [
      set heading (180 - heading) ;if first-time = 0 [set first-time 1] set speed speed
;      if color = blue and first-time <= 5 [
;        set speed 0.0001
;        set first-time first-time + 1
;      ]
    ]
    ;; if hitting the left or right, reflect heading around x axis
    if (xcor <= new-px - 1 or xcor >= new-px - 1) [
      set heading (360 - heading)
    ]
  ]
end 

to factor-gravity  ;; turtle procedure
  let gravity 0
  ifelse color = cyan [set gravity 0.01 ][set gravity .01 ]
  let vx (dx * speed)
  let vy (dy * speed) - (gravity * tick-delta) ;; fixed gravity now is 3.5 was
  set speed sqrt ((vy ^ 2) + (vx ^ 2))
  set heading atan vx vy
end 

to move  ;; particle procedure
  ;; In other GasLab models, we use "jump speed * tick-delta" to move the
  ;; turtle the right distance along its current heading.  In this
  ;; model, though, the particles are affected by gravity as well, so we
  ;; need to offset the turtle vertically by an additional amount.  The
  ;; easiest way to do this is to use "setxy" instead of "jump".
  ;; Trigonometry tells us that "jump speed * tick-delta" is equivalent to:
  ;;   setxy (xcor + dx * speed * tick-delta)
  ;;         (ycor + dy * speed * tick-delta)
  ;; so to take gravity into account we just need to alter ycor
  ;; by an additional amount given by the classical physics equation:
  ;;   y(t) = 0.5*a*t^2 + v*t + y(t-1)
  ;; but taking tick-delta into account, since tick-delta is a multiplier of t.
  let xcorr (xcor + dx * speed * tick-delta)
  if xcorr <= -117 or xcorr >= 116 [set xcorr xcor]
  let gravity 0
  ifelse color = cyan [set gravity 0.01 ][set gravity .01 ]
  ;;
  let ycorr (ycor + dy * speed * tick-delta - gravity * (0.5 * tick-delta * tick-delta))
  ifelse color = cyan [

  ]
  [
    ;if ycorr >= limit [set ycorr ycor - 1]
  ]
  if ycorr > 75 [die]
  setxy xcorr ycorr
  factor-gravity
end 

to drop-ink   ;Turtle procedure for releasing a drop onto the pond
  ask patch round mouse-xcor round mouse-ycor [
    sprout-particles 30 [
      set heading -180
      setxy round mouse-xcor + random 20 round mouse-ycor + random 10
      set speed temperatura-inicial
      set mass 1
      set energy (.5 * mass * speed * speed)
      set color blue
      set size 4
      set first-time 0
      set last-collision nobody
    ]
  ]
end 

to drop-cold-water
  ask patch round mouse-xcor round mouse-ycor [
    ask particles in-radius 20 [
      set speed 1
    ]
  ]
end 

to drop-hot-water
  ask patch round mouse-xcor round mouse-ycor [
    ask particles in-radius 20 [
      set speed 150
    ]
  ]
end 

to check-for-collision  ;; particle procedure

  let where (patch-set patch-at -1 1 patch-at 0 1 patch-at 1 1 patch-at -1 -1 patch-at 1 0 patch-at 0 0)
  let enemies other particles-on where
  if count enemies = 1 ;; modified to be realistic, was = 1
  [

    let candidate one-of enemies with
      [who < [who] of myself and myself != last-collision]

    if (candidate != nobody) and (speed > 0 or [speed] of candidate > 0)
    [
      collide-with candidate
      set last-collision candidate
      ask candidate [ set last-collision myself ]
    ]
  ]
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 component 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
  ;;; PHASE 1: initial setup

  ;; for convenience, grab some quantities from other-particle
  let mass2 [mass] of other-particle
  let speed2 [speed] of other-particle
  let 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.
  let 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
  let v1t (speed * cos (theta - heading))
  let v1l (speed * sin (theta - heading))
  ;; do the same for other-particle
  let v2t (speed2 * cos (theta - heading2))
  let v2l (speed2 * sin (theta - heading2))
  ;;; PHASE 3: manipulate vectors to implement collision
  ;; compute the velocity of the system's center of mass along theta
  let 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 ^ 2) + (v1l ^ 2))
  set energy (0.5 * mass * speed * speed)
  ;; 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
  ask other-particle [
    set speed sqrt ((v2t ^ 2) + (v2l ^ 2))
    set energy (0.5 * mass * (speed ^ 2))
    if v2l != 0 or v2t != 0
      [ set heading (theta - (atan v2l v2t)) ]
  ]
  ;; PHASE 5: final updates
  ;; no recoloring in our case
end 

to mouseme
  ask erasers [
    if mouse-inside? [setxy mouse-xcor mouse-ycor]
    if mouse-inside? [
      if experiments = "draw wall" [
        set shape "square" set color grey set size 6
      ]
      if experiments = "add ink" [
        set shape "none" set color blue set size 6
      ]
;      set shape "none"
      if experiments = "cool down water" [
        set shape "circle 3" set color blue set size 40
      ]
      if experiments = "heat up water" [
        set shape "circle 3" set color red set size 40
      ]
    ]
    set hidden? not mouse-inside?
  ]
end 

There are 4 versions of this model.

Uploaded by When Description Download
Paulo Blikstein almost 6 years ago updated translation Download this version
Paulo Blikstein almost 6 years ago 500 particles - new UI Download this version
Paulo Blikstein almost 6 years ago 500 particles Download this version
Paulo Blikstein almost 6 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Liquid Difusion - Bifocal Modeling Curriculum.png preview Preview for 'Liquid Difusion - Bifocal Modeling Curriculum' almost 6 years ago, by Paulo Blikstein Download

This model does not have any ancestors.

This model does not have any descendants.