Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get and set state examples #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
386 changes: 386 additions & 0 deletions examples/.ipynb_checkpoints/run-model-from-bmi-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,386 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run the `Heat` model through its BMI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`Heat` models the diffusion of temperature on a uniform rectangular plate with Dirichlet boundary conditions. View the source code for the [model](https://github.com/csdms/bmi-example-python/blob/master/heat/heat.py) and its [BMI](https://github.com/csdms/bmi-example-python/blob/master/heat/bmi_heat.py) on GitHub."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start by importing `os`, `numpy` and the `Heat` BMI:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"\n",
"from heat import BmiHeat"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an instance of the model's BMI."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x = BmiHeat()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What's the name of this model?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The 2D Heat Equation\n"
]
}
],
"source": [
"print(x.get_component_name())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start the `Heat` model through its BMI using a configuration file:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# Heat model configuration\n",
"shape:\n",
" - 6\n",
" - 8\n",
"spacing:\n",
" - 1.0\n",
" - 1.0\n",
"origin:\n",
" - 0.0\n",
" - 0.0\n",
"alpha: 1.0\n"
]
}
],
"source": [
"cat heat.yaml"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"x.initialize(\"heat.yaml\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check the time information for the model."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Start time: 0.0\n",
"End time: 1.7976931348623157e+308\n",
"Current time: 0.0\n",
"Time step: 0.25\n",
"Time units: s\n"
]
}
],
"source": [
"print(\"Start time:\", x.get_start_time())\n",
"print(\"End time:\", x.get_end_time())\n",
"print(\"Current time:\", x.get_current_time())\n",
"print(\"Time step:\", x.get_time_step())\n",
"print(\"Time units:\", x.get_time_units())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Show the input and output variables for the component (aside on [Standard Names](https://csdms.colorado.edu/wiki/CSDMS_Standard_Names)):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(x.get_input_var_names())\n",
"print(x.get_output_var_names())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, get the identifier for the grid on which the temperature variable is defined:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"grid_id = x.get_var_grid(\"plate_surface__temperature\")\n",
"print(\"Grid id:\", grid_id)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then get the grid attributes:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Grid type:\", x.get_grid_type(grid_id))\n",
"\n",
"rank = x.get_grid_rank(grid_id)\n",
"print(\"Grid rank:\", rank)\n",
"\n",
"shape = np.ndarray(rank, dtype=int)\n",
"x.get_grid_shape(grid_id, shape)\n",
"print(\"Grid shape:\", shape)\n",
"\n",
"spacing = np.ndarray(rank, dtype=float)\n",
"x.get_grid_spacing(grid_id, spacing)\n",
"print(\"Grid spacing:\", spacing)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These commands are made somewhat un-Pythonic by the generic design of the BMI."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Through the model's BMI, zero out the initial temperature field, except for an impulse near the middle.\n",
"Note that *set_value* expects a one-dimensional array for input."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temperature = np.zeros(shape)\n",
"temperature[3, 4] = 100.0\n",
"x.set_value(\"plate_surface__temperature\", temperature)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check that the temperature field has been updated. Note that *get_value* expects a one-dimensional array to receive output."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temperature_flat = np.empty_like(temperature).flatten()\n",
"x.get_value(\"plate_surface__temperature\", temperature_flat)\n",
"print(temperature_flat.reshape(shape))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now advance the model by a single time step:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x.update()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"View the new state of the temperature field:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x.get_value(\"plate_surface__temperature\", temperature_flat)\n",
"print(temperature_flat.reshape(shape))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's diffusion!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Advance the model to some distant time:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"distant_time = 2.0\n",
"while x.get_current_time() < distant_time:\n",
" x.update()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"View the final state of the temperature field:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.set_printoptions(formatter={\"float\": \"{: 5.1f}\".format})\n",
"x.get_value(\"plate_surface__temperature\", temperature_flat)\n",
"print(temperature_flat.reshape(shape))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that temperature isn't conserved on the plate:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(temperature_flat.sum())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"End the model:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x.finalize()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading