Dispersion Model (patch based)

No preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 5.2.0-RC4 • Viewed 389 times • Downloaded 38 times • Run 0 times
Download the 'Dispersion Model (patch based)' modelDownload this modelEmbed this model

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


WHAT IS IT?

This is an agent-based approach to modeling continuous-flow chemical reactors. By using simple agent-level rules to recreate a number of known reaction engineering phenomena, the model is designed to serve as an educational tool for chemical engineering students.

The scope of the model is analogous to that of the convection-dispersion-reaction system taught in undergraduate chemical engineering courses. Namely, non-idealities due to fluid dynamics and bulk diffusion effects are rolled into a single diffusion mechanism called dispersion.

The model operates from the Eulerian reference frame, as all actions are assigned to stationary patches. This model is designed to supplement another NetLogo model operating from the Lagrangian reference frame (see RELATED MODELS), but may be used independently if desired.

HOW IT WORKS

The reactor is oriented such that the feed stream enters at the left boundary and leaves at the right boundary. The model consists of patches to which two properties are assigned: reactant and product concentrations.

On setup, the far left column of patches sprout reactant at a user-specified concentration (number per patch). If the FILL-REACTOR? option is switched on, all patches are filled with reactant in this manner.

A significant portion of the code was written for analysis purposes and is not crucial to the function of the reactor. Consequently, all such code has been omitted from the pseudo-code below.

At each tick:

1. Feed enters the reactor.  Patches at the far left boundary are asked to set their reactant and product concentrations to that of the feed.
2. Convection occurs.  Species flow as a result of each patch inheriting the concentrations of its upstream neighbor.  The FEED-VELOCITY is used to determine the number of convection steps per tick, and the single-step flow is repeated until this number of steps have been taken.
3. Diffusion occurs.  Each patch is asked to diffuse 100% of its contents to surrounding patches.  This action is repeated DIFFUSIVITY times per step.  
4. Reaction occurs.  The local rate is deterministically calculated by each patch.  The patch inserts its local concentrations into the specified rate equation, which is of the form:  rxn-rate = RATE-CONSTANT * (reactant) ^ RXN-ORDER.  This form is consistent with typical rate expressions for elementary reactions.
5. Performance metrics are calculated. Inlet and outlet average concentrations are used to determine the conversion, while patches along the longitudinal axis are aggregated for plotting concentration profiles.

HOW TO USE IT

The best way to the learn the model is to play around with parameters and view the results. The model is essentially 1-D, but has been constructed in 2-D so as to maximize interpretability. To run the model:

1. Select a desired FEED-CONCENTRATION. 

2. Select a FEED-VELOCITY to set the convection rate.

3. Select a DIFFUSIVITY to set the dispersion rate.

4. Select a RATE-CONSTANT and REACTION-ORDER to set the rate law.

5. Click Go

OPTIONAL: To export these parameters to the turtle-based model, click the EXPORT PARAMETERS button, then click the IMPORT PATCH MODEL PARAMS. button on the turtle model's interface.  Similarly, to match a turtle-model export its parameters then click the IMPORT TURTLE MODEL PARAMS. button on this model's interface.

THINGS TO NOTICE

The model is able to capture dynamic behavior, including startup, with no lag time. This is not typically coded into high level reactor models, and allows for some interesting analyses as well as potential for developing control strategies.

The model allows for direct input of reaction rate expressions. To model a real reactor, known kinetics can be entered into the patch-based model. The results can then be exported to the turtle-based model, where the DIFFUSIVITY is tuned to match the residence time distribution of a real tracer response experiment.

The stochastic reaction mechanisms of the turtle-based model can be mapped to real rate expressions here. Allow the turtle-based model to reach steady state with the desired mechanism and a sufficiently wide sampling window, then export its parameters. Import those parameters in the patch-based model and tune the rate law to match the reactant profile. In general, it appears that a first order rate constant is exactly the same as a CONCENTRATION-INDEPENDENT REACTION-PROBABILITY. Similarly, the CONCENTRATION-DEPENDENT rate mechanism from the turtle-based model produces a concentration profile that can be mapped to a second order reaction in the patch-model. The BIMOLECULAR mechanism maps to a first order model, presumably because collision with a second reactant is a requirement but does not actively increase the probability of reaction. In the CONCENTRATION-DEPENDENT mechanism, the REACTION-PROBABILITY scales linearly with the number of collisions.

THINGS TO TRY

Try obtaining a steady state in the turtle-based model and import the results to this model. Can you map the turtle-based model's stochastic reaction mechanisms to the deterministic rate laws in this model?

Try to achieve a perfectly mixed ideal CSTR. Is this feasible in this model? Is it feasible in the turtle-based model? If not, try to determine why the two models might differ in this regard.

EXTENDING THE MODEL

Potential extensions could entail:

1. Building a means to import a reaction network, complete with multiple products, reactants, and stoichiometries.
2. Implementation of heat effects.  
3. Addition of a mechanism that tracks residence time.

RELATED MODELS

The accompanying turtle-based model is available on the NetLogo Modeling Commons. This model handles everything from the perspective of mobile reactants. :

