<jupyter_start><jupyter_text>Water Tank Example for the Basic Modeling Interface (BMI)  Using the dill Python Package to Serialize Model State This Jupyter notebook is a successor to "Water_Tank_BMI_Example.ipynb".  In this notebook, we demonstrate how a model's state can be serialized using a Python package called dill, which extends Python's built-in pickle module.  See Appendix 1 for instructions on how to set up a conda environment for running this notebook. First, we import the model class and create an instance of it.<jupyter_code>import water_tank_bmi_2021 as water_tank_module  # Import 2021 version for Python 3
tank_model = water_tank_module.water_tank()      # Create an instance of the water tank model<jupyter_output><empty_output><jupyter_text>Next, we initialize the model with the default configuration file.<jupyter_code>import os
os.chdir('input_files')
cfg_file = 'tank_info.cfg'
tank_model.initialize( cfg_file)                   # Initialize the model<jupyter_output>Reading file = tank_info.cfg
   number of lines = 6

Reading file = tank_info.cfg
   data format = key_value

Reading file = tank_info.cfg
   dt         = 4000.0 [sec]
   n_steps    = 300
   init_depth = 1.0 [m]
   top_radius = 20.0 [m]
   top_area   = 1256.6370614359173 [m2]
   out_radius = 0.05 [m]
   out_speed  = 2.2147234590350102 [m/s]
   depth      = 1.0 [m]
   volume     = 1256.6370614359173 [m3]
   out_area   = 0.007853981633974483 [m2]
   rain_file  = rain_data.txt<jupyter_text>Next, we udate the model state 8 timesteps.<jupyter_code>for k in range(8):
    tank_model.update()<jupyter_output>--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.9890763579685691 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.9233911114137668 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.8614255774552847 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.8030924409613482 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.748300193314165 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.696953035201209 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.6489508107270017 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.6041889811962382 [meters]<jupyter_text>Next, we use dill to serialize the entire model state.<jupyter_code>import dill
os.chdir('../state_files')
model_state_file = 'tank_model_state1.bin'
dill.dump( tank_model, open(model_state_file, mode='wb'))<jupyter_output><empty_output><jupyter_text>Next, we will load the saved model state into a new model object.<jupyter_code>tank_model2 = dill.load( open(model_state_file, mode='rb') )
os.chdir('../input_files')
print( tank_model2.depth )
print( tank_model2.out_speed )
tank_model2.time_index
tank_model2.get_time_units()
tank_model2.get_time_step()
tank_model2.print_tank_data()
for k in range(5):
    tank_model2.update()<jupyter_output>--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.562558647790729 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.5239466330129443 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.4882356310590233 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.45530443705142476 [meters]
--------------------------------------
rain_rate = 40.0  [mmph]
depth     = 0.4250282641312951 [meters]
{'fraction_non_alphanumeric': 0.21951219512195122, 'fraction_numerical': 0.10310421286031042, 'mean_word_length': 2.2989031078610602, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '590181', 'license': 'MIT', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1198, 'n_words': 297, 'path': 'notebooks/Water_Tank_BMI_dill_Example.ipynb', 'repo_name': 'peckhams/water_tank_bmi', 'size': '3608'}