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