Table Of Contents

Previous topic

outputconstraint.py

Next topic

lexicontranslationtable.py

This Page

simcontrol.py

class SimControlDef(XmlObject):

def __init__(self, typedef):

Initializes the simulaiton control by processing the schema and xml documents:

>>> from simo.builder.simulation.simcontrol import SimControlDef
>>> from simo.builder import names
>>> from minimock import Mock
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/simulation.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<simulation
...         xmlns="http://www.simo-project.org/simo"
...         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
...         xsi:schemaLocation="http://www.simo-project.org/simo
...         ../schemas/simulation.xsd" desc="first test in SIMO">
...         <main_level>comp_unit</main_level>
...         <built_in_attributes>
...                 <time_step>time_step</time_step>
...                 <year>year</year>
...                 <month>month</month>
...                 <day>day</day>
...                 <iteration>iter</iteration>
...         </built_in_attributes>
...         <control>
...         <growth_season_start_date>--03-15</growth_season_start_date>
...         <growth_season_end_date>--07-15</growth_season_end_date>
...                 <span>
...                         <time>
...                                 <time_step>1</time_step>
...                                 <operation_step>1</operation_step>
...                                 <time_unit>year</time_unit>
...                                 <steps>10</steps>
...                         </time>
...                         <save_steps_to_db>all</save_steps_to_db>
...                         <init_chains>
...                                 <chain>Chain1</chain>
...                                 <chain>Chain2</chain>
...                         </init_chains>
...                         <simulation_chains>
...                                 <chain>Chain3</chain>
...                         </simulation_chains>
...                         <operation_chains>
...                                 <chain>Chain4</chain>
...                                 <chain>Chain5</chain>
...                         </operation_chains>
...                         <forced_operation_chains>
...                                 <chain>Chain6</chain>
...                         </forced_operation_chains>
...                 </span>
...         </control>
...     <stop_logic>comp_unit:DEV_CLASS eq 5.0 or comp_unit:V gt 300
...     </stop_logic>
...         <init_variables>
...                 <variable>
...                         <name>DIAM_CLASS_WIDTH</name>
...                         <level>simulation</level>
...                         <value>1</value>
...                 </variable>
...                 <variable>
...                         <name>passed_thinning_limit</name>
...                         <level>comp_unit</level>
...                         <value>0</value>
...                 </variable>
...                 <variable>
...                         <name>passed_regen_limit</name>
...                         <level>comp_unit</level>
...                         <value>0</value>
...                 </variable>
...         </init_variables>
...         <output_constraints active="on">
...                 <level>
...                         <name>comp_unit</name>
...                         <variables>
...                                 <variable>AREA</variable>
...                                 <variable>DEVEL_CLASS</variable>
...                                 <variable>MAIN_GROUP</variable>
...                         </variables>
...                 </level>
...                 <level>
...                         <name>stratum</name>
...                         <variables>
...                                 <variable>SP</variable>
...                         </variables>
...                 </level>
...         <level>
...             <name>tree</name>
...             <variables>
...                 <variable>*</variable>
...             </variables>
...         </level>
...         </output_constraints>
... </simulation>'''
>>> class Lexicon(object):
...     def get_level_ind(self, level):
...         if level=='comp_unit':
...             return 1
...         elif level=='simulation':
...             return 0
...         elif level=='stratum':
...             return 2
...         else:
...             return 3
...     def get_variable_ind(self, level, var, check_active=False):
...         if var=='some_exotic_variable':
...             return (None, None)
...         else:
...             return (1, 1)
>>> mcc1 = Mock('ModelChainCollection')
>>> mcc1.depth = 10
>>> mcc1.branching_groups = None
>>> mcc1.opres_cfiers = set([])
>>> mcc1.opres_vars = set([])
>>> mcc1.cf_cfiers = set([])
>>> mcc1.memory_models = set([])
>>> mcc1.name = 'test-chain-1'
>>> mcc2 = Mock('ModelChainCollection')
>>> mcc2.depth = 15
>>> mcc2.branching_groups = {'bg1':[{'bt1':['cond1', 'cond2']}, True],
...                          'bg2':[{'bt2':['cname', 'cother']}, False]}
>>> mcc2.opres_cfiers = set(['SP'])
>>> mcc2.opres_vars = set(['Volume'])
>>> mcc2.cf_cfiers = set(['SP'])
>>> mcc2.memory_models = set(['mock model 1', 'mock model 5'])
>>> mcc2.name = 'test-chain-2'
>>> mcc3 = Mock('ModelChainCollection')
>>> mcc3.depth = 5
>>> mcc3.branching_groups = {'bg2':[{'bt1':['cond1', 'cond2']}, True],
...                          'bg4':[{'bt2':['cname', 'cother']}, True]}
>>> mcc3.opres_cfiers = set(['SP'])
>>> mcc3.opres_vars = set(['Volume', 'Income'])
>>> mcc3.cf_cfiers = set(['SP'])
>>> mcc3.memory_models = set(['mock model 2'])
>>> mcc3.name = 'test-chain-3'
>>> mcc4 = Mock('ModelChainCollection')
>>> mcc4.depth = 10
>>> mcc4.branching_groups = None
>>> mcc4.opres_cfiers = set(['SP', 'assortment'])
>>> mcc4.opres_vars = set(['Volume', 'Income'])
>>> mcc4.cf_cfiers = set(['SP', 'assortment'])
>>> mcc4.memory_models = set(['mock model 2', 'mock model 3'])
>>> mcc4.name = 'test-chain-4'
>>> mccs = {names.INIT: {'Chain1': mcc1, 'Chain2': mcc1},
...           names.SIMULATION: {'Chain3': mcc1},
...           names.OPERATION: {'Chain4': mcc2, 'Chain5': mcc3},
...           names.FORCED_OPERATION: {'Chain6': mcc4}}
>>> scd = SimControlDef(typedef)
>>> scd.schema = schema
>>> try:
...     scd.xml = ('testxml', xml, Lexicon(), mccs)
... except ValueError, e:
...     pass

def xml_to_obj(self, root, lexicon, simodb):

NB! output constraints have no effect in the simulation in the current implementation; i.e., all data variables are written to the result database

TODO: remove output constraints from simcontrol, if the current implementation stays:

>>> sc = scd.obj['testxml']
>>> sc.main_level
1
>>> sc.built_ins
{'time_step': (1, 1), 'month': (1, 1), 'iteration': (1, 1), 'day': (1, 1), 'year': (1, 1)}
>>> sc.growth_season_end_month
7
>>> sc.growth_season_end_day
15
>>> sc.stop_logic 
[('data', (1, 1, True)), ('value', 5.0), ('eq', <function eee...),
 ('data', (1, 1, True)), ('value', 300.0), ('eq', <function gte...),
 ('group', <function or_...)]
>>> span = sc.time_spans[0]
>>> span.branching_groups
{'bg1': [{'bt1': ['cond1', 'cond2']}, True], 'bg2': [{'bt2': ['cname', 'cother']}, False], 'bg4': [{'bt2': ['cname', 'cother']}, True]}
>>> span.chain_collections['forced_operation'] 
[<Mock ... ModelChainCollection>]
>>> span.chain_collections['init'] 
[<Mock ... ModelChainCollection>, <Mock ... ModelChainCollection>]
>>> names.SIMULATION in span.chain_collections
True
>>> names.OPERATION in span.chain_collections
True
>>> span.ending.target
10
>>> span.ending.type
'steps'
>>> span.forced_operations
True
>>> span.operation_step
1
>>> span.time_step
1
>>> span.unit
'year'
>>> span.save_steps_to_db
'all'
>>> sc.forced_operations
True
>>> sc.max_model_chain_depth
15
>>> sc.all_branching_groups 
{'bg1': [{'bt1': ['cond1', 'cond2']}, True],
 'bg2': [{'bt1': ['cond1', 'cond2']}, True],
 'bg4': [{'bt2': ['cname', 'cother']}, True]}
>>> mm = list(sc.memory_models)
>>> mm.sort()
>>> mm
['mock model 1', 'mock model 2', 'mock model 3', 'mock model 5']
>>> 'assortment' in sc.opres_cfiers
True
>>> 'SP' in sc.opres_cfiers
True
>>> 'SP' in sc.cf_cfiers
True
>>> sc.opres_vars
set(['Volume', 'Income'])
>>> sc.init_variables
[((1, 1), 1.0), ((1, 1), 0.0), ((1, 1), 0.0)]
>>> sc.output_constraints[1]
[1, 1, 1]
>>> sc.output_constraints[2]
[1]
>>> sc.output_constraints[3] is None
True
>>> scd.errors 
set(["Duplicate branching group name 'bg2' in operation model chains
'test-chain-2' and 'test-chain-3' (1. timespan) for Simulation control
'testxml'"])
>>> scd.warnings
[]