def generate_second_initialisation(self): loop = [Symbol('_'+x.name) for x in self.index] # symbols for loop m = self.margin.value statements = [] v = symbols("v") for field in self.fields: body = [] if self.omp: statements.append(cgen.Pragma('omp for schedule(static,1)')) # populate xvalue, yvalue zvalue code for d in range(self.dimension-1, -1, -1): i = loop[d] i0 = m i1 = ccode(self.dim[d]-m) pre = [] post = [] if d == self.dimension-1: # inner loop # first time step kernel = self.transform_kernel(field) for arg in kernel.args: if str(arg).startswith("-") and str(self.t - 1) in str(arg): kernel = kernel.subs({arg: 0}, simultaneous=True) arg = 2*v*self.dt kernel = 0.5*(kernel + arg) kernel = kernel.subs({self.t: self.time[0]}) for idx in self.index: kernel = kernel.subs(idx, '_'+idx.name) body = [cgen.Assign(ccode(field[[self.time[1]]+loop]), ccode(kernel))] body = pre + body + post body = [cgen.For(cgen.InlineInitializer(cgen.Value('int', i), i0), cgen.Line('%s<%s' % (i, i1)), cgen.Line('++%s' % i), cgen.Block(body))] statements.append(body[0]) return statements
def generate_loop(self, fields): """ The functions to generate stress loops and velocity loops are identical, save for a single parameter. Moved the common code to this function to reduce repetition of code. """ if self.eval_const: self.create_const_dict() m = self.margin.value body = [] for d in range(self.dimension-1, -1, -1): i = self.index[d] i0 = m i1 = ccode(self.dim[d]-m) if d == self.dimension-1: # inner loop if not self.fission: body = self.simple_kernel(fields, [d, i, i0, i1]) else: body = self.fission_kernel(fields, [d, i, i0, i1]) if not d == self.dimension-1: body = [cgen.For(cgen.InlineInitializer(cgen.Value('int', i), i0), cgen.Line('%s<%s' % (i, i1)), cgen.Line('++%s' % i), cgen.Block(body))] if not self.pluto and self.omp: body.insert(0, cgen.Pragma('omp for schedule(static,1)')) return cgen.Module(body)
def initialise(self): loop = [Symbol('_'+x.name) for x in self.index] # symbols for loop statements = [] for field in self.fields: body = [] if self.omp: statements.append(cgen.Pragma('omp for schedule(static,1)')) # populate xvalue, yvalue zvalue code for d in range(self.dimension-1, -1, -1): i = loop[d] i0 = 0 i1 = ccode(self.dim[d]) pre = [] #velocity_initialisation = cgen.Assign(ccode()) post = [] if d == self.dimension-1: # inner loop # first time step t0 = 0 sol = field.sol.subs(self.t, t0) for idx in self.index: sol = sol.subs(idx, '_'+idx.name) body = [cgen.Assign(ccode(field[[0]+loop]), ccode(sol))] body = pre + body + post body = [cgen.For(cgen.InlineInitializer(cgen.Value('int', i), i0), cgen.Line('%s<%s' % (i, i1)), cgen.Line('++%s' % i), cgen.Block(body))] statements.append(body[0]) statements += self.generate_second_initialisation() return cgen.Module(statements)
def converge_test(self): """ - generate code for convergence test - convergence test implemented by calculating L2 norm of the simulation against analytical solution - L2 norm of each field is calculated and output with printf() - return generated code as string """ result = [] if not self.converge: return cgen.Module(result) m = self.margin.value ti = self.ntsteps.value % 2 # last updated grid loop = [Symbol('_'+x.name) for x in self.index] # symbols for loop for i in range(len(self.spacing)): result.append(cgen.Statement('printf("%d\\n")' % self.spacing[i].value)) for field in self.fields: body = [] l2 = ccode(field.label)+'_l2' idx = [ti] + loop result.append(cgen.Initializer(cgen.Value(self.real_t, l2), 0.0)) # populate xvalue, yvalue zvalue code for d in range(self.dimension-1, -1, -1): i = loop[d] i0 = m i1 = ccode(self.dim[d]-m) expr = self.spacing[d]*(loop[d] - self.margin.value) pre = [cgen.Initializer(cgen.Value(self.real_t, self.index[d].name), ccode(expr))] if d == self.dimension-1: # inner loop tn = self.dt.value*self.ntsteps.value \ if not field.staggered[0] \ else self.dt.value*self.ntsteps.value \ + self.dt.value/2.0 body = [cgen.Statement('%s += %s' % (l2, ccode((field[idx] - (field.sol.subs(self.t, tn)))**2.0)))] body = pre+body body = [cgen.For(cgen.InlineInitializer(cgen.Value('int', i), i0), cgen.Line('%s<%s' % (i, i1)), cgen.Line('++%s' % i), cgen.Block(body))] result += body volume = 1.0 for i in range(len(self.spacing)): volume *= self.spacing[i].value l2_value = 'pow(' + l2 + '*' + ccode(volume) + ', 0.5)' result.append(cgen.Statement('conv->%s = %s' % (l2, l2_value))) return cgen.Module(result)
def simple_kernel(self, grid_field, indexes): """ Generate the inner loop with all fields from stress or velocity :param grid_field: stress or velocity field array :param indexes: array with dimension, dimension var, initial margin, final margin - iterate through fields and replace mako template - return inner loop code as string """ body = [] idx = [self.time[len(self.time)-1]] + self.index # This loop create the most inner loop with all fields for field in grid_field: body.append(cgen.Assign(ccode(field[idx]), ccode(self.kernel_sympy(field)))) body = [cgen.For(cgen.InlineInitializer(cgen.Value('int', indexes[1]), indexes[2]), cgen.Line('%s<%s' % (indexes[1], indexes[3])), cgen.Line('++%s' % indexes[1]), cgen.Block(body))] if not self.pluto and self.ivdep and indexes[0] == self.dimension-1: body.insert(0, self.compiler._ivdep) if not self.pluto and self.simd and indexes[0] == self.dimension-1: body.insert(0, cgen.Pragma('simd')) return body