Peano
Loading...
Searching...
No Matches
dg.py
Go to the documentation of this file.
1import os, sys
2import argparse
3import peano4
4import mghype
5import numpy as np
6
7
8def get_args():
9 print( """
10
11 Peano 4 multigrid solver using Discontinuous Galerkin
12
13 """)
14
15 parser = argparse.ArgumentParser(description='Multigrid - Poisson solver')
16 parser.add_argument("-j", "--parallel-builds", dest="j", type=int, default=-1, help="Parallel builds" )
17 parser.add_argument("-pt", "--plot-sweep", dest="plot_after_each_mesh_sweep", action="store_true", default=False, help="Plot after each timestep. By default (False) we plot after initialisation and at the end" )
18 parser.add_argument("-pd", "--peano-dir", dest="peanodir", default="../../../../", help="Peano4 directory" )
19 parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose")
20 parser.add_argument("-d", "--dimensions", dest="dimensions", type=int, default=2, help="Dimensions (default 2)")
21 parser.add_argument("-p", "--poly_degree", dest="poly_degree", type=int, default=1, help="Polynomial Degree (default 1)")
22 parser.add_argument("-meshsize", "--meshsize", dest="meshsize", default=0.3, help="Mesh size (default 0.3)")
23 parser.add_argument("-omega_c", "--omega_c", dest="omega_c", default=0.5, help="Mesh size (default 0.5)")
24 parser.add_argument("-omega_f", "--omega_f", dest="omega_f", default=1.0, help="Mesh size (default 1.0)")
25 parser.add_argument("-m", "--mode", dest="mode", choices=["release","stats","asserts", "trace"], required=True, help="Pick build type" )
26 parser.add_argument("-t", "--test-run", dest="test_run", action="store_true", default=False, help="Trigger test run")
27
28 return parser.parse_args()
29
30
32 #
33 # Configure build
34 #
35 if args.mode=="release":
36 build_mode = peano4.output.CompileMode.Release
37 if args.mode=="stats":
38 build_mode = peano4.output.CompileMode.Stats
39 if args.mode=="asserts":
40 build_mode = peano4.output.CompileMode.Asserts
41 if args.mode=="trace":
42 build_mode = peano4.output.CompileMode.Trace
43
44
45 cube_size = 1.0
46 project.set_global_simulation_parameters(
47 dimensions = args.dimensions,
48 offset = [ 0.0 for _ in range(args.dimensions) ],
49 domain_size = [ cube_size for _ in range(args.dimensions) ],
50 plot_after_each_mesh_sweep = args.plot_after_each_mesh_sweep,
51 max_iterations = 10000,
52# max_iterations = 10,
53 )
54
55 project.set_load_balancing( "toolbox::loadbalancing::strategies::SpreadOutOnceGridStagnates", "new ::exahype2::LoadBalancingConfiguration(0.9, 0, false, 8, 50)" )
56 project.set_Peano4_installation( args.peanodir, build_mode )
57
58 peano4_project = project.generate_Peano4_project(args.verbose)
59 peano4_project.output.makefile.add_cpp_file( "Scenario.cpp" )
60 peano4_project.generate()
61
62
63def run_and_validate(args,executable):
64 os.system( "make clean" )
65 if args.j>0:
66 os.system( "make -j{}".format(args.j) )
67 else:
68 os.system( "make -j$(nproc --all)" )
69
70 if args.test_run:
71 os.system( "./" + executable + " > " + executable + ".out")
72 output_file = open(executable+".out", "r")
73 initial_r_max = 0.0
74 final_r_max = 0.0
75 for line in output_file.readlines():
76 if "endMeshSweep()" in line and initial_r_max==0.0:
77 initial_r_max = float( line.split( "|r|_max=" )[1].split(",")[0] )
78 if "endMeshSweep()" in line:
79 final_r_max = float( line.split( "|r|_max=" )[1].split(",")[0] )
80 print( "initial residual = {}".format( initial_r_max) )
81 print( "final residual = {}".format( final_r_max) )
82 if initial_r_max>0.0:
83 improvement = initial_r_max / final_r_max;
84 else:
85 improvement = 0.0
86 print( "improvement = {}".format( improvement) )
87 if improvement>10.0:
88 print( "SUCCESS")
89 else:
90 sys.exit( "FAILED: residual not within bounds" )
91
92
93
94if __name__ == "__main__":
95 args = get_args()
96
97
98 executable = "peano4matrixfree"
99
100 project = mghype.matrixfree.api.Project( project_name = "DGPointwise",
101 namespace = [ "tests", "multigrid", "matrixfree", "poisson" ],
102 executable = executable
103 )
104
105
107 args.dimensions,
108 args.poly_degree,
109 1, # Unknowns per cell dof. Scalar PDE here
110 2, # We use the penalty formulation (see docu in tutorials)
111 2
112 )
113
114
115 assembly_matrix, assembly_matrix_scaling = matrices.get_cell_system_matrix_for_laplacian()
116 mass_matrix, mass_matrix_scaling = matrices.get_cell_mass_matrix()
117 face_from_cell_projection, \
118 face_from_cell_projection_scaling = matrices.get_face_from_cell_matrix()
119 cell_from_face_projection, \
120 cell_from_face_projection_scaling = matrices.get_cell_from_face_matrix()
121 approximate_system_matrix, \
122 approximate_system_matrix_scaling = matrices.get_A_tilde()
123
124
125 solver = mghype.matrixfree.solvers.api.DiscontinuousGalerkinDiscretisationPointWiseRiemannSolver(
126 name = "DGPoisson",
127 dimensions = args.dimensions,
128 poly_degree = args.poly_degree,
129 unknowns_per_cell_node = 1,
130 solutions_per_face_node = 2,
131 projections_per_face_node = 2,
132 min_h = args.meshsize,
133 max_h = args.meshsize,
134 assembly_matrix= assembly_matrix,
135 assembly_matrix_scaling = assembly_matrix_scaling,
136 mass_matrix = mass_matrix,
137 mass_matrix_scaling = mass_matrix_scaling,
138 face_from_cell_projection = face_from_cell_projection,
139 face_from_cell_projection_scaling = face_from_cell_projection_scaling,
140 cell_from_face_projection = cell_from_face_projection,
141 cell_from_face_projection_scaling = cell_from_face_projection_scaling,
142 riemann_matrix = matrices.get_face_face_riemann_problem_matrix(),
143 boundary_matrix = matrices.get_boundary_matrix(),
144 cell_relaxation = args.omega_c,
145 face_relaxation = args.omega_f, # default here is 1, i.e. we solve exactly. See class docu
146 approximate_system_matrix = approximate_system_matrix,
147 approximate_system_matrix_scaling = approximate_system_matrix_scaling,
148 solver_tolerance = 0.01,
149 integration_points = 0.5*(matrices.get_nodal_points_1d() + 1.0) # Should be specified in [0,1] interval
150 )
151
152
153 project.add_solver(solver)
154
155 configure_and_build_project(args,project)
156 run_and_validate(args,executable)
configure_and_build_project(args, project)
Definition dg.py:31
get_args()
Definition dg.py:8
run_and_validate(args, executable)
Definition dg.py:63