Skip to content

Commit 00161d3

Browse files
committed
Another net example
1 parent 356fc47 commit 00161d3

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,4 @@ examples/TensorFlow/Keras/keras_to_MDF
317317
/docs/sphinx/source/api/export_format/MDF/images/newton.png
318318
/docs/sphinx/source/api/export_format/MDF/images/newton_plot.png
319319
/examples/MDF/networks/Net_acyclical
320+
/examples/MDF/networks/Net_network2
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"Net_network2": {
3+
"format": "ModECI MDF v0.4",
4+
"generating_application": "Python modeci-mdf v0.4.14",
5+
"graphs": {
6+
"Net_network2": {
7+
"nodes": {
8+
"A": {
9+
"input_ports": {
10+
"input": {
11+
"default_value": 5
12+
}
13+
},
14+
"parameters": {
15+
"total_inputs": {
16+
"value": "total_inputs + input"
17+
},
18+
"execute_count": {
19+
"value": "execute_count + 1"
20+
}
21+
},
22+
"output_ports": {
23+
"out_port": {
24+
"value": "total_inputs"
25+
}
26+
}
27+
},
28+
"B": {
29+
"input_ports": {
30+
"input": {}
31+
},
32+
"parameters": {
33+
"total_inputs": {
34+
"value": "total_inputs + input"
35+
},
36+
"execute_count": {
37+
"value": "execute_count + 1"
38+
}
39+
},
40+
"output_ports": {
41+
"out_port": {
42+
"value": "total_inputs"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
14.7 KB
Loading
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Net_network2:
2+
format: ModECI MDF v0.4
3+
generating_application: Python modeci-mdf v0.4.14
4+
graphs:
5+
Net_network2:
6+
nodes:
7+
A:
8+
input_ports:
9+
input:
10+
default_value: 5
11+
parameters:
12+
total_inputs:
13+
value: total_inputs + input
14+
execute_count:
15+
value: execute_count + 1
16+
output_ports:
17+
out_port:
18+
value: total_inputs
19+
B:
20+
input_ports:
21+
input: {}
22+
parameters:
23+
total_inputs:
24+
value: total_inputs + input
25+
execute_count:
26+
value: execute_count + 1
27+
output_ports:
28+
out_port:
29+
value: total_inputs

examples/MDF/networks/network2.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)