From a981632555675fa77e9a2d910c1d7d743e82de1c Mon Sep 17 00:00:00 2001 From: ignis Date: Sat, 20 Apr 2019 23:26:05 +0900 Subject: [PATCH] parse variable dependency --- code/code_gen/code_gen.py | 70 +++++++++++++++++++++++++-------------- code/code_gen/terms.input | 2 +- code/m_terms.f90 | 3 ++ 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/code/code_gen/code_gen.py b/code/code_gen/code_gen.py index 92fefd6..f79e7b8 100644 --- a/code/code_gen/code_gen.py +++ b/code/code_gen/code_gen.py @@ -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() diff --git a/code/code_gen/terms.input b/code/code_gen/terms.input index ed2e5ff..de2b056 100644 --- a/code/code_gen/terms.input +++ b/code/code_gen/terms.input @@ -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))) diff --git a/code/m_terms.f90 b/code/m_terms.f90 index 2c5f161..85ca4df 100644 --- a/code/m_terms.f90 +++ b/code/m_terms.f90 @@ -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']}