def test_model_update_insert_component_on_conduit() -> None: macro = Component('macro', 'my.macro') micro = Component('micro', 'my.micro') components = [macro, micro] conduit1 = Conduit('macro.intermediate_state', 'micro.initial_state') conduit2 = Conduit('micro.final_state', 'macro.state_update') conduits = [conduit1, conduit2] base = Model('test_update', components, conduits) tee = Component('tee', 'muscle3.tee') conduit3 = Conduit('macro.intermediate_state', 'tee.input') conduit4 = Conduit('tee.output', 'micro.initial_state') overlay = Model('test_update_tee', [tee], [conduit3, conduit4]) base.update(overlay) assert len(base.components) == 3 assert macro in base.components assert micro in base.components assert tee in base.components assert len(base.conduits) == 3 assert conduit2 in base.conduits assert conduit3 in base.conduits assert conduit4 in base.conduits
def test_model_update_set_implementation() -> None: abstract_reaction = Component('reaction') base = Model('test_set_impl', [abstract_reaction]) assert base.components[0].name == 'reaction' assert base.components[0].implementation is None reaction_python = Component('reaction', 'reaction_python') overlay = Model('test_set_impl', [reaction_python]) base.update(overlay) assert len(base.components) == 1 assert base.components[0].name == 'reaction' assert base.components[0].implementation == 'reaction_python'
def test_configuration_update_model4() -> None: component1 = Component('macro', 'my.macro') model1 = Model('model1', [component1]) base = Configuration(model1) component2 = Component('micro', 'my.micro') model2 = Model('model2', [component2]) overlay = Configuration(model2) base.update(overlay) assert isinstance(base.model, Model) assert base.model == model1 assert base.model.name == 'model2' assert component1 in base.model.components assert component2 in base.model.components
def test_config5() -> Configuration: model = Model( 'test_model', [ Component('ic', 'isr2d.initial_conditions'), Component('smc', 'isr2d.smc'), Component('bf', 'isr2d.blood_flow'), Component('smc2bf', 'isr2d.smc2bf'), Component('bf2smc', 'isr2d.bf2smc')], [ Conduit('ic.out', 'smc.initial_state'), Conduit('smc.cell_positions', 'smc2bf.in'), Conduit('smc2bf.out', 'bf.initial_domain'), Conduit('bf.wss_out', 'bf2smc.in'), Conduit('bf2smc.out', 'smc.wss_in')]) implementations = [ Implementation( Reference('isr2d.initial_conditions'), 'isr2d/bin/ic'), Implementation( Reference('isr2d.smc'), 'isr2d/bin/smc'), Implementation( Reference('isr2d.blood_flow'), 'isr2d/bin/bf'), Implementation( Reference('isr2d.smc2bf'), 'isr2d/bin/smc2bf.py'), Implementation( Reference('isr2d.bf2smc'), 'isr2d/bin/bf2smc.py')] resources = [ Resources(Reference('isr2d.initial_conditions'), 4), Resources(Reference('isr2d.smc'), 4), Resources(Reference('isr2d.blood_flow'), 4), Resources(Reference('isr2d.smc2bf'), 1), Resources(Reference('isr2d.bf2smc'), 1)] return Configuration(model, None, implementations, resources)
def macro_micro() -> Model: macro = Component('macro', 'my.macro') micro = Component('micro', 'my.micro') components = [macro, micro] conduit1 = Conduit('macro.intermediate_state', 'micro.initial_state') conduit2 = Conduit('micro.final_state', 'macro.state_update') conduits = [conduit1, conduit2] return Model('macro_micro', components, conduits)
def test_model_update_add_component() -> None: macro = Component('macro', 'my.macro') base = Model('test_update', [macro]) micro = Component('micro', 'my.micro') conduit1 = Conduit('macro.intermediate_state', 'micro.initial_state') conduit2 = Conduit('micro.final_state', 'macro.state_update') conduits = [conduit1, conduit2] overlay = Model('test_update_add', [micro], conduits) base.update(overlay) assert len(base.components) == 2 assert macro in base.components assert micro in base.components assert conduit1 in base.conduits assert conduit2 in base.conduits
def test_configuration_update_model2() -> None: model_ref1 = ModelReference('model1') base = PartialConfiguration(model_ref1) component1 = Component('macro', 'my.macro') model2 = Model('model2', [component1]) overlay = PartialConfiguration(model2) base.update(overlay) assert base.model == model2
def test_model_update_replace_component() -> None: macro = Component('macro', 'my.macro') micro = Component('micro', 'my.micro') components = [macro, micro] conduit1 = Conduit('macro.intermediate_state', 'micro.initial_state') conduit2 = Conduit('micro.final_state', 'macro.state_update') conduits = [conduit1, conduit2] base = Model('test_update', components, conduits) surrogate = Component('micro', 'my.surrogate') overlay = Model('test_update_surrogate', [surrogate]) base.update(overlay) assert len(base.components) == 2 assert macro in base.components assert surrogate in base.components assert len(base.conduits) == 2 assert conduit1 in base.conduits assert conduit2 in base.conduits
def test_configuration_update_model3() -> None: component1 = Component('macro', 'my.macro') model1 = Model('model1', [component1]) base = Configuration(model1) model_ref2 = ModelReference('model2') overlay = PartialConfiguration(model_ref2) base.update(overlay) assert base.model == model1 assert model1.name == 'model2' assert model1.components == [component1]
def topology_store() -> TopologyStore: config = Configuration( Model( 'test_model', [ ComputeElement('macro', 'macro_implementation'), ComputeElement( 'micro', 'micro_implementation', [10, 10])], [ Conduit('macro.out', 'micro.in'), Conduit('micro.out', 'macro.in') ])) return TopologyStore(config)
def test_dump_model(dump_model: Callable) -> None: ce1 = Component('ce1', 'test.impl1') ce2 = Component('ce2', 'test.impl2') cd1 = Conduit('ce1.state_out', 'ce2.init_in') cd2 = Conduit('ce2.fini_out', 'ce1.boundary_in') model = Model('test_model', [ce1, ce2], [cd1, cd2]) text = dump_model(model) assert text == ('name: test_model\n' 'components:\n' ' ce1: test.impl1\n' ' ce2: test.impl2\n' 'conduits:\n' ' ce1.state_out: ce2.init_in\n' ' ce2.fini_out: ce1.boundary_in\n')
def test_config2() -> PartialConfiguration: model = Model( 'test_model', [ Component('ic', 'isr2d.initial_conditions'), Component('smc', 'isr2d.smc'), Component('bf', 'isr2d.blood_flow'), Component('smc2bf', 'isr2d.smc2bf'), Component('bf2smc', 'isr2d.bf2smc')], [ Conduit('ic.out', 'smc.initial_state'), Conduit('smc.cell_positions', 'smc2bf.in'), Conduit('smc2bf.out', 'bf.initial_domain'), Conduit('bf.wss_out', 'bf2smc.in'), Conduit('bf2smc.out', 'smc.wss_in')]) return PartialConfiguration(model)
def test_duplication_mapper(log_file_in_tmpdir): """A positive all-up test of duplication mappers. This is an acyclic workflow. """ elements = [ ComputeElement('dm', 'muscle.duplication_mapper'), ComputeElement('first', 'receiver'), ComputeElement('second', 'receiver') ] conduits = [Conduit('dm.out', 'first.in'), Conduit('dm.out2', 'second.in')] model = Model('test_model', elements, conduits) settings = Settings() configuration = Configuration(model, settings) implementations = { 'muscle.duplication_mapper': duplication_mapper, 'receiver': receiver } run_simulation(configuration, implementations)
def test_all(log_file_in_tmpdir): """A positive all-up test of everything. """ elements = [ ComputeElement('macro', 'macro_impl'), ComputeElement('micro', 'micro_impl', [10]) ] conduits = [ Conduit('macro.out', 'micro.in'), Conduit('micro.out', 'macro.in') ] model = Model('test_model', elements, conduits) settings = Settings( OrderedDict([('test1', 13), ('test2', 13.3), ('test3', 'testing'), ('test4', True), ('test5', [2.3, 5.6]), ('test6', [[1.0, 2.0], [3.0, 1.0]])])) configuration = Configuration(model, settings) implementations = {'macro_impl': macro, 'micro_impl': micro} run_simulation(configuration, implementations)
def test_settings_overlays(log_file_in_tmpdir): """A positive all-up test of settings overlays. """ elements = [ ComputeElement('qmc', 'qmc'), ComputeElement('macro', 'macro', [10]), ComputeElement('relay', 'explicit_relay'), ComputeElement('relay2', 'explicit_relay'), ComputeElement('micro', 'micro', [10])] conduits = [ Conduit('qmc.settings_out', 'macro.muscle_settings_in'), Conduit('macro.out', 'relay.in'), Conduit('relay.out', 'micro.in'), Conduit('micro.out', 'relay2.in'), Conduit('relay2.out', 'macro.in')] model = Model('test_model', elements, conduits) settings = Settings(OrderedDict([ ('test1', 13), ('test2', 13.3), ('test3', 'testing'), ('test4', True), ('test5', [2.3, 5.6]), ('test6', [[1.0, 2.0], [3.0, 1.0]])])) configuration = Configuration(model, settings) implementations = { 'qmc': qmc, 'macro': macro, 'explicit_relay': explicit_relay, 'micro': micro} run_simulation(configuration, implementations)
def test_model_check_consistent1(macro_micro: Model) -> None: macro_micro.check_consistent()
def test_model_check_consistent2(macro_micro: Model) -> None: macro_micro.conduits[0].sender = Reference('marco.intermediate_state') with pytest.raises(RuntimeError): macro_micro.check_consistent()
def test_model_check_consistent3(macro_micro: Model) -> None: macro_micro.conduits[1].receiver = Reference('Macro.state_update') with pytest.raises(RuntimeError): macro_micro.check_consistent()
for i in range(n_samples): feed.append(float(inputs['feed' + str(i)])) kill.append(float(inputs['kill' + str(i)])) feed = np.array(feed) kill = np.array(kill) # Compute elements for the macro and micro model: specify the name of the python file here elements = [ComputeElement('macro', 'macro', [n_samples]), ComputeElement('micro', 'micro', [n_samples])] # connect the submodels conduits = [Conduit('macro.state_out', 'micro.state'), Conduit('micro.sgs', 'macro.state_in')] # create model instance model = Model('gray_scott_reduced', elements, conduits) # common settings settings_dict = {'micro.t_max': 0.1, 'micro.dt': 0.1, 'N_Q': 2, 'N_LF': 128} # parameter value settings, differs per run for i in range(n_samples): settings_dict['macro[%d].feed' % i] = feed[i] settings_dict['macro[%d].kill' % i] = kill[i] settings = Settings(settings_dict) configuration = Configuration(model, settings) # actual subroutines to run, imported from macro.py and micro.py implementations = {'macro': gray_scott_macro, 'micro': reduced_sgs}
elements = [ ComputeElement('qmc', 'qmc_driver'), ComputeElement('rr', 'load_balancer'), ComputeElement('macro', 'diffusion', [10]), ComputeElement('micro', 'reaction', [10])] conduits = [ Conduit('qmc.parameters_out', 'rr.front_in'), Conduit('rr.front_out', 'qmc.states_in'), Conduit('rr.back_out', 'macro.muscle_settings_in'), Conduit('macro.final_state_out', 'rr.back_in'), Conduit('macro.state_out', 'micro.initial_state'), Conduit('micro.final_state', 'macro.state_in') ] model = Model('reaction_diffusion_qmc', elements, conduits) settings = Settings(OrderedDict([ ('micro.t_max', 2.469136e-6), ('micro.dt', 2.469136e-8), ('macro.t_max', 1.234568e-4), ('macro.dt', 2.469136e-6), ('qmc.t_max', 1.234568e-4), ('qmc.dt', 2.469136e-6), ('x_max', 1.01), ('dx', 0.01), ('k_min', -4.455e4), ('k_max', -3.645e4), ('d_min', 0.03645), ('d_max', 0.04455), ('n_samples', 100) ]))
def test_dump_no_conduits(dump_model: Callable) -> None: ce1 = Component('ce1', 'test.impl1') model = Model('test_model', [ce1]) text = dump_model(model) assert text == ('name: test_model\n' 'components:\n' ' ce1: test.impl1\n')