Peano
Loading...
Searching...
No Matches
test3.py
Go to the documentation of this file.
1"""
2This test checks that the CG solver converges to u_random for the RHS set as b_c = A_CG * u_random with some vector u_random.
3It shows that the solution u of the problem
4 A * u = A * u_random
5approximates u_random. u_random has to satisfy the boundary conditions but can be random at the interior vertices.
6
7Algorithm:
8
91. Generate u_random by assigning a value from [0,1] to each vertex.
102. Set u_random to 0 at the boundary vertices to satisfy the boundary conditions.
113. Compute b_c = A_CG * u_random, where A_CG is the system matrix for CG.
124. Solve A_CG * u = b_c with CollocatedSolver.
135. Check |u - u_random| < tol.
14"""
15
16import os, sys
17import argparse
18import peano4.api
19import numpy as np
20import mghype
21
22from time import sleep
23
24# custom action set
25from actionsets.initCollocatedRandom import InitDofsCollocatedRandomRhs
26
27print( """
28 WARNING: implementation file (eg CollocatedPoisson.cpp) must be removed before
29 running this script if you want custom behaviour.
30
31""")
32
33sleep(2)
34
35parser = argparse.ArgumentParser(description='Multigrid - Poisson solver')
36parser.add_argument("-j", "--parallel-builds", dest="j", type=int, default=-1, help="Parallel builds" )
37parser.add_argument("-pt", "--plot-timestep", 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" )
38parser.add_argument("-pd", "--peano-dir", dest="peanodir", default="../../../../", help="Peano4 directory" )
39parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose")
40parser.add_argument("-d", "--dimensions", dest="dimensions", type=int, default=2, help="Dimensions")
41parser.add_argument("-meshsize", "--meshsize", dest="meshsize", default=0.3, help="Mesh size")
42parser.add_argument("-m", "--mode", dest="mode", choices=["release","stats","asserts", "trace"], required=True, help="Pick build type" )
43args = parser.parse_args()
44
45project = mghype.matrixfree.api.Project( project_name = "CollocatedFEM",
46 namespace = [ "tests", "multigrid", "matrixfree", "verifyRandom" ]
47 )
48
49#! [Construct matrices]
50# poisson_equation_matrices = mghype.api.matrixgenerators.DLinear(
51# dimensions = args.dimensions,
52# poly_degree = 1,
53# unknowns_per_vertex_dof = 1
54# )
56 dimensions = args.dimensions,
57 poly_degree = 1,
58 unknowns_per_node = 1
59)
60
61system_matrix, system_matrix_scaling = poisson_equation_matrices.get_cell_system_matrix_for_laplacian()
62mass_matrix, mass_matrix_scaling = poisson_equation_matrices.get_cell_mass_matrix()
63#! [Construct matrices]
64
65solver = mghype.matrixfree.solvers.api.CollocatedDLinearDiscretisationWithPointJacobi("CollocatedPoisson",
66 1, # unknowns
67 args.dimensions,
68 args.meshsize,
69 args.meshsize,
70 local_assembly_matrix = system_matrix,
71 local_assembly_matrix_scaling=system_matrix_scaling,
72 mass_matrix = mass_matrix,
73 mass_matrix_scaling = mass_matrix_scaling,
74 solver_tolerance=1e-9,
75 custom_init_actionset=InitDofsCollocatedRandomRhs,
76 custom_plot_actionset=[("rand","getRand")]
77 )
78
79# need to make sure we have the correct field name to support extra random field
80solver.add_to_vertex_data("rand", 1)
81
82project.add_solver(solver)
83
84#
85# Configure build
86#
87if args.mode=="release":
88 build_mode = peano4.api.output.CompileMode.RELEASE
89if args.mode=="stats":
90 build_mode = peano4.api.output.CompileMode.STATS
91if args.mode=="asserts":
92 build_mode = peano4.api.output.CompileMode.ASSERTS
93if args.mode=="trace":
94 build_mode = peano4.api.output.CompileMode.TRACE
95
96cube_size = 1.0
97project.set_global_simulation_parameters(
98 dimensions = args.dimensions,
99 offset = [ 0.0 for _ in range(args.dimensions) ],
100 domain_size = [ cube_size for _ in range(args.dimensions) ],
101 plot_after_each_mesh_sweep = args.plot_after_each_mesh_sweep,
102 max_iterations = 5000
103)
104
105project.set_load_balancing( "toolbox::loadbalancing::RecursiveSubdivision", "new ::exahype2::LoadBalancingConfiguration()" )
106project.set_Peano4_installation( args.peanodir, build_mode )
107
108peano4_project = project.generate_Peano4_project(args.verbose)
109peano4_project.generate()
110
111os.system( "make clean" )
112if args.j>0:
113 os.system( "make -j{}".format(args.j) )
114else:
115 os.system( "make -j" )
116
117print("Done")
118
Using normalised identity matrix instead of the mass matrix.