Before we start, make sure you've installed the bmipy
package:
$ conda install bmipy -c conda-forge
Also install our bmi-live
package in developer mode:
$ python setup.py develop
Last, a pair of imports for later:
In [1]:
import os
import numpy as np
Start by importing the BmiDiffusion
class from the bmi-live package:
In [2]:
from bmi_live import BmiDiffusion
Create an instance of the model's BMI.
In [3]:
x = BmiDiffusion()
What's the name of this component?
In [4]:
print(x.get_component_name())
Show the input and output variables for the component:
In [5]:
print(x.get_input_var_names())
print(x.get_output_var_names())
Locate a sample configuration file included with the bmi-live package:
In [6]:
from bmi_live import data_directory
cfg_file = os.path.join(data_directory, 'diffusion.yaml')
Use the sample configuration to initialize the Diffusion model through its BMI:
In [7]:
x.initialize(cfg_file)
Check the time information for the model.
In [8]:
print('Start time:', x.get_start_time())
print('End time:', x.get_end_time())
print('Current time:', x.get_current_time())
print('Time step:', x.get_time_step())
print('Time units:', x.get_time_units())
Next, get attributes of the grid on which the temperature variable is defined:
In [9]:
grid_id = x.get_var_grid('plate_surface__temperature')
print('Grid id:', grid_id)
grid_rank = x.get_grid_rank(grid_id)
print('Grid rank:', grid_rank)
grid_shape = np.ndarray(grid_rank, int)
x.get_grid_shape(grid_id, grid_shape)
print('Grid shape:', grid_shape)
grid_spacing = np.ndarray(grid_rank, float)
x.get_grid_spacing(grid_id, grid_spacing)
print('Grid spacing:', grid_spacing)
print('Grid type:', x.get_grid_type(grid_id))
Get the model's initial temperature field through the BMI:
In [10]:
temp = np.ndarray(grid_shape).flatten() #flattened!
x.get_value('plate_surface__temperature', temp)
print(temp.reshape(grid_shape)) # dimensional
Add an impulse to the initial temperature field:
In [11]:
temp[20] = 100.0
x.set_value('plate_surface__temperature', temp)
Check that the temperature field has been updated:
In [12]:
x.get_value('plate_surface__temperature', temp)
print(temp.reshape(grid_shape))
Now advance the model by a single time step:
In [13]:
x.update()
View the new state of the temperature field:
In [14]:
x.get_value('plate_surface__temperature', temp)
print(temp.reshape(grid_shape))
There's diffusion!
Advance another step:
In [15]:
x.update()
View the new state of the temperature field (with help from np.set_printoptions
):
In [16]:
x.get_value('plate_surface__temperature', temp)
np.set_printoptions(formatter={'float': '{: 6.2f}'.format})
print(temp.reshape(grid_shape))
Advance the model to some distant time:
In [17]:
distant_time = 5.0
while x.get_current_time() < distant_time:
x.update()
View the new state of the temperature field:
In [18]:
x.get_value('plate_surface__temperature', temp)
print(temp.reshape(grid_shape))
Finalize the model:
In [19]:
x.finalize()