|
| 1 | +""" |
| 2 | + Example of ModECI MDF - Testing networks |
| 3 | +""" |
| 4 | + |
| 5 | +from modeci_mdf.mdf import * |
| 6 | +import sys |
| 7 | +import os |
| 8 | +from modeci_mdf.utils import simple_connect |
| 9 | + |
| 10 | + |
| 11 | +def main(ref="network2"): |
| 12 | + |
| 13 | + mod = Model(id="Net_%s" % ref) |
| 14 | + |
| 15 | + mod_graph = Graph(id="Net_%s" % ref) |
| 16 | + mod.graphs.append(mod_graph) |
| 17 | + |
| 18 | + for id in ["A", "B"]: |
| 19 | + |
| 20 | + node = Node(id=id) |
| 21 | + |
| 22 | + if id == "A": |
| 23 | + ip1 = InputPort(id="input", default_value=5) |
| 24 | + node.input_ports.append(ip1) |
| 25 | + |
| 26 | + if id == "B": |
| 27 | + ip1 = InputPort(id="input") |
| 28 | + node.input_ports.append(ip1) |
| 29 | + |
| 30 | + p1 = Parameter(id="total_inputs", value="total_inputs + input") |
| 31 | + node.parameters.append(p1) |
| 32 | + |
| 33 | + p2 = Parameter(id="execute_count", value="execute_count + 1") |
| 34 | + node.parameters.append(p2) |
| 35 | + |
| 36 | + op1 = OutputPort(id="out_port", value=p1.id) |
| 37 | + node.output_ports.append(op1) |
| 38 | + |
| 39 | + mod_graph.nodes.append(node) |
| 40 | + |
| 41 | + # simple_connect(mod_graph.get_node("A"), mod_graph.get_node("B"), mod_graph) |
| 42 | + # simple_connect(mod_graph.get_node("B"), mod_graph.get_node("A"), mod_graph) |
| 43 | + |
| 44 | + new_file = mod.to_json_file("%s.json" % mod.id) |
| 45 | + new_file = mod.to_yaml_file("%s.yaml" % mod.id) |
| 46 | + |
| 47 | + eg = None |
| 48 | + |
| 49 | + if "-run" in sys.argv: |
| 50 | + verbose = True |
| 51 | + # verbose = False |
| 52 | + from modeci_mdf.utils import load_mdf, print_summary |
| 53 | + |
| 54 | + from modeci_mdf.execution_engine import EvaluableGraph |
| 55 | + |
| 56 | + eg = EvaluableGraph(mod_graph, verbose) |
| 57 | + dt = 1 |
| 58 | + |
| 59 | + duration = 2 |
| 60 | + t = 0 |
| 61 | + recorded = {} |
| 62 | + times = [] |
| 63 | + ai = [] |
| 64 | + bi = [] |
| 65 | + at = [] |
| 66 | + bt = [] |
| 67 | + ao = [] |
| 68 | + bo = [] |
| 69 | + while t <= duration: |
| 70 | + times.append(t) |
| 71 | + print("====== Evaluating at t = %s ======" % (t)) |
| 72 | + if t == 0: |
| 73 | + eg.evaluate() # replace with initialize? |
| 74 | + else: |
| 75 | + eg.evaluate(time_increment=dt) |
| 76 | + |
| 77 | + ai.append(eg.enodes["A"].evaluable_parameters["execute_count"].curr_value) |
| 78 | + bi.append(eg.enodes["B"].evaluable_parameters["execute_count"].curr_value) |
| 79 | + at.append(eg.enodes["A"].evaluable_parameters["total_inputs"].curr_value) |
| 80 | + bt.append(eg.enodes["B"].evaluable_parameters["total_inputs"].curr_value) |
| 81 | + ao.append(eg.enodes["A"].evaluable_outputs["out_port"].curr_value) |
| 82 | + bo.append(eg.enodes["B"].evaluable_outputs["out_port"].curr_value) |
| 83 | + t += dt |
| 84 | + |
| 85 | + if "-nogui" not in sys.argv: |
| 86 | + import matplotlib.pyplot as plt |
| 87 | + |
| 88 | + plt.plot(times, ai, marker="x", label="A exe count") |
| 89 | + plt.plot(times, bi, label="B exe count") |
| 90 | + plt.plot(times, at, marker="D", label="A total_inputs") |
| 91 | + plt.plot(times, bt, label="B total_inputs") |
| 92 | + |
| 93 | + plt.legend() |
| 94 | + plt.show() |
| 95 | + |
| 96 | + if "-graph" in sys.argv: |
| 97 | + mod.to_graph_image( |
| 98 | + engine="dot", |
| 99 | + output_format="png", |
| 100 | + view_on_render=False, |
| 101 | + level=3, |
| 102 | + filename_root=mod_graph.id, |
| 103 | + only_warn_on_fail=( |
| 104 | + os.name == "nt" |
| 105 | + ), # Makes sure test of this doesn't fail on Windows on GitHub Actions |
| 106 | + ) |
| 107 | + |
| 108 | + return mod_graph, eg |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + g, eg = main() |
| 113 | + |
| 114 | + print("--------------------") |
| 115 | + print(g.to_yaml()) |
| 116 | + print("--------------------") |
| 117 | + if eg is not None: |
| 118 | + for n in eg.enodes: |
| 119 | + enode = eg.enodes[n] |
| 120 | + print(enode) |
| 121 | + print([enode.evaluable_outputs for enode in eg.enodes.values()]) |
| 122 | + else: |
| 123 | + print("No execution engine created. Run with: -run") |
0 commit comments