http://modelingcommons.org/browse/onemodel/4401#modeltabsbrowseinfo

CREDITS AND REFERENCES

The model was created for EECS 472 by Sebastian Bernasek at Northwestern University. The latest version of this model is available on the NetLogo Modeling Commons:

http://modelingcommons.org/browse/onemodel/4402#modeltabsbrowseinfo

Comments and Questions

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

Click to Run Model

patches-own [reactant-in product-in reactant product rxn-rate]
globals [centerline reactant-profile reactant-profile-turtle]


;;GLOBAL VARIABLES:
  ;;centerline = agentset of patches with pycor = 0
  ;;reactant-profile = ordered list of reactant concentrations along the longitudinal axis (centerline) of the current reactor
  ;;reactant-profile-turtle = ordered list of reactant concentrations along the longitudinal axis (centerline) of the imported turtle-based reactor


;;PATCH VARIABLES:
  ;;reactant,product = local concentration of reactant/product within the present patch
  ;;reactant-in,product-in = local concentration of reactant/product in the patch directly upstream (will become my concentration after convection)
  ;;rxn-rate = local change in reactant/product concentration, expressed in mol/L/s

to setup
  clear-all
  
  if fill-reactor?
    [ask patches [set reactant feed-concentration] 
     recolor]
  
  reset-ticks  
end 

to go
;;1 tick = 1 second

  ask patches with [pxcor = min-pxcor] [feed-reactor]
  convective-transport
  diffusive-transport
  ask patches with [pxcor > min-pxcor] [run-kinetics]
  calculate-performance
  recolor
  update-plots
  tick
end 

to feed-reactor  ;;Patch procedure
;;Ask patches to set their concentration to that of the feed.
  
    set reactant feed-concentration
    set product 0
end 

to run-kinetics  ;;Patch procedure
;;Calculate instantaneous deterministic rxn rate at each patch according to local concentration
;;Ask each patch to add the quantity reacted per tick to their previous concentration 

   set rxn-rate (rate-constant * ( reactant ^ rxn-order )) ;mol reacted / L / s
   set reactant ( reactant - rxn-rate )
   set product ( product + rxn-rate )
end 

to convective-transport
;;RUNS CONVECTION PROCESS FOR PATCHES  
  
;;Set ticks per convection step
  let steps-per-tick  (( feed-velocity * (world-width - 1)  ) / reactor-length)
  
  ;;Ask patches to undergo convection steps-per-tick times per tick
  let counter 0
  while [counter < steps-per-tick]
    [flow
      set counter (counter + 1)]
end 

to flow
;;ACTUAL CONVECTIVE TRANSPORT OCCURS HERE
  ;;Fluid moves one patch per tick.  
  ;;Patches store the incoming reactant and product before setting their own in order to make sure that every patch inherits the current value of its upstream neighbor.
  
  ;;Store concentration of incoming fluid
  ask patches with [pxcor > min-pxcor] [
    set reactant-in [reactant] of patch-at -1 0
    set product-in [product] of patch-at -1 0]
  
  ;;Set new concentrations    
  ask patches with [pxcor > min-pxcor] [
    set reactant reactant-in
    set product product-in]
end 

to diffusive-transport
;;DIFFUSIVE MECHANISM FOR THE PATCH-BASED MODEL
  
  ;;Ask patches to undergo diffusion "diffusivity" times per tick.  This keeps diffusion on the same time scale as convection, as they are both in units of "executions per tick".
  let counter 0
  while [counter < diffusivity]
    [diffuse reactant 1
      diffuse product 1
      set counter (counter + 1)]
end 

to calculate-performance
;;CALCULATES PERFORMANCE METRICS FOR THE REACTOR
  
  ;;Defines "centerline" as patches lying along the longitudinal axis.  Convenient for calculating profiles
  set centerline patches with [pycor = 0]
  
  ;;Store reactant concentration profile
  set reactant-profile (map [[reactant] of ?] (sort-on [pxcor] centerline))   
end 

to recolor
;;RECOLORS PATCHES AT EACH TICK. COLOR DEPENDS UPON VISUALIZATION MODE
  
  if visualization-mode = "reactant-profile"
    [ask patches [set pcolor scale-color blue reactant 0 (feed-concentration * 2)]] ;;Scales color between blue (high reactant concentration) and black (no/low reactant)
  
  if visualization-mode = "product-profile"
    [ask patches [set pcolor scale-color red product 0 (feed-concentration * 2)]] ;;Scales color between red (high product concentration) and black (no/low products)
  
  if visualization-mode = "blended-profile"
    [ask patches 
      [ifelse (reactant + product) = 0
        [set pcolor black ]                                                     ;;If patch is empty, set color black
        [let reactant-frac (reactant / (reactant + product))                                     ;;Reactant mole fraction 
          set pcolor rgb ((1 - reactant-frac) * 255) 0 (reactant-frac * 255)]]] ;;Scales color between blue (reactants) and red (products)
end 

