-
Notifications
You must be signed in to change notification settings - Fork 3
/
model_create.py
182 lines (156 loc) · 7.52 KB
/
model_create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright 2013 Devsim LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import devsim as ds
debug = False
def CreateSolution(device, region, name):
'''
Creates solution variables
As well as their entries on each edge
'''
ds.node_solution(name=name, device=device, region=region)
ds.edge_from_node_model(node_model=name, device=device, region=region)
def CreateNodeModel(device, region, model, expression):
'''
Creates a node model
'''
result=ds.node_model(device=device, region=region, name=model, equation=expression)
if debug:
print(("NODEMODEL {d} {r} {m} \"{re}\"".format(d=device, r=region, m=model, re=result)))
def CreateNodeModelDerivative(device, region, model, expression, *vars):
'''
Create a node model derivative
'''
for v in vars:
CreateNodeModel(device, region,
"{m}:{v}".format(m=model, v=v),
"diff({e},{v})".format(e=expression, v=v))
#"simplify(diff({e},{v}))".format(e=expression, v=v))
def CreateContactNodeModel(device, contact, model, expression):
'''
Creates a contact node model
'''
result=ds.contact_node_model(device=device, contact=contact, name=model, equation=expression)
if debug:
print(("CONTACTNODEMODEL {d} {c} {m} \"{re}\"".format(d=device, c=contact, m=model, re=result)))
def CreateContactNodeModelDerivative(device, contact, model, expression, variable):
'''
Creates a contact node model derivative
'''
CreateContactNodeModel(device, contact,
"{m}:{v}".format(m=model, v=variable),
"diff({e}, {v})".format(e=expression, v=variable))
#"simplify(diff({e}, {v}))".format(e=expression, v=variable))
def CreateEdgeModel (device, region, model, expression):
'''
Creates an edge model
'''
result=ds.edge_model(device=device, region=region, name=model, equation=expression)
if debug:
print("EDGEMODEL {d} {r} {m} \"{re}\"".format(d=device, r=region, m=model, re=result));
def CreateEdgeModelDerivatives(device, region, model, expression, variable):
'''
Creates edge model derivatives
'''
CreateEdgeModel(device, region,
"{m}:{v}@n0".format(m=model, v=variable),
"diff({e}, {v}@n0)".format(e=expression, v=variable))
#"simplify(diff({e}, {v}@n0))".format(e=expression, v=variable))
CreateEdgeModel(device, region,
"{m}:{v}@n1".format(m=model, v=variable),
"diff({e}, {v}@n1)".format(e=expression, v=variable))
#"simplify(diff({e}, {v}@n1))".format(e=expression, v=variable))
def CreateContactEdgeModel(device, contact, model, expression):
'''
Creates a contact edge model
'''
result=contact_edge_model(device=device, contact=contact, name=model, equation=expression)
if debug:
print(("CONTACTEDGEMODEL {d} {c} {m} \"{re}\"".format(d=device, c=contact, m=model, re=result)))
def CreateContactEdgeModelDerivative(device, contact, model, expression, variable):
'''
Creates contact edge model derivatives with respect to variable on node
'''
CreateContactEdgeModel(device, contact, "{m}:{v}".format(m=model, v=variable), "diff({e}, {v})".format(e=expression, v=variable))
#CreateContactEdgeModel(device, contact, "{m}:{v}".format(m=model, v=variable), "simplify(diff({e}, {v}))".format(e=expression, v=variable))
def CreateInterfaceModel(device, interface, model, expression):
'''
Creates a interface node model
'''
result=ds.interface_model(device=device, interface=interface, name=model, equation=expression)
if debug:
print(("INTERFACEMODEL {d} {i} {m} \"{re}\"".format(d=device, i=interface, m=model, re=result)))
#def CreateInterfaceModelDerivative(device, interface, model, expression, variable):
# '''
# Creates interface edge model derivatives with respect to variable on node
# '''
# CreateInterfaceModel(device, interface, "{m}:{v}".format(m=model, v=variable), "simplify(diff({e}, {v}))".format(e=expression, v=variable))
def CreateContinuousInterfaceModel(device, interface, variable):
mname = "continuous{0}".format(variable)
meq = "{0}@r0 - {0}@r1".format(variable)
mname0 = "{0}:{1}@r0".format(mname, variable)
mname1 = "{0}:{1}@r1".format(mname, variable)
CreateInterfaceModel(device, interface, mname, meq)
CreateInterfaceModel(device, interface, mname0, "1")
CreateInterfaceModel(device, interface, mname1, "-1")
return mname
def InEdgeModelList(device, region, model):
'''
Checks to see if this edge model is available on device and region
'''
return model in ds.get_edge_model_list(device=device, region=region)
def InNodeModelList(device, region, model):
'''
Checks to see if this node model is available on device and region
'''
return model in ds.get_node_model_list(device=device, region=region)
#### Make sure that the model exists, as well as it's node model
def EnsureEdgeFromNodeModelExists(device, region, nodemodel):
'''
Checks if the edge models exists
'''
if not InNodeModelList(device, region, nodemodel):
raise "{} must exist"
emlist = get_edge_model_list(device=device, region=region)
emtest = ("{0}@n0".format(nodemodel) and "{0}@n1".format(nodemodel))
if not emtest:
if debug:
print("INFO: Creating ${0}@n0 and ${0}@n1".format(nodemodel))
ds.edge_from_node_model(device=device, region=region, node_model=nodemodel)
def CreateElementModel2d(device, region, model, expression):
result=element_model(device=device, region=region, name=model, equation=expression)
if debug:
print(("ELEMENTMODEL {d} {r} {m} \"{re}\"".format(d=device, r=region, m=model, re=result)))
def CreateElementModelDerivative2d(device, region, model_name, expression, *args):
if len(args) == 0:
raise ValueError("Must specify a list of variable names")
for i in args:
for j in ("@en0", "@en1", "@en2"):
CreateElementModel2d(device, region, "{0}:{1}{2}".format(model_name, i, j), "diff({0}, {1}{2})".format(expression, i, j))
### edge_model is the name of the edge model to be created
def CreateGeometricMean(device, region, nmodel, emodel):
ds.edge_average_model(device=device, region=region, edge_model=emodel, node_model=nmodel, average_type="geometric")
def CreateGeometricMeanDerivative(device, region, nmodel, emodel, *args):
if len(args) == 0:
raise ValueError("Must specify a list of variable names")
for i in args:
ds.edge_average_model(device=device, region=region, edge_model=emodel, node_model=nmodel,
derivative=i, average_type="geometric")
def CreateArithmeticMean(device, region, nmodel, emodel):
ds.edge_average_model(device=device, region=region, edge_model=emodel, node_model=nmodel, average_type="arithmetic")
def CreateArithmeticMeanDerivative(device, region, nmodel, emodel, *args):
if len(args) == 0:
raise ValueError("Must specify a list of variable names")
for i in args:
edge_average_model(device=device, region=region, edge_model=emodel, node_model=nmodel,
derivative=i, average_type="arithmetic")