GCBM
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CBMDisturbanceListener Class Reference

#include <cbmdisturbancelistener.h>

Inheritance diagram for CBMDisturbanceListener:
Inheritance graph
Collaboration diagram for CBMDisturbanceListener:
Collaboration graph

Public Member Functions

 CBMDisturbanceListener ()
 
virtual ~CBMDisturbanceListener ()=default
 
void configure (const DynamicObject &config) override
 
void subscribe (NotificationCenter &notificationCenter) override
 
flint::ModuleTypes moduleType ()
 
virtual void doLocalDomainInit () override
 
virtual void doSystemShutdown () override
 
virtual void doTimingInit () override
 
virtual void doTimingStep () override
 
virtual void doDisturbanceEvent (DynamicVar) override
 
- Public Member Functions inherited from CBMModuleBase
virtual ~CBMModuleBase ()=default
 
void onSystemInit () override
 
void onSystemShutdown () override
 
void onLocalDomainInit () override
 
void onLocalDomainShutdown () override
 
void onLocalDomainProcessingUnitInit () override
 
void onLocalDomainProcessingUnitShutdown () override
 
void onPreTimingSequence () override
 
void onTimingInit () override
 
void onTimingPrePostInit () override
 
void onTimingPostInit () override
 
void onTimingPostInit2 () override
 
void onTimingShutdown () override
 
void onTimingStep () override
 
void onTimingPreEndStep () override
 
void onTimingEndStep () override
 
void onTimingPostStep () override
 
void onOutputStep () override
 
void onPrePostDisturbanceEvent () override
 
void onPostDisturbanceEvent () override
 
void onPostDisturbanceEvent2 () override
 
void onError (std::string msg) override
 
void onDisturbanceEvent (DynamicVar e) override
 
void onPostNotification (short preMessageSignal) override
 
virtual void doSystemInit ()
 
virtual void doLocalDomainShutdown ()
 
virtual void doLocalDomainProcessingUnitInit ()
 
virtual void doLocalDomainProcessingUnitShutdown ()
 
virtual void doPreTimingSequence ()
 
virtual void doTimingPrePostInit ()
 
virtual void doTimingPostInit ()
 
virtual void doTimingPostInit2 ()
 
virtual void doTimingShutdown ()
 
virtual void doTimingPreEndStep ()
 
virtual void doTimingEndStep ()
 
virtual void doTimingPostStep ()
 
virtual void doOutputStep ()
 
virtual void doError (std::string msg)
 
virtual void doPrePostDisturbanceEvent ()
 
virtual void doPostDisturbanceEvent ()
 
virtual void doPostDisturbanceEvent2 ()
 
virtual void doPostNotification (short preMessageSignal)
 

Private Types

typedef std::vector< CBMDistEventTransferEventVector
 
typedef std::unordered_map< int, EventVectorEventMap
 

Private Member Functions

void fetchMatrices ()
 
void fetchDMAssociations ()
 
void fetchLandClassTransitions ()
 
void fetchDistTypeCodes ()
 
void fetchPeatlandDMAssociations ()
 
void fetchDisturbanceOrder ()
 
std::shared_ptr< IDisturbanceSubConditioncreateSubCondition (const DynamicObject &config)
 
std::string getDisturbanceTypeName (const DynamicObject &eventData)
 
bool addLandUnitEvent (const DynamicVar &eventData)
 
void fireCBMDisturbanceEvent (CBMDistEventRef &e)
 
void firePeatlandDisturbanceEvent (CBMDistEventRef &e)
 

Private Attributes

NotificationCenter * _notificationCenter
 
std::vector< std::string > _layerNames
 
std::vector< const flint::IVariable * > _layers
 
flint::IVariable * _landClass
 
flint::IVariable * _spu
 
flint::IVariable * _classifierSet
 
flint::IVariable * _age
 
EventMap _matrices
 
std::unordered_map< std::pair< int, std::string >, std::pair< int, int > > _peatlandDmAssociations
 
std::unordered_map< std::pair< std::string, int >, int > _dmAssociations
 
std::unordered_map< std::string, std::string > _landClassTransitions
 
std::map< int, std::vector< CBMDistEventRef > > _landUnitEvents
 
std::unordered_map< std::string, int > _distTypeCodes
 
