Table of Contents

  • 1  Import the functions and classes you’ll need from the build_2D_matrix module

  • 2  Import the state from the original notebook

  • 3  Turn the npz object into a dictionary

  • 4  Summary

restarting a notebook from a checkpoint

Import the functions and classes you’ll need from the build_2D_matrix module

[1]:
import numpy as np
import build_2D_matrix
print(dir(build_2D_matrix))
['Boundary_Def', 'Problem_Def', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'avg', 'build_2D_matrix', 'index_to_row_col']
[2]:
from build_2D_matrix import Boundary_Def, Problem_Def, build_2D_matrix, index_to_row_col

Import the state from the original notebook

[3]:
import numpy as np
in_file = 'savestate.npz'
values = np.load(in_file)
print(list(values.keys()))
['n_x', 'n_y', 'poro', 'width_x', 'width_y', 'Qsource', 'c0', 'c_init', 'Diff', 'save_date', 'case_name', 'comment', 'history', 'units']

Turn the npz object into a dictionary

  • This is a good time to review list, dictionary and set comprehensions: Vanderplas section 11

  • Essentially we’re doing this so we can get a nicely formatted printout of the values

[4]:
values_dict={key:value for key,value in values.items()}
values_dict
[4]:
{'n_x': array(51),
 'n_y': array(1),
 'poro': array(0.4),
 'width_x': array(10),
 'width_y': array(0),
 'Qsource': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
 'c0': array(1),
 'c_init': array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
 'Diff': array(0.01728),
 'save_date': array('March 3, 2019', dtype='<U13'),
 'case_name': array('1D simulation', dtype='<U13'),
 'comment': array('simple x-only domain', dtype='<U20'),
 'history': array('written by 2D_Assignment_Transient_Error_checkpoint.ipynb',
       dtype='<U57'),
 'units': array(['Diff: dm²/day', 'length: dm', 'concentration: mg/L'], dtype='<U19')}
[5]:
c0 = values_dict['c0']
[6]:
# Here we create 4 boundaries, west has a constant concentration at c0, east has a constant boundary at 0;
west = Boundary_Def("const", val=c0)
east = Boundary_Def("const", val=0)

# For 1D problem, the used boundaries are west and east.

# The other south and north boundaries have a zero flux (impermeable)

north = Boundary_Def("flux", val=0)
south = Boundary_Def("flux", val=0)
[7]:
bc_dict = {"west": west, "north": north, "east": east, "south": south}
# The latter array bc_dict will be sent to the different functions
[8]:
n_x, n_y = values_dict['n_x'], values_dict['n_y']
Diff = values_dict['Diff']
width_x, width_y = values_dict['width_x'], values_dict['width_y']
n = n_x * n_y
x = np.linspace(0, width_x, n_x)
c_init = values_dict['c_init']
D_matrix = Diff * np.ones(n)
poro = values_dict['poro']
prob = Problem_Def(n_x, n_y, poro, width_x, width_y)
Qsource = values_dict['Qsource']
A, b = build_2D_matrix(bc_dict, prob, D_matrix, Qsource)

Summary

  • modules and npz files allow you to save classes, functions and data

  • Note that numpy doesn’t handle strings in a particularly elegant manner, and that npz files can’t be edited by hand, or read by non-pyrhon programs. We’ll go through how to address these issues in another notebook