Composites¶
There are a set of objects in Sympl that wrap multiple components into a single
object so they can be called as if they were one component. There is one each
for TendencyComponent
, DiagnosticComponent
, and
Monitor
. These can be used to simplify code, so that
the way you call a list of components is the same as the way you would
call a single component. For example, instead of writing:
tendency_component_list = [
MyTendencyComponent(),
MyOtherTendencyComponent(),
YetAnotherTendencyComponent(),
]
all_diagnostics = {}
total_tendencies = {}
for tendency_component in tendency_component_list:
tendencies, diagnostics = tendency_component(state)
# this should actually check to make sure nothing is overwritten,
# but this code does not
total_tendencies.update(tendencies)
for name, value in tendencies.keys():
if name not in total_tendencies:
total_tendencies[name] = value
else:
total_tendencies[name] += value
for name, value in diagnostics.items():
all_diagnostics[name] = value
You could write:
tendency_component_composite = TendencyComponentComposite([
MyTendencyComponent(),
MyOtherTendencyComponent(),
YetAnotherTendencyComponent(),
])
tendencies, diagnostics = tendency_component_composite(state)
This second call is much cleaner. It will also automatically detect whether
multiple components are trying to write out the same diagnostic, and raise
an exception if that is the case (so no results are being silently
overwritten). You can get similar simplifications for
DiagnosticComponent
and Monitor
.
Note
TendencyComponentComposites are mainly useful inside of TimeSteppers, so if you’re only writing a model script it’s unlikely you’ll need them.
API Reference¶
-
class
sympl.
TendencyComponentComposite
(*args)[source]¶ -
__call__
(state)[source]¶ Gets tendencies and diagnostics from the passed model state.
Parameters: state (dict) – A model state dictionary.
Returns: - tendencies (dict) – A dictionary whose keys are strings indicating state quantities and values are the time derivative of those quantities in units/second at the time of the input state.
- diagnostics (dict) – A dictionary whose keys are strings indicating state quantities and values are the value of those quantities at the time of the input state.
Raises: KeyError
– If a required quantity is missing from the state.InvalidStateError
– If state is not a valid input for a TendencyComponent instance.
-
__init__
(*args)[source]¶ Parameters: *args – The components that should be wrapped by this object.
Raises: SharedKeyError
– If two components compute the same diagnostic quantity.InvalidPropertyDictError
– If two components require the same input or compute the same output quantity, and their dimensions or units are incompatible with one another.
-
array_call
(state)[source]¶ Gets tendencies and diagnostics from the passed model state.
Parameters: state (dict) – A model state dictionary. Instead of data arrays, should include numpy arrays that satisfy the input_properties of this object. Returns: - tendencies (dict) – A dictionary whose keys are strings indicating state quantities and values are the time derivative of those quantities in units/second at the time of the input state, as numpy arrays.
- diagnostics (dict) – A dictionary whose keys are strings indicating state quantities and values are the value of those quantities at the time of the input state, as numpy arrays.
-
component_class
¶ alias of
sympl._core.base_components.TendencyComponent
-
-
class
sympl.
DiagnosticComponentComposite
(*args)[source]¶ -
__call__
(state)[source]¶ Gets diagnostics from the passed model state.
Parameters: state (dict) – A model state dictionary.
Returns: diagnostics – A dictionary whose keys are strings indicating state quantities and values are the value of those quantities at the time of the input state.
Return type: dict
Raises: KeyError
– If a required quantity is missing from the state.InvalidStateError
– If state is not a valid input for a DiagnosticComponent instance.
-
array_call
(state)[source]¶ Gets diagnostics from the passed model state.
Parameters: state (dict) – A model state dictionary. Instead of data arrays, should include numpy arrays that satisfy the input_properties of this object. Returns: diagnostics – A dictionary whose keys are strings indicating state quantities and values are the value of those quantities at the time of the input state, as numpy arrays. Return type: dict
-
component_class
¶ alias of
sympl._core.base_components.DiagnosticComponent
-