std::unordered_map< int, std::string > _distTypeNames
 
std::unordered_set< std::string > _errorLayers
 
std::unordered_set< std::string > _classifierNames
 
std::unordered_map< std::string, int > _disturbanceOrder
 
bool _disturbanceConditionsInitialized = false
 
DynamicVar _conditionConfig
 
std::vector< DisturbanceCondition_disturbanceConditions
 
std::shared_ptr< std::deque< DisturbanceHistoryRecord > > _disturbanceHistory
 

Member Typedef Documentation

◆ EventMap

typedef std::unordered_map<int, EventVector> EventMap
private

◆ EventVector

typedef std::vector<CBMDistEventTransfer> EventVector
private

Constructor & Destructor Documentation

◆ CBMDisturbanceListener()

◆ ~CBMDisturbanceListener()

virtual ~CBMDisturbanceListener ( )
virtualdefault

Member Function Documentation

◆ addLandUnitEvent()

bool addLandUnitEvent ( const DynamicVar &  eventData)
private

If parameter eventData is not a structure, return
Get the disturbanceType using CBMDisturbanceListener.getDisturbanceTypeName(), year and transition Id from parameter eventData,
Extract DynamicObject from parameter eventData and store it in a variable event
If event has "conditions" and it is not empty, create a variable to store all the disturbance conditions
for every condition, assign variable varName to condition[0], target to condition[2], targetType to condition[1] that can either take values DisturbanceConditionType::LessThan, DisturbanceConditionType::AtLeast or DisturbanceConditionType::EqualTo
If varName is not found in CBMDisturbanceListener._classifierNames, instantiate an object of VariableDisturbanceSubCondition with CBMDisturbanceListener._classifierSet, targetType, target, varName,
else instantiate it with variable varName in _landUnitData, targetType, target and append it to conditions
Corresponding to the current year, instantiate an object of CBMDistEventRef with disturbanceType, year, transitionId, conditions, event and append it to CBMDisturbanceListener._landUnitEvents

Parameters
eventDataDynamicVar&
Returns
bool

◆ configure()

void configure ( const DynamicObject &  config)
override

Configuration function.

If parameter config has variable "vars", and it is not empty,
add each layer in config["vars"] to CBMDisturbanceListener._layerNames
If parameter config has, variable, "conditions", assign the value to CBMDisturbanceListener._conditionConfig
else assign the result of DynamicVar()

Parameters
configDynamicObject&
Returns
void

◆ createSubCondition()

std::shared_ptr< IDisturbanceSubCondition > createSubCondition ( const DynamicObject &  config)
private

Determine the various subconditions based on the configuration

An shared pointer of type IDisturbanceSubCondition, subConditions, is created to store all the subConditions
For each condition in parameter config, if the first element of the condition, (condition.first), is either "disturbance_type", "run_conditions", "override_conditions" or "override_disturbance_type", the next configuration object is checked
If condition.first is "disturbance_sequence", all the historical disturbances are added to an object of DisturbanceHistoryCondition. A shared pointer of DisturbanceSequenceSubCondition containing the timing, disturbance history and sequence of disturbance events is added to subConditions and the next condition is visited
Extract the comparison type (<, =, >=, <->, !=) and target of value of condition.second
If the condition is a single variable, the condition.first is present in _landUnitData, a shared pointer of VariableDisturbanceSubCondition is added to subConditions and the next condition is visited
Based on whether the condition is on one or more pools, a shared pointer of PoolDisturbanceSubCondition is added to subConditions and the next condition is visited
An object of CompositeDisturbanceSubCondition with the subConditions is returned

Parameters
configDynamicObject&
Returns
shared_ptr<IDisturbanceSubCondition>

◆ doDisturbanceEvent()

void doDisturbanceEvent ( DynamicVar  n)
overridevirtual

Extract the current year from _landUnitData, disturbance type from "disturbance" in parameter n, and add to the beginning of CBMDisturbanceListener._disturbanceHistory, object of DisturbanceHistoryRecord

Parameters
nDynamicVar
Returns
void

Reimplemented from CBMModuleBase.

◆ doLocalDomainInit()

void doLocalDomainInit ( )
overridevirtual

◆ doSystemShutdown()

void doSystemShutdown ( )
overridevirtual

