parse variable dependency

This commit is contained in:
ignis 2019-04-20 23:26:05 +09:00
parent d8bedc9fc7
commit a981632555
3 changed files with 50 additions and 25 deletions

View file

@ -75,59 +75,77 @@ end do
@v_args(inline=True) # Affects the signatures of the methods
class CalculateTree(Transformer):
number = float
def __init__(self):
self.primary = []
self.derived = {}
self.averaged = {}
self.derivatives = {}
self.dependency = {}
def varlist(self, *args):
for arg in args:
self.primary.append(arg.value)
return self.primary
def assign_var(self, name, value):
def assign_var(self, name, (value, dep)):
self.derived[name.value] = value
self.dependency[name.value] = dep
return "{} = {}".format(name, value)
def number(self, numeral):
return (numeral, [])
def var(self, name):
return name + "(i,j,k)"
dep = []
if name not in self.primary:
dep.append(name.value)
return (name + "(i,j,k)", dep)
def env(self, name):
return name
return (name, [])
def dnx (self, a, b):
signature = "{}_{}".format(a.data, b)
def dnx (self, partial, b):
signature = "{}_{}".format(partial.data, b)
self.derivatives[signature] = (a.data, b)
self.derivatives[signature] = (partial.data, b.value)
return "{}_{}".format(a.data, b) + "(i,j,k)"
self.dependency[signature] = [b.value]
return (signature + "(i,j,k)", [signature])
def icall (self, a, (b, dep)):
fcode = "({0})".format(b)
def icall (self, a, b):
if a.data == "sqr":
return "(({0})*({0}))".format(b)
fcode = "(({0})*({0}))".format(b)
elif a.data == "pow3":
return "(({0})*({0})*({0}))".format(b)
fcode = "(({0})*({0})*({0}))".format(b)
def fcall (self, a, b):
return "( {} ( {} ) )".format(a, b)
return (fcode, dep)
def neg(self, value):
return "( - {} )".format(value)
def fcall (self, a, (b, dep)):
fcode = "( {} ( {} ) )".format(a, b)
return (fcode, dep)
def add(self, a, b):
return "( {} + {} )".format(a, b)
def neg(self, (b, dep)):
fcode = "( - {} )".format(b)
return (fcode, dep)
def sub(self, a, b):
return "( {} - {} )".format(a, b)
def add(self, (a, adep), (b, bdep)):
fcode = "( {} + {} )".format(a, b)
return (fcode, adep + bdep)
def mul(self, a, b):
return "( {} * {} )".format(a, b)
def sub(self, (a, adep), (b, bdep)):
fcode = "( {} - {} )".format(a, b)
return (fcode, adep + bdep)
def div(self, a, b):
return "( {} / {} )".format(a, b)
def mul(self, (a, adep), (b, bdep)):
fcode = "( {} * {} )".format(a, b)
return (fcode, adep + bdep)
def div(self, (a, adep), (b, bdep)):
fcode = "( {} / {} )".format(a, b)
return (fcode, adep + bdep)
log = lambda self : "log"
exp = lambda self : "exp"
@ -223,6 +241,10 @@ def test():
print mod_form.format(tf.module_dict())
print "! ", tf.derived
print "! ", tf.derivatives
print "! ", tf.dependency
if __name__ == '__main__':
test()
# main()

View file

@ -1,5 +1,5 @@
[u, v, w, y]
c_auto = 1 - y
c_auto = 1.0 - y
wrate_auto = rxn_rate(c_auto)
fsd_auto = sqrt (sqr(dx(c_auto)) + sqr(dy(c_auto)) + sqr(dz(c_auto)))

View file

@ -82,3 +82,6 @@ end subroutine m_terms_calculate_instant
end module m_terms
! {'c_auto': '( 1.0 - y(i,j,k) )', 'fsd_auto': '( sqrt ( ( ( ((dx_c_auto(i,j,k))*(dx_c_auto(i,j,k))) + ((dy_c_auto(i,j,k))*(dy_c_auto(i,j,k))) ) + ((dz_c_auto(i,j,k))*(dz_c_auto(i,j,k))) ) ) )', 'wrate_auto': '( rxn_rate ( c_auto(i,j,k) ) )'}
! {'dx_c_auto': ('dx', 'c_auto'), 'dy_c_auto': ('dy', 'c_auto'), 'dz_c_auto': ('dz', 'c_auto')}
! {'dx_c_auto': ['c_auto'], 'fsd_auto': ['dx_c_auto', 'dy_c_auto', 'dz_c_auto'], 'dy_c_auto': ['c_auto'], 'c_auto': [], 'wrate_auto': ['c_auto'], 'dz_c_auto': ['c_auto']}