to import-turtle-model
;;FUNCTION IMPORTS PARAMETERS AND RESULTS FROM TURTLE-BASED MODEL
;;THEN SETS CURRENT MODEL TO THOSE OPERATING PARAMETERS 
  
  
  ;;Open file containing data from turtle-based model
  file-open "turtle-results.csv"
  
  ;;If file is at the end (i.e. already imported once), close it and reopen it.
  if file-at-end? 
    [file-close-all
      file-open "turtle-results.csv"]
  
  
  ;;Store each of the variables from the other model.  List order is known a priori, not ideal but... no tengo tiempo!
  
  let dim                 read-from-string (file-read-line)
  resize-world (item 0 dim) (item 1 dim) (item 2 dim) (item 3 dim)          ;;Set dimensions to those of turtle model
  set feed-velocity       read-from-string (file-read-line)                 ;;Sets feed-velocity to turtle-based model's value
  set diffusivity         (2 * round (read-from-string (file-read-line)))   ;;Sets diffusivity to turtle-based model's value, rounded because patch model takes integer values.  The 8x is an approximate scaling factor between the two mechanisms..
  set rate-constant       read-from-string (file-read-line)                 ;;Sets rate constant to turtle-based model's reaction-probability 
  let reaction-type       file-read-line                                    ;;Stores the reaction type (i.e. conc. ind./dep. or bimolecular) as string
  set reactant-profile-turtle read-from-string (file-read-line)                 ;;Import results (conc. profile) from turtle-based model
  
  
                                                                                ;;Set appropriate rxn order based on turtle model's mechanism
  import-rxn-order reaction-type
  
  ;;Close file, resetting line count.
  file-close-all
end 

to import-rxn-order [reaction-type]
;;CONVERTS TURTLE-BASED MODEL MECHANISM TO RXN ORDER.  BASED ON INSIGHT FROM THIS MODEL!
  
  show reaction-type
  
  ifelse reaction-type = "Bimolecular" or reaction-type = "Concentration Independent"
    [set rxn-order 1]
    [set rxn-order 2]
end 

to export-results
;;WRITE OPERATING PARAMETERS AND CONCENTRATION PROFILE TO A FILE FROM WHICH IT CAN BE IMPORTED TO TURTLE MODEL

;;Normalize reactant concentration profile 
  let raw-profile (map [([reactant] of ?)] (sort-on [pxcor] centerline))
  let normalized-profile (map [? / (item (world-width - 10) raw-profile)] raw-profile)
  
  
  ;;Create file (or delete old one and reopen)   
  ifelse file-exists? "patch-results.csv"
    [file-delete "patch-results.csv"
      file-open "patch-results.csv" ]
    [file-open "patch-results.csv"]
  
  ;;Write variables to file.  I've left out labels because I can't figure out how to make the import step recognize them.
  file-print (list min-pxcor max-pxcor min-pycor max-pycor)
  file-print feed-velocity
  file-print diffusivity
  file-print (rate-constant)  
  file-print normalized-profile     
  
  ;;Save and close file  
  file-flush
  file-close
end 

to-report conversion
;;CALCULATES FRACTIONAL CONVERSION
  
  ;;Conversion is defined as: ((reactant-in - reactant-out) / reactant-in )
  let outlet (mean [reactant] of patches with [pxcor = max-pxcor])
  let inlet  (mean [reactant] of patches with [pxcor = min-pxcor])
  ifelse inlet > 0
    [report (( inlet - outlet ) / inlet)]
    [report 0]
end 

to-report mean-sq-error [profile1 profile2]
;;REPORTS MEAN SQUARED ERROR BETWEEN PATCH AND TURTLE BASED MODELS

  ;;Turtle concentration profile is scaled to a feed of 1 before export.  Here it's rescaled based on the concentration at the point 10 patches from the end of the reactor.
  let scaled-patch-conc ([reactant] of (item (world-width - 10) (sort-on [pxcor] centerline)))
  let scaled-profile2 map [? * scaled-patch-conc ] profile2 
  
  ;;Filters out first ten and last five entries from profile.  These are the regions where noise has a strong influence.
  let filtered-profile1 sublist profile1 (10) ((length profile1) - 5)
  let filtered-profile2 sublist scaled-profile2 (10) ((length scaled-profile2) - 5)
  
  
  ;;Calculates sum of mean squared error between profiles
  let squared-errors (map [(?1 - ?2) ^ 2] filtered-profile1 filtered-profile2)
  
  ;;Report sum of squared error
  report (sum squared-errors)   
end 






There are 5 versions of this model.

Uploaded by When Description Download
Sebastian Bernasek almost 9 years ago updated interface Download this version
Sebastian Bernasek almost 9 years ago updated interface, info Download this version
Sebastian Bernasek almost 9 years ago info tab completed Download this version
Sebastian Bernasek almost 9 years ago Import/Export Capability, fixed convection Download this version
Sebastian Bernasek almost 9 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Bernasek_Sebastian_Report_ds.pdf pdf Final Report for EECS472 (double spaced) almost 9 years ago, by Sebastian Bernasek Download
Bernasek_Sebastian_Report_ss.pdf pdf Final Report for EECS472 (single spaced) almost 9 years ago, by Sebastian Bernasek Download

This model does not have any ancestors.

This model does not have any descendants.