For each layerName in CBMDisturbanceListener._errorLayers, report that the disturbance layer is not in the expected format

Returns
void

Reimplemented from CBMModuleBase.

◆ doTimingInit()

void doTimingInit ( )
overridevirtual

Clear CBMDisturbanceListener._disturbanceHistory
If CBMDisturbanceListener._classifierNames is empty, append classifier set to CBMDisturbanceListener._classifierNames

If CBMDisturbanceListener._disturbanceConditionsInitialized and CBMDisturbanceListener._conditionConfig is not empty
add disturbance type to a variable matchDisturbanceTypes. Instantiate an object of class DisturbanceCondition and append
it to CBMDisturbanceListener._disturbanceConditions

For each event in CBMDisturbanceListener._layers if CBMDisturbanceListener.addLandUnitEvent with parameter event is false, then
the layer is added to CBMDisturbanceListener._errorLayers

Perform a stable_sort on each event year in CBMDisturbanceListener._landUnitEvents

Returns
void

Reimplemented from CBMModuleBase.

◆ doTimingStep()

void doTimingStep ( )
overridevirtual

For each event in CBMDisturbanceListener._landUnitEvents of the current year, invoke CBMDisturbanceListener.checkConditions()
If it is true, then for each condition in CBMDisturbanceListener._disturbanceConditions, if it is applicable,
it is assigned to variable result of DisturbanceConditionResult
Apply the first matching condition from each category (run/override), conditions are prioritized in the order they're configured.

If it is running on Peatland, fire CBMDisturbanceListener.firePeatlandDisturbanceEvent, else CBMDisturbanceListener.fireCBMDisturbanceEvent

Returns
void

Reimplemented from CBMModuleBase.

◆ fetchDistTypeCodes()

void fetchDistTypeCodes ( )
private

Get disturbance type and disturbance type codes

If _landUnitData has the variable "disturbance_type_codes", for each code in variable "disturbance_type_codes", populate CBMDisturbanceListener._distTypeCodes mapping the "disturbance_type" to the "disturbance_type_code" and CBMDisturbanceListener.__distTypeNames mapping "disturbance_type_code" to the "disturbance_type"

Returns
void

◆ fetchDisturbanceOrder()

void fetchDisturbanceOrder ( )
private

If _landUnitData has variable "user_disturbance_order", add all the user disturbances in serial order (starting from 1) to CBMDisturbanceListener._disturbanceOrder
If _landUnitData has variable "default_disturbance_order", add all those default disturbances that are not user disturbances in serial order (starting from 1) to CBMDisturbanceListener._disturbanceOrder

Returns
void

◆ fetchDMAssociations()

void fetchDMAssociations ( )
private

For each dmAssociation in variable "disturbance_matrix_associations" of _landUnitData, insert the "disturbance_type", "spatial_unit_id" and "disturbance_matrix_id" as a pair of pairs into CBMDisturbanceListener._dmAssociations

Returns
void

◆ fetchLandClassTransitions()

void fetchLandClassTransitions ( )
private

For each tranistion in variable "land_class_transitions" of _landUnitData, insert the "disturbance_type" and "land_class_transition" as a pair into CBMDisturbanceListener._landClassTransitions

Returns
void

◆ fetchMatrices()

void fetchMatrices ( )
private

Fetch disturbance matrices

For each row in variable "disturbance_matrices" of _landUnitData, instantiate an object transfer of CBMDisturbanceListener with *_landUnitData, row and obtain the disturbance id
If the disturbance id is not present in CBMDisturbanceListener._matrices add the disturbance matrix id, and a vector containing all the transfers encountered to CBMDisturbanceListener._matrices

Returns
void

◆ fetchPeatlandDMAssociations()

void fetchPeatlandDMAssociations ( )
private

If _landUnitData has variable "peatland_dm_associations", insert each dmAssociation in variable "peatland_dm_associations" with attributes "peatland_id", "disturbance_type", "peatland_dm_id" and "wtd_modifier_id" to CBMDisturbanceListener._peatlandDmAssociations

Returns
void

◆ fireCBMDisturbanceEvent()

void fireCBMDisturbanceEvent ( CBMDistEventRef e)
private

Fire a CBM disturbance event.

