BMI Live!

Let's use this notebook to test our BMI as we develop it.

Setup

Before we start, make sure you've installed the basic-modeling-interface package:

$ pip install basic-modeling-interface

Also install our bmi-live-2017 package in developer mode:

$ python setup.py develop

Last, an import for later:


In [ ]:
import numpy as np

Test the BMI methods

Import the BmiDiffusion class from the bmi-live-2017 package and make an instance:


In [ ]:
from bmi_live.bmi_diffusion import BmiDiffusion
x = BmiDiffusion()

What's the name of this component?


In [ ]:
print x.get_component_name()

Show the input and output variables for the component:


In [ ]:
print x.get_input_var_names()
print x.get_output_var_names()

Initialize the Diffusion model through its BMI:


In [ ]:
x.initialize('../data/diffusion.yaml')

Check the time information for the model.


In [ ]:
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()

Get the model's initial temperature field through the BMI:


In [ ]:
temp = x.get_value('plate_surface__temperature')
#print temp  # flattened!
print temp.reshape(x._model.temperature.shape)  # dimensional

Add a unit impulse to the initial temperature field:


In [ ]:
temp[20] = 100.0
x.set_value('plate_surface__temperature', temp)

Check that the temperature field has been updated:


In [ ]:
temp = x.get_value('plate_surface__temperature')
#print temp
print temp.reshape(x._model.temperature.shape)

Next, get attributes of the grid on which the temperature variable is defined:


In [ ]:
grid_id = x.get_var_grid('plate_surface__temperature')
print 'Grid id:', grid_id
grid_shape = x.get_grid_shape(grid_id)
print 'Grid shape:', grid_shape
print 'Grid spacing:', x.get_grid_spacing(grid_id)
print 'Grid origin:', x.get_grid_origin(grid_id)
print 'Grid type:', x.get_grid_type(grid_id)

Now advance the model by a single time step:


In [ ]:
x.update()

View the new state of the temperature field:


In [ ]:
temp = x.get_value('plate_surface__temperature')
print temp.reshape(grid_shape)

There's diffusion!

Advance another step:


In [ ]:
x.update()

View the new state of the temperature field (with help from np.set_printoptions):


In [ ]:
temp = x.get_value('plate_surface__temperature')
np.set_printoptions(formatter={'float': '{: 6.2f}'.format})
print temp.reshape(grid_shape)

Advance the model to some distant time:


In [ ]:
distant_time = 5.0
x.update_until(distant_time)

View the new state of the temperature field:


In [ ]:
temp = x.get_value('plate_surface__temperature')
print temp.reshape(grid_shape)

Finalize the model:


In [ ]:
x.finalize()