.. _simcontrol-py: ############# 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''' ... comp_unit ... ... time_step ... year ... month ... day ... iter ... ... ... --03-15 ... --07-15 ... ... ... all ... ... Chain1 ... Chain2 ... ... ... Chain3 ... ... ... Chain4 ... Chain5 ... ... ... Chain6 ... ... ... ... comp_unit:DEV_CLASS eq 5.0 or comp_unit:V gt 300 ... ... ... ... DIAM_CLASS_WIDTH ... simulation ... 1 ... ... ... passed_thinning_limit ... comp_unit ... 0 ... ... ... passed_regen_limit ... comp_unit ... 0 ... ... ... ... ... comp_unit ... ... AREA ... DEVEL_CLASS ... MAIN_GROUP ... ... ... ... stratum ... ... SP ... ... ... ... tree ... ... * ... ... ... ... ''' >>> 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 # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE [('data', (1, 1, True)), ('value', 5.0), ('eq', >> 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'] # doctest: +ELLIPSIS [] >>> span.chain_collections['init'] # doctest: +ELLIPSIS [, ] >>> 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 # doctest: +NORMALIZE_WHITESPACE {'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 # doctest: +NORMALIZE_WHITESPACE 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 []