If CBMDisturbanceListener._dmAssociations does not contain the key <disturbance type - from parameter e, spatial unit - value of CBMDisturbanceListener._spu >, return
If the disturbance type of parameter e transitions to a new land class, set CBMDisturbanceListener._landClass to the landClassTransition
Find the disturbance type code corresponding to the disturbance type of parameter e, else set it to 1
Add all transfers from CBMDisturbanceListener._matrices for the current disturbance id
Prepare the disturbance data object with attributes "disturbance" - e.disturbanceType() ,
"disturbance_type_code", "transfers" - disturbance matrix, "transition" - e.transitionRuleId()
Merge any additional metadata into disturbance data and fire the disturbance events

Parameters
eCBMDistEventRef&
Returns
void

◆ firePeatlandDisturbanceEvent()

void firePeatlandDisturbanceEvent ( CBMDistEventRef e)
private

Fire a Peatland disturbance event.

If the disturbance type of parameter e transitions to a new land class, set CBMDisturbanceListener._landClass to the landClassTransition
Find the disturbance type code corresponding to the disturbance type of parameter e, else set it to 1
If disturbance is applied in this peatland, prepare the disturbance data object with "disturbance" - e.disturbanceType(),
"disturbance_type_code", "transition" - e.transitionRuleId(), "transfers" is a vector of CBMDistEventTransfer, which will be injected by peatland disturbance module
Merge any additional metadata into disturbance data and fire the disturbance events

Parameters
eCBMDistEventRef&
Returns
void

◆ getDisturbanceTypeName()

std::string getDisturbanceTypeName ( const DynamicObject &  eventData)
private

Return disturbance type name

If the parameter eventData contains the variables "disturbance_type", "disturbance_id" check if CBMDisturbanceListener._distTypeNames has eventData["disturbance_type_id"] and the value of disturbance id corresponds to disturbance name
Else if, parameter eventData contains only "disturbance_id", check if check if CBMDisturbanceListener._distTypeNames has eventData["disturbance_type_id"]
Else report the absence of a disturbance name and id

Parameters
eventDataDynamicObject
Returns
string

◆ moduleType()

flint::ModuleTypes moduleType ( )

◆ subscribe()

void subscribe ( NotificationCenter &  notificationCenter)
override

Subscribe to the signals LocalDomainInit, SystemShutdown, TimingInit, DisturbanceEvent, and TimingStep.

Parameters
notificationCenterNotificationCenter&
Returns
void

Member Data Documentation

◆ _age

flint::IVariable* _age
private

◆ _classifierNames

std::unordered_set<std::string> _classifierNames
private

◆ _classifierSet

flint::IVariable* _classifierSet
private

◆ _conditionConfig

DynamicVar _conditionConfig
private

◆ _distTypeCodes

std::unordered_map<std::string, int> _distTypeCodes
private

◆ _distTypeNames

std::unordered_map<int, std::string> _distTypeNames
private

◆ _disturbanceConditions

std::vector<DisturbanceCondition> _disturbanceConditions
private

◆ _disturbanceConditionsInitialized

bool _disturbanceConditionsInitialized = false
private

◆ _disturbanceHistory

std::shared_ptr<std::deque<DisturbanceHistoryRecord> > _disturbanceHistory
private

◆ _disturbanceOrder

std::unordered_map<std::string, int> _disturbanceOrder
private

◆ _dmAssociations

std::unordered_map<std::pair<std::string, int>, int> _dmAssociations
private

◆ _errorLayers

std::unordered_set<std::string> _errorLayers
private

◆ _landClass

flint::IVariable* _landClass
private

◆ _landClassTransitions

std::unordered_map<std::string, std::string> _landClassTransitions
private

◆ _landUnitEvents

std::map<int, std::vector<CBMDistEventRef> > _landUnitEvents
private

◆ _layerNames

std::vector<std::string> _layerNames
private

◆ _layers

std::vector<const flint::IVariable*> _layers
private

◆ _matrices

EventMap _matrices
private

◆ _notificationCenter

NotificationCenter* _notificationCenter
private

◆ _peatlandDmAssociations

std::unordered_map<std::pair<int, std::string>, std::pair<int, int> > _peatlandDmAssociations
private

◆ _spu

flint::IVariable* _spu
private

The documentation for this class was generated from the following files: