from zoomy_core.model.sympy2c_new import*from sympy import symbolsx, y = symbols("x y")expr = x + yargs = [symbols("out"), x, y]write_plain_c_module("test_module", [("f", expr, args)], "./build")
---------------------------------------------------------------------------TypeError Traceback (most recent call last)
CellIn[5], line 8 5 expr = x + y
6 args = [symbols("out"), x, y]
----> 8write_plain_c_module("test_module",[("f",expr,args)],"./build")File ~/Git/Zoomy/library/model/sympy2c_new.py:46, in write_plain_c_module(module_name, expression_name_tuples, directory) 43 c_code = "" 45for name, expr, args in expression_name_tuples:
---> 46 routine = make_plain_routine(name,expr,args) 47 c_code += printer._print_Routine(routine) + "\n\n" 49# Write C fileFile ~/Git/Zoomy/library/model/sympy2c_new.py:35, in make_plain_routine(name, expr, args) 33 output_arg = args[0]
34 assign = Assignment(output_arg, expr)
---> 35returnRoutine(name,[assign],args)TypeError: Routine.__init__() missing 2 required positional arguments: 'local_vars' and 'global_vars'
Load Mesh with correct boundary function indices at facets
def load_mesh(path_to_mesh): mesh = Mesh.from_gmsh(path_to_mesh) min_inradius = np.min(mesh.cell_inradius) tags = [int(v) for v in mesh.boundary_conditions_sorted_physical_tags] tags.sort() map_tag_to_id = {v: i for i, v inenumerate(tags)} mesh, cell_tags, facet_tags = gmshio.read_from_msh( path_to_mesh, MPI.COMM_WORLD, 0, gdim=2 ) unique_facet_tags = np.unique(facet_tags.values) facet_boundary_function_id = np.array([map_tag_to_id[tag] for tag in facet_tags.values[:]])return mesh, cell_tags, facet_tags, unique_facet_tags, facet_boundary_function_id, min_inradius
Info : Reading '/home/ingo/Git/Zoomy/meshes/channel_quad_2d/mesh_coarse.msh'...
Info : 729 nodes
Info : 816 elements
Info : Done reading '/home/ingo/Git/Zoomy/meshes/channel_quad_2d/mesh_coarse.msh'
#################TODO#################################I = ufl.as_tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]])######################################################def numerical_flux(model, Ql, Qr, Qauxl, Qauxr, parameters, n, domain):return ufl.dot(0.5*(model.flux(Ql, Qauxl, parameters)+ model.flux(Qr, Qauxr, parameters)), n)-0.5*0.5*(max_abs_eigenvalue(model, Ql, Qauxl, n, domain) + max_abs_eigenvalue(model, Qr, Qauxr, n, domain) )* I * (Qr- Ql)def extract_scalar_fields(Q): n_dofs = Q.function_space.num_sub_spaces out = []for i inrange(n_dofs): qi = Q.sub(i).collapse()# qi.x.array[qi.x.array < 1e-12] = 0. qi.name =f"q_{i}" out.append(qi)return outdef _max_abs_eigenvalue(model, Q, Qaux, n, domain): eigenvalues = model.eigenvalues(Q, Qaux, model.parameters, n) evs = evaluate_on_all_facets_midpoint(eigenvalues, domain)[1]return np.max(abs(evs))def max_abs_eigenvalue(model, Q, Qaux, n, domain): ev = model.eigenvalues(Q, Qaux, model.parameters, n) max_ev =abs(ev[0, 0])for i inrange(1, model.n_variables): max_ev = ufl.conditional(ev[1, 0] > max_ev, ev[1, 0], max_ev)return max_evdef compute_time_step_size(model, Q, Qaux, n, reference_cell_diameter, domain, CFL=0.45): n1 = ufl.as_vector((1, 0)) n2 = ufl.as_vector((0, 1)) evs_m = _max_abs_eigenvalue(model, Q, Qaux, n1, domain) evs_p = _max_abs_eigenvalue(model, Q, Qaux, n2, domain) local_max_eigenvalue =max(evs_m, evs_p)# Global maximum reduction across all ranks global_max_eigenvalue = MPI.COMM_WORLD.allreduce( local_max_eigenvalue, op=MPI.MAX) dt = CFL * reference_cell_diameter / global_max_eigenvalueif np.isnan(dt) or np.isinf(dt) or dt <10**(-6): dt =10**(-6)return dt
def update_qaux(Q, Qaux, Qold, Qauxold, model, parameters, time, dt):return Qaux
def weak_form_swe(model, functionspace, q_n, q_np, qaux_n, qaux_np, parameters, t, x, dt, domain, cell_tags, facet_tags, unique_facet_tags, facet_boundary_function_id):# facet normals# domain = functionspace.mesh n = ufl.FacetNormal(domain)# our integration measures over the inner boundaries, the domain boundaries and the whole domain. # Note that we separate the domain boundaries in order to potentially apply different boundary conditions# on each side dS = ufl.Measure("dS", domain=domain)# facet_tags = generate_facets_tags(domain, P0, P1) ds = ufl.Measure("ds", domain=domain, subdomain_data=facet_tags) dx = ufl.dx# implicit/explicit switch q = q_n qaux = qaux_n# q_ghost = ufl.Function(functionspace)# We would like to have gradients of the bottom topography. However, DG0 is constant in each cell, resulting in zero gradients.# we help ourselves by projecting DG0 to a CG1 (linear continuous functions) space, where the gradients do exist.# note that this is a 'cheap trick'. In reality, the computation of the bottom topography gradient is critical and deserves# more attention. elem_CG1 = basix.ufl.element("CG", domain.topology.cell_name(), 1) space_CG1 = fem.functionspace(domain, elem_CG1) test_q = ufl.TestFunction(functionspace) trial_q = ufl.TrialFunction(functionspace)# weak formulation weak_form = ufl.dot(test_q, (trial_q-q)/dt) * dx weak_form += ufl.dot((test_q("+") - test_q("-")), numerical_flux(model, q("+"), q("-"), qaux("+"), qaux("-"), parameters, n("+"), domain)) * dS# weak_form += ufl.dot((test_q), numerical_flux(q, q_extrapolation, n)) * (ds(1) + ds(2) + ds(3) + ds(4))for i, tag inenumerate(unique_facet_tags):# q_ghost.interpolate(boundary_functions[i](q))#TODO dX is wrong dX = x[0] weak_form += ufl.dot((test_q), numerical_flux(model, q, model.bcs(t, x, dX, q, qaux, parameters, n)[i,:], qaux, qaux,parameters, n, domain)) * ds(tag)# weak_form += ufl.dot((test_q), numerical_flux(q, q, n)) * ds(tag)#################TODO################################# weak_form +=0###################################################### weak_form_lhs = fem.form(ufl.lhs(weak_form)) weak_form_rhs = fem.form(ufl.rhs(weak_form))return weak_form_lhs, weak_form_rhs
def prepare_solver(weak_form_lhs, weak_form_rhs): A = petsc.create_matrix(weak_form_lhs) b = petsc.create_vector(weak_form_rhs) solver = PETSc.KSP().create(MPI.COMM_WORLD) solver.setOperators(A) solver.setType(PETSc.KSP.Type.BCGS) preconditioner = solver.getPC() preconditioner.setType(PETSc.PC.Type.JACOBI)return solver, A, b
Info : Reading '/home/ingo/Git/Zoomy/meshes/channel_quad_2d/mesh_coarse.msh'...
Info : 729 nodes
Info : 816 elements
Info : Done reading '/home/ingo/Git/Zoomy/meshes/channel_quad_2d/mesh_coarse.msh'