BuildingMOTIF is a Python SDK for creating, manipulating, and validating semantic building models. Here, we will demonstrate how to create a simple ASHRAE 223P model using BuildingMOTIF.
We will create a model that includes a simple HVAC system with a VAV terminal unit serving a zone. This will use BuildingMOTIF Templates, which are reusable components that encapsulate common patterns in building modeling.
Setting up BuildingMOTIF¶
Downloading the NREL 223P templates library
!git clone --filter=blob:none --no-checkout https://github.com/NREL/BuildingMOTIF
!pushd BuildingMOTIF && git sparse-checkout init --cone
!pushd BuildingMOTIF && git sparse-checkout set libraries/ashrae/223p/nrel-templates
!pushd BuildingMOTIF && git checkoutThe following code sets up a temporary (in-memory) BuildingMOTIF instance, loads the necessary libraries, and creates an (empty) model to hold our building data.
from rdflib import Namespace
from buildingmotif import BuildingMOTIF
from buildingmotif.dataclasses import Library, Model
from buildingmotif.model_builder import TemplateBuilderContext as ModelBuilder
import logging
# Create a BuildingMOTIF object. If you do not have Java installed, remove the "shacl_engine" parameter
bm = BuildingMOTIF('sqlite://', shacl_engine='topquadrant', log_level=logging.ERROR)
# load 223P library and some dependencies. We will load a recent copy from the open223.info
s223 = Library.load(ontology_graph="https://open223.info/223p.ttl")
unit = Library.load(ontology_graph="http://qudt.org/3.1.1/vocab/unit")
quantitykind = Library.load(ontology_graph="http://qudt.org/3.1.1/vocab/quantitykind")
templates = Library.load(directory="BuildingMOTIF/libraries/ashrae/223p/nrel-templates")
# create a Model to hold our building model
model = Model.create("urn:example")
BLDG = Namespace("urn:example/")/home/runner/work/docs.open223.info/docs.open223.info/.venv/lib/python3.12/site-packages/pyshacl/extras/__init__.py:46: Warning: Extra "js" is not satisfied because requirement pyduktape2 is not installed.
warn(Warning(f"Extra \"{extra_name}\" is not satisfied because requirement {req} is not installed."))
Building the Model¶
Now, we load the templates into a ModelBuilder context, which allows us to use the templates to create our model.
builder = ModelBuilder(BLDG)
builder.add_templates_from_library(templates)We will create a reheat VAV terminal unit serving a physical space
# Create a VAV terminal unit with reheat
vav = builder["vav-reheat"](name="my_vav")
# we can give names to the sensors inside the VAV
vav["sup-air-temp-sensor"] = "BLDG_VAV:SAT"
vav["sup-air-flow-sensor"] = "BLDG_VAV:SAF"
# create the physical space
zone = builder["hvac-space"](name="my_zone")
# connect the VAV terminal unit to the zone using a duct
duct2zone = builder["duct"](a=vav['air-out'], b=zone['in'], name="duct2zone")When we are done, compile the “builder” into the model
model.add_graph(builder.compile())
print(f"Model has {len(model.graph)} triples")Model has 191 triples
/home/runner/work/docs.open223.info/docs.open223.info/.venv/lib/python3.12/site-packages/buildingmotif/dataclasses/template.py:483: UserWarning: Parameters "dmp-in, rhc-air-out-mapsto, rhc-valve-feedback, rhc-air-out, rhc-valve-out, rhc-valve-in-mapsto, c0, dmp-in-mapsto, rhc-valve-command, rhc-supply-water-temp, rhc-water-out-mapsto, sup-air-flow, air-in, air-in-mapsto, rhc-valve-in, rhc-air-in, rhc-supply-water-temp-sensor, dmp-out, rhc-return-water-temp-sensor, rhc-water-in-mapsto, rhc-valve-out-mapsto, rhc, air-out-mapsto, sup-air-pressure, rhc-water-in, rhc-valve, rhc-water-out, dmp, sup-air-pressure-sensor, dmp-feedback, dmp-out-mapsto, sup-air-temp, rhc-air-in-mapsto, rhc-return-water-temp, dmp-command" were not provided during evaluation
warnings.warn(
/home/runner/work/docs.open223.info/docs.open223.info/.venv/lib/python3.12/site-packages/buildingmotif/dataclasses/template.py:483: UserWarning: Parameters "in2, in-mapsto, out, temp-sensor, in4-mapsto, out-mapsto, exhaust-air-flow, out2-mapsto, in4, relative-humidity, out2, physical-space, humidity-sensor, temp, in2-mapsto, supply-air-flow, exh-flow-sensor, sup-flow-sensor, in3, in3-mapsto" were not provided during evaluation
warnings.warn(
The model is now ready and contains the VAV terminal unit and the physical space it serves:
223P model generated from templates
print(model.graph.serialize())Compiling the Model¶
The generated model is the “pre-inference” model, as described in Model Inference. To apply inference rules to this model, we will need to load the 223P ontology and apply the inference rules as described in that document.
compiled_model = model.compile([s223.get_shape_collection(), unit.get_shape_collection(), quantitykind.get_shape_collection()])
print(f"Compiled model has {len(compiled_model.graph)} triples")Compiled model has 103675 triples
223P model with all inferred triples
print(compiled_model.graph.serialize())