Numerical models are defined by subclassing the NumericalModel class:
class AreaCalculator(NumericalModel):
name = "AreaCalculator"
label = "Area Calculator"
figure = ModelFigure(src="img/Calculator.png", height=150, width=250)
description = ModelDescription("A calculator of a rectangle's area", show = True)
showOnHome = True
############# Inputs ###############
# Fields
width = Quantity('Length')
length = Quantity('Length')
geometryIn = FieldGroup([width, length], label = "Geometry")
inputs = SuperGroup([geometryIn], label = "Inputs")
# Actions
computeAction = ServerAction("compute", label = "Compute", outputView = 'resultView')
inputActionBar = ActionBar([computeAction], save = True)
# Model view
inputView = ModelView(ioType = "input", superGroups = [inputs],
actionBar = inputActionBar, autoFetch = True)
############# Results ###############
# Fields
area = Quantity('Area')
geometryOut = FieldGroup([area], label = "Geometry")
results = SuperGroup([geometryOut], label = "Results")
# Model view
resultView = ModelView(ioType = "output", superGroups = [results])
############# Page structure ########
modelBlocks = [inputView, resultView]
############# Methods ################
def compute(self):
self.area = self.width * self.length
The AreaCalculator class defines 2 input fields (width and length) and one output field (area). The input fields are grouped in a field-group with label Geometry, which is part of a super-group with label Inputs. The output field area is part of a field-group again with label Geometry (but a different one), which is part of the super-group Results. The model is visualized with an input view, comprised of the Inputs super-group and a bar of buttons for carrying out actions (Compute to perform the calculation via the compute method and Save to save the inputs), and a result view, consisting of the Results super-group.
The numerical model may have a field named figure, an instance of ModelFigure, which represents the figure displayed with the model, as well as a field named description, an instance of ModelDescription, providing a description that can be displayed at the model page and as a tooltip of the model’s thumbnal on the home page. The resulting user interface in the browser can be seen in the following figure:
Metaclass facilitating the creation of a numerical model class. Collects all declared fields, submodels, basic groups, supergroups and model views in the class in respective dictionaries
Abstract base class for numerical models.
Constructor for all numerical models. Sets default values for all model fields
Creates JSON representation of the modelView including field definitions, field values and actions
Abstract base class for all the field types.
Parameters: |
|
---|
Parameters: | value – value to parse |
---|
Checks if the value is of valid type for this field type, and, if not, attempts to convert it into one. For example if the Field is of type Quantity(‘Length’) then parseValue((2, ‘mm’)) will return 2e-3 (in the base SI unit ‘m’) which can be assigned to the field. Used implicitly by the smo.model.model.NumericalModel.__setattr__ method
All the fields also contain a private _name attribute, which is the name used to declare the field. This attribute is crated in the constructor of NumericalModelMeta
Bases: smo.model.fields.Field
Represents a physical quantity (e.g. Length, Time, Mass etc.). Allows values to be set using units e.g. (2, ‘km’)
Parameters: |
|
---|
Bases: smo.model.fields.Field
Represents a string field
Parameters: |
|
---|
Bases: smo.model.fields.Field
Represents a boolean value (True or False)
Parameters: | default (bool) – default value |
---|
Bases: smo.model.fields.Field
Allows the user to make a choice from a list of options
Parameters: |
|
---|
Bases: smo.model.fields.Field
Composite input field for representing a structured table (array of records)
Parameters: |
|
---|
Example:
compositePipe = RecordArray(
(
('name', String(maxLength = 20)),
('length', Quantity('Length')),
('diameter', Quantity('Length')),
), label='composite pipe'
)
Bases: smo.model.fields.Field
Object reference
Bases: smo.model.fields.Field
Field specifying HDF storage. Its value is dataset name
Parameters: |
|
---|
Bases: smo.model.fields.Field
Composite output field for representing a table or plot
Parameters: | structTuple – tuple defining the structure of the view data. It consists of (name, type) pairs, where name is the column name, and type is one of the basic field types (Quantity, String, Boolean etc.) |
---|
Parameters: |
|
---|
Bases: smo.model.fields.DataSeriesView
Field for visualization of table data
Parameters: | structTuple – tuple defining the structure of the view data. It consists of (name, type) pairs, where name is the column name, and type is the Quantity field type |
---|
Parameters: | options (dict) – additional options to be passed |
---|
Bases: smo.model.fields.DataSeriesView
Field for creating interactive plots
Parameters: | structTuple – tuple defining the structure of the view data. It consists of (name, type) pairs, where name is the column name, and type is the Quantity field type |
---|
Parameters: |
|
---|
Bases: smo.model.fields.Field
Field for displaying an image
Parameters: |
|
---|
Bases: smo.model.fields.Field
Field for displaying a matplotlib plot
Parameters: |
|
---|
Bases: smo.model.fields.Field
Used to create ports for linking to other components
Bases: smo.model.fields.Field
Used to include field-group or supergroup from a sub-model
Parameters: |
|
---|
Represents a figure displayed with the numerical model, which also serves as thumbnail for the model
Parameters: |
|
---|
A class field defines the structure of the field it refers to as part of the hierarchical structure of the model. It is included in the definitions property of the JSON object representing the model that is sent to the client. By contrast, an instance field represents the value of the particular field, which is contained in the values attribute of the JSON object.
Bases: smo.model.fields.Group
Abstract class for group of fields
Bases: smo.model.fields.BasicGroup
Represents a group of fields of all basic types except for PlotView and TableView
Bases: smo.model.fields.BasicGroup
Represents a group of fields of type PlotView and/or TableView
Bases: smo.model.fields.Group
Represents a group of FieldGroup and/or ViewGroup groups
Represents a view of the numerical model, comprised of super-groups and a bar of buttons for performing actions.
Parameters: |
|
---|