From 5f4f87afebceba4afb5f1d96cdc8329c6a9226ba Mon Sep 17 00:00:00 2001 From: ignis Date: Wed, 17 Apr 2019 03:33:14 +0900 Subject: [PATCH] working instant term calculation --- code/code_gen/calc.py | 125 ------------------------- code/code_gen/code_gen.py | 58 ++++++++++-- code/code_gen/resources/m_template.f90 | 37 ++++++++ code/code_gen/terms.input | 5 +- code/m_arrays.f90 | 33 +++++++ code/m_terms.f90 | 58 ++++++++++++ code/makefile | 2 + code/post.f90 | 13 ++- 8 files changed, 190 insertions(+), 141 deletions(-) delete mode 100644 code/code_gen/calc.py create mode 100644 code/code_gen/resources/m_template.f90 create mode 100644 code/m_arrays.f90 create mode 100644 code/m_terms.f90 diff --git a/code/code_gen/calc.py b/code/code_gen/calc.py deleted file mode 100644 index f893bc0..0000000 --- a/code/code_gen/calc.py +++ /dev/null @@ -1,125 +0,0 @@ -# -# This example shows how to write a basic calculator with variables. -# - -from lark import Lark, Transformer, v_args - - -try: - input = raw_input # For Python2 compatibility -except NameError: - pass - -''' - ?function: "log" -> log - | "exp" -> exp - | "dx" -> dx - | "d2x" -> d2x - - ''' - - -calc_grammar = """ - ?varlist: "[" [NAME ("," NAME)*] "]" - - ?start: NAME "=" sum -> assign_var - | varlist - - ?sum: product - | sum "+" product -> add - | sum "-" product -> sub - - ?product: atom - | product "*" atom -> mul - | product "/" atom -> div - - ?atom: NUMBER -> number - | "-" atom -> neg - | NAME -> var - | "(" sum ")" - | function "(" sum ")" -> fcall - - ?function: "log" -> log - | "exp" -> exp - | "dx" -> dx - | "d2x" -> d2x - | "sqrt" -> sqrt - - %import common.CNAME -> NAME - %import common.NUMBER - %import common.WS - - %ignore WS -""" - - -@v_args(inline=True) # Affects the signatures of the methods -class CalculateTree(Transformer): - from operator import add, sub, mul, truediv as div, neg - number = float - - def __init__(self): - self.primary = [] - self.derived = {} - - def varlist(self, *args): - for arg in args: - self.primary.append(arg.value) - return self.primary - - def assign_var(self, name, value): - self.derived[name] = value - return "{} = {}".format(name, value) - - def var(self, name): - return name - - def fcall (self, a, b): - return "( {} ( {} ) )".format(a, b) - - def neg(self, value): - return "( - {} )".format(value) - - def add(self, a, b): - return "( {} + {} )".format(a, b) - - def sub(self, a, b): - return "( {} - {} )".format(a, b) - - def mul(self, a, b): - return "( {} * {} )".format(a, b) - - def div(self, a, b): - return "( {} / {} )".format(a, b) - - log = lambda self : "log" - exp = lambda self : "exp" - dx = lambda self : "dx" - d2x = lambda self : "d2x" - sqrt = lambda self : "sqrt" - -tf=CalculateTree() - -calc_parser = Lark(calc_grammar, parser='lalr' , transformer=tf) -calc = calc_parser.parse - - -def main(): - while True: - try: - s = input('> ') - except EOFError: - break - print(calc(s)) - - -def test(): - with open("terms.input") as inputfile: - for line in inputfile: - if len(line.strip()) > 0: - print(calc(line)) - - -if __name__ == '__main__': - test() - # main() diff --git a/code/code_gen/code_gen.py b/code/code_gen/code_gen.py index f893bc0..c20e9d0 100644 --- a/code/code_gen/code_gen.py +++ b/code/code_gen/code_gen.py @@ -10,13 +10,6 @@ try: except NameError: pass -''' - ?function: "log" -> log - | "exp" -> exp - | "dx" -> dx - | "d2x" -> d2x - - ''' calc_grammar = """ @@ -52,6 +45,20 @@ calc_grammar = """ %ignore WS """ +real_array_decl = "real*8, allocatable, dimension(:,:,:) :: {}" +real_array_alloc = "allocate({0}(nxp,nyp,nzp), stat=ierr) ; {0} = 0." +real_array_free = "deallocate({})" + +real_array_loop = """ +do k = 1, nzp +do j = 1, nyp +do i = 1, nxp +{0[0]}(i,j,k) = {0[1]} +end do +end do +end do +""" + @v_args(inline=True) # Affects the signatures of the methods class CalculateTree(Transformer): @@ -61,6 +68,7 @@ class CalculateTree(Transformer): def __init__(self): self.primary = [] self.derived = {} + self.averaged = {} def varlist(self, *args): for arg in args: @@ -68,11 +76,11 @@ class CalculateTree(Transformer): return self.primary def assign_var(self, name, value): - self.derived[name] = value + self.derived[name.value] = value return "{} = {}".format(name, value) def var(self, name): - return name + return name+"(i,j,k)" def fcall (self, a, b): return "( {} ( {} ) )".format(a, b) @@ -98,6 +106,29 @@ class CalculateTree(Transformer): d2x = lambda self : "d2x" sqrt = lambda self : "sqrt" + def array_decl (self): + return "\n".join(map(real_array_decl.format, self.derived.iterkeys())) + + def array_init (self): + return "\n".join(map(real_array_alloc.format, self.derived.iterkeys())) + + def array_final (self): + return "\n".join(map(real_array_free.format, self.derived.iterkeys())) + + def array_ci (self): + return "\n".join(map(real_array_loop.format, self.derived.iteritems())) + + def module_dict (self): + md = {} + md["module_name"] = "terms" + md["module_data"] = self.array_decl() + md["module_init"] = self.array_init() + md["module_finalize"] = self.array_final() + md["module_ci"] = self.array_ci() + + return md + + tf=CalculateTree() calc_parser = Lark(calc_grammar, parser='lalr' , transformer=tf) @@ -114,12 +145,19 @@ def main(): def test(): + + + with open("resources/m_template.f90") as template_file: + mod_form = template_file.read() + with open("terms.input") as inputfile: for line in inputfile: if len(line.strip()) > 0: - print(calc(line)) + (calc(line)) + print mod_form.format(tf.module_dict()) + if __name__ == '__main__': test() # main() diff --git a/code/code_gen/resources/m_template.f90 b/code/code_gen/resources/m_template.f90 new file mode 100644 index 0000000..fbd5bd7 --- /dev/null +++ b/code/code_gen/resources/m_template.f90 @@ -0,0 +1,37 @@ +module m_{0[module_name]} + +use m_parameters +use m_arrays + +implicit none + +{0[module_data]} + +contains + +subroutine m_{0[module_name]}_init + +integer :: ierr + +{0[module_init]} + +end subroutine m_{0[module_name]}_init + + +subroutine m_{0[module_name]}_finalize + +{0[module_finalize]} + +end subroutine m_{0[module_name]}_finalize + + +subroutine m_{0[module_name]}_calculate_instant + +integer :: i, j, k + +{0[module_ci]} + +end subroutine m_{0[module_name]}_calculate_instant + + +end module m_{0[module_name]} diff --git a/code/code_gen/terms.input b/code/code_gen/terms.input index 560848e..44e4e78 100644 --- a/code/code_gen/terms.input +++ b/code/code_gen/terms.input @@ -1,5 +1,4 @@ [u, v, w, y] -c = 1 - y -wrate = 21000 * (1 - c) * exp (- 26.7 / (1 + 3 * c)) -dcdx = dx (y) +c_auto = 1 - y +wrate_auto = 21000 * (1 - c_auto) * exp (- 26.7 / (1 + 3 * c_auto)) diff --git a/code/m_arrays.f90 b/code/m_arrays.f90 new file mode 100644 index 0000000..ecc9f76 --- /dev/null +++ b/code/m_arrays.f90 @@ -0,0 +1,33 @@ +module m_arrays + +use m_parameters + +implicit none + +real*8, allocatable, dimension(:,:,:) :: u,v,w,y + +contains + +subroutine m_arrays_init + +integer :: ierr + + +end subroutine m_arrays_init + + +subroutine m_arrays_finalize + + +end subroutine m_arrays_finalize + + +subroutine m_arrays_calculate_instant + +integer :: i,j,k + +end subroutine m_arrays_calculate_instant + + +end module m_arrays + diff --git a/code/m_terms.f90 b/code/m_terms.f90 new file mode 100644 index 0000000..4920bc9 --- /dev/null +++ b/code/m_terms.f90 @@ -0,0 +1,58 @@ +module m_terms + +use m_parameters +use m_arrays + +implicit none + +real*8, allocatable, dimension(:,:,:) :: c_auto +real*8, allocatable, dimension(:,:,:) :: wrate_auto + +contains + +subroutine m_terms_init + +integer :: ierr + +allocate(c_auto(nxp,nyp,nzp), stat=ierr) ; c_auto = 0. +allocate(wrate_auto(nxp,nyp,nzp), stat=ierr) ; wrate_auto = 0. + +end subroutine m_terms_init + + +subroutine m_terms_finalize + +deallocate(c_auto) +deallocate(wrate_auto) + +end subroutine m_terms_finalize + + +subroutine m_terms_calculate_instant + +integer :: i, j, k + + +do k = 1, nzp +do j = 1, nyp +do i = 1, nxp +c_auto(i,j,k) = ( 1.0 - y(i,j,k) ) +end do +end do +end do + + +do k = 1, nzp +do j = 1, nyp +do i = 1, nxp +wrate_auto(i,j,k) = ( ( 21000.0 * ( 1.0 - c_auto(i,j,k) ) ) * ( exp ( ( ( - 26.7 ) / ( 1.0 + ( 3.0 * c_auto(i,j,k) ) ) ) ) ) ) +end do +end do +end do + + +end subroutine m_terms_calculate_instant + + +end module m_terms + diff --git a/code/makefile b/code/makefile index 004ee70..81ff884 100644 --- a/code/makefile +++ b/code/makefile @@ -19,6 +19,8 @@ compiler = gfortran MODULES += \ Compact.o\ m_parameters.o\ + m_arrays.o\ + m_terms.o\ post.o # Objects diff --git a/code/post.f90 b/code/post.f90 index b060469..526dcf6 100644 --- a/code/post.f90 +++ b/code/post.f90 @@ -1,6 +1,8 @@ MODULE post USE Compact USE m_parameters + USE m_arrays + USE m_terms IMPLICIT NONE PRIVATE @@ -25,11 +27,9 @@ !REAL, DIMENSION(:,:,:), ALLOCATABLE :: c_dot,c_dot_g,c_g,FSD_dot !REAL, DIMENSION(:,:,:), ALLOCATABLE :: vn,GN_vn,GN_sd,G2N_c !REAL, DIMENSION(:,:,:), ALLOCATABLE :: G2_Y - REAL, DIMENSION(:,:,:), ALLOCATABLE :: c,Wc,y + REAL, DIMENSION(:,:,:), ALLOCATABLE :: c,Wc REAL, DIMENSION(:,:,:), ALLOCATABLE :: c_dot,c_dot_g,c_g,FSD_dot - REAL, DIMENSION(:,:,:), ALLOCATABLE :: u,v,w - REAL, DIMENSION(:,:,:,:), ALLOCATABLE :: old_scalar, new_scalar REAL, DIMENSION(:,:,:,:), ALLOCATABLE :: m_v,NV,G_C,sd,u_dot,G_V,G_FSD,ub_dot,uu_dot @@ -49,6 +49,8 @@ CALL ALLOCATE_ARRAYS + CALL m_terms_init + CALL PRINT_BANNER countnum=0 @@ -75,6 +77,9 @@ ! CALL CAL_vnsd CALL CAL_SUM ! Sum for each fort.xxxx CALL SAVE_SUM ! Total sum + + CALL m_terms_calculate_instant + ENDIF ENDDO firstloop @@ -114,6 +119,8 @@ CALL SAVE_AVG_RESULTS CALL DEALLOCATES_CLOSE + CALL m_terms_finalize + WRITE(*,*) ' Avergaing RAW data is FINISHED' WRITE(*,*) 'qEdge_X.dat is generated'