def test_h1_real(): """Test h1 amg for real example.""" with ngs.TaskManager(): mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.2)) fes = ngs.H1(mesh, dirichlet=[1, 2, 3], order=1) u = fes.TrialFunction() v = fes.TestFunction() # rhs f = ngs.LinearForm(fes) f += ngs.SymbolicLFI(v) f.Assemble() # lhs a = ngs.BilinearForm(fes, symmetric=True) a += ngs.SymbolicBFI(grad(u) * grad(v)) c = ngs.Preconditioner(a, 'h1amg2') a.Assemble() solver = ngs.CGSolver(mat=a.mat, pre=c.mat) gfu = ngs.GridFunction(fes) gfu.vec.data = solver * f.vec assert_greater(solver.GetSteps(), 0) assert_less_equal(solver.GetSteps(), 4)
def do_test(rots, reo, ms=50): print('======= test 3d, lo, rots =', rots, ', reorder=', reo) sys.stdout.flush() geo, mesh = gen_beam(dim=3, maxh=0.2, lens=[7, 1, 1], nref=0, comm=ngsolve.mpi_world) V, a, f = setup_elast(mesh, rotations=rots, f_vol=ngsolve.CoefficientFunction((0, -0.005, 0)), diri="left", multidim=False, reorder=reo) print('V ndof', V.ndof) ngsolve.ngsglobals.msg_level = 5 pc_opts = {"ngs_amg_max_coarse_size": 10} if reo == "sep": pc_opts["ngs_amg_dof_blocks"] = [3, 3] if rots else [3] elif reo is not False: pc_opts["ngs_amg_dof_blocks"] = [6] if rots else [3] if rots: pc_opts["ngs_amg_rots"] = True c = ngsolve.Preconditioner(a, "ngs_amg.elast_3d", **pc_opts) Solve(a, f, c, ms=ms) print('======= completed test 3d, lo, rots =', rots, ', reorder=', reo, '\n\n') sys.stdout.flush()
def do_ho_smooth(rots, ms, pc_opts): print('test 2d, rots = ', rots) geo, mesh = gen_beam(dim = 2, maxh = 0.2, lens = [10,1], nref = 0, comm = ngsolve.mpi_world) V, a, f = setup_elast(mesh, order = 4, rotations = rots, f_vol = ngsolve.CoefficientFunction( (0, -0.005) ), diri = "left") print('V ndof', V.ndof) pc_opts["ngs_amg_rots"] = rots c = ngsolve.Preconditioner(a, "ngs_amg.elast_2d", **pc_opts) Solve(a, f, c, ms = ms)
def test_2d_lo(): geo, mesh = gen_square(maxh=0.05, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=1, diri="left|top") pc_opts = { "ngs_amg_max_coarse_size" : 5, "ngs_amg_log_level" : "none", "ngs_amg_print_log" : True } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=30)
def test_2d_bddc(): geo, mesh = gen_square(maxh=0.05, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=4, diri="right") pc_opts = {"ngs_amg_max_coarse_size": 5} c = ngsolve.Preconditioner(a, "bddc", coarsetype="ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=60)
def test_3d_bddc_v1(): sys.stdout.flush() print('\n\n---\ntest 3d bddc, vertex only WB') sys.stdout.flush() geo, mesh = gen_cube(maxh=0.25, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=4, fes_opts = { "wb_withedges" : False }, diri="top|right|left") pc_opts = { "ngs_amg_max_coarse_size" : 5, "ngs_amg_do_test" : True, "ngs_amg_log_level" : "extra", "ngs_amg_print_log" : True } c = ngsolve.Preconditioner(a, "bddc", coarsetype="ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms = 200)
def test_3d_nodalp2(): geo, mesh = gen_cube(maxh=0.3, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=2, fes_opts={"nodalp2": True}, diri="left|bot") pc_opts = { "ngs_amg_lo": False, "ngs_amg_do_test": True, "ngs_amg_max_coarse_size": 1 } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=50)
def test_2d_lo_R(): geo, mesh = gen_beam(dim=2, maxh=0.1, lens=[10, 1], nref=0, comm=ngsolve.mpi_world) V, a, f = setup_elast(mesh, rotations=True, f_vol=ngsolve.CoefficientFunction((0, -0.005)), diri="left") pc_opts = {"ngs_amg_rots": True, "ngs_amg_max_coarse_size": 20} c = ngsolve.Preconditioner(a, "ngs_amg.elast_2d", **pc_opts) Solve(a, f, c, ms=50)
def test_3d_ho_nodalp2(): geo, mesh = gen_cube(maxh=0.3, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=5, fes_opts={"nodalp2": True}, diri="left|right") pc_opts = { "ngs_amg_on_dofs": "select", "ngs_amg_do_test": True, "ngs_amg_subset": "nodalp2", "ngs_amg_max_coarse_size": 1 } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=280)
def construct_preconditioner( self, a_assembled: ngs.BilinearForm) -> ngs.Preconditioner: """ Function to construct the preconditioner. Args: a_assembled: The Bilinear form. Returns: precond: The preconditioner. """ precond = ngs.Preconditioner(a_assembled, self.preconditioner) return precond
def test_3d_lo(): geo, mesh = gen_beam(dim=3, maxh=0.25, lens=[10, 1, 1], nref=0, comm=ngsolve.mpi_world) V, a, f = setup_elast(mesh, rotations=False, f_vol=ngsolve.CoefficientFunction( (0, -0.005, 0.002)), diri="left") pc_opts = {"ngs_amg_reg_rmats": True, "ngs_amg_max_coarse_size": 10} c = ngsolve.Preconditioner(a, "ngs_amg.elast_3d", **pc_opts) Solve(a, f, c, ms=40)
def test_3d_bddc_v2(): sys.stdout.flush() print('\n\n---\ntest 3d bddc, vertex+edge WB, ho smoothing') sys.stdout.flush() geo, mesh = gen_cube(maxh=0.4, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=4, beta=1, diri="")#diri="right|left") pc_opts = { "ngs_amg_max_coarse_size" : 5, "ngs_amg_do_test" : True, # note: ngs_amg - test includes edge-bubbles, so its already bad "ngs_amg_log_level" : "extra", "ngs_amg_print_log" : True, "ngs_amg_clev" : "inv", "ngs_amg_max_levels" : 20 } c = ngsolve.Preconditioner(a, "bddc", coarsetype="ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms = 250)
def test_3d_bddc_nodalp2(): sys.stdout.flush() print('\n\n---\ntest 3d bddc, vertex+edge WB, nodalp2') sys.stdout.flush() geo, mesh = gen_cube(maxh=0.25, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=4, fes_opts = {"nodalp2" : True}, diri="top|left") pc_opts = { "ngs_amg_on_dofs" : "select", "ngs_amg_do_test" : True, "ngs_amg_log_level" : "extra", "ngs_amg_print_log" : True, "ngs_amg_subset" : "free", "ngs_amg_max_coarse_size" : 5 } c = ngsolve.Preconditioner(a, "bddc", coarsetype = "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms = 100)
def do_test_2d_ho(jump): print('test sq_with_sqs, jump =', jump) sys.stdout.flush() geo, mesh = gen_sq_with_sqs(maxh=0.1, nref=0, comm=ngsolve.mpi_world) a0 = 1 alpha = {"mat_a": a0, "mat_b": jump * a0} V, a, f = setup_poisson(mesh, order=3, diri="outer_.*", alpha=ngsolve.CoefficientFunction( [alpha[name] for name in mesh.GetMaterials()])) pc_opts = { "ngs_amg_max_coarse_size": 5, "ngs_amg_log_level": "extra", "ngs_amg_print_hog": True } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=100, tol=1e-6, nocb=False)
def do_test_2d_lo_fiber(jump, ms=25): print('\n\n---\ntest 2d fibers, jump =', jump, '\n---') sys.stdout.flush() geo, mesh = gen_fibers_2d(maxh=0.1, nref=1, comm=ngsolve.mpi_world) a0 = 1 alpha = {"mat_a": a0, "mat_b": jump * a0} V, a, f = setup_poisson(mesh, order=1, diri="outer_top|outer_bot", alpha=ngsolve.CoefficientFunction( [alpha[name] for name in mesh.GetMaterials()])) pc_opts = { "ngs_amg_max_coarse_size": 5, "ngs_amg_sm_type": "bgs", "ngs_amg_log_level": "extra", "ngs_amg_print_log": True } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=ms, tol=1e-6)
def do_test_2d_lo(jump, ms=25): print('\n\n---\ntest sq_with_sqs, jump =', jump, '\n---') sys.stdout.flush() geo, mesh = gen_sq_with_sqs(maxh=0.05, nref=1, comm=ngsolve.mpi_world) a0 = 1 alpha = {"mat_a": a0, "mat_b": jump * a0} V, a, f = setup_poisson(mesh, order=1, diri="outer_bot|outer_top|outer_left|outer_right", \ alpha = ngsolve.CoefficientFunction([alpha[name] for name in mesh.GetMaterials()]) ) pc_opts = { "ngs_amg_max_coarse_size": 5, "ngs_amg_log_level": "extra", "ngs_amg_crs_alg": "agg", "ngs_amg_agg_wt_geom": True, "ngs_amg_enable_disc": False, #"ngs_amg_max_levels" : 4, "ngs_amg_enable_sp": True, "ngs_amg_print_log": True } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=ms, tol=1e-6)
def do_test_2d_lo(jump, rots, ms=40): geo, mesh = gen_sq_with_sqs(maxh=0.1, nref=0, comm=ngsolve.mpi_world) a0 = 1 mu = {"mat_a": a0, "mat_b": jump * a0} V, a, f = setup_elast(mesh, rotations=rots, f_vol=ngsolve.CoefficientFunction((0, -0.005)), diri="outer_left", mu=ngsolve.CoefficientFunction( [mu[name] for name in mesh.GetMaterials()])) print('V ndof', V.ndof) pc_opts = { "ngs_amg_max_coarse_size": 10, "ngs_amg_log_level": "extra", "ngs_amg_print_log": True } if rots: pc_opts["ngs_amg_rots"] = True c = ngsolve.Preconditioner(a, "ngs_amg.elast_2d", **pc_opts) Solve(a, f, c, ms=50)
def test_2d_ho(): geo, mesh = gen_square(maxh=0.05, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=3, diri="left|top") pc_opts = { "ngs_amg_max_coarse_size" : 5 } c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=40)
mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.2)) fes = ngs.H1(mesh, dirichlet=[1, 2, 3], order=1) u = fes.TrialFunction() v = fes.TestFunction() # rhs f = ngs.LinearForm(fes) f += ngs.SymbolicLFI(v) # lhs a = ngs.BilinearForm(fes, symmetric=True) a += ngs.SymbolicBFI(grad(u) * grad(v)) c = ngs.Preconditioner(a, 'h1amg', test=True) gfu = ngs.GridFunction(fes) bvp = ngs.BVP(bf=a, lf=f, gf=gfu, pre=c) while True: fes.Update() gfu.Update() a.Assemble() f.Assemble() bvp.Do() ngs.Draw(gfu, mesh, 'solution') input('Hit enter for refinement')
def solve(self): # disable garbage collector # --------------------------------------------------------------------# gc.disable() while (gc.isenabled()): time.sleep(0.1) # --------------------------------------------------------------------# # measure how much memory is used until here process = psutil.Process() memstart = process.memory_info().vms # starts timer tstart = time.time() if self.show_gui: import netgen.gui # create mesh with initial size 0.1 self._mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.1)) #create finite element space self._fes = ngs.H1(self._mesh, order=2, dirichlet=".*", autoupdate=True) # test and trail function u = self._fes.TrialFunction() v = self._fes.TestFunction() # create bilinear form and enable static condensation self._a = ngs.BilinearForm(self._fes, condense=True) self._a += ngs.grad(u) * ngs.grad(v) * ngs.dx # creat linear functional and apply RHS self._f = ngs.LinearForm(self._fes) self._f += (-4) * v * ngs.dx # preconditioner: multigrid - what prerequisits must the problem have? self._c = ngs.Preconditioner(self._a, "multigrid") # create grid function that holds the solution and set the boundary to 0 self._gfu = ngs.GridFunction(self._fes, autoupdate=True) # solution self._g = self._ngs_ex self._gfu.Set(self._g, definedon=self._mesh.Boundaries(".*")) # draw grid function in gui if self.show_gui: ngs.Draw(self._gfu) # create Hcurl space for flux calculation and estimate error self._space_flux = ngs.HDiv(self._mesh, order=2, autoupdate=True) self._gf_flux = ngs.GridFunction(self._space_flux, "flux", autoupdate=True) # TaskManager starts threads that (standard thread nr is numer of cores) with ngs.TaskManager(): # this is the adaptive loop while self._fes.ndof < self.max_ndof: self._solveStep() self._estimateError() self._mesh.Refine() # since the adaptive loop stopped with a mesh refinement, the gfu must be # calculated one last time self._solveStep() if self.show_gui: ngs.Draw(self._gfu) # set measured exectution time self._exec_time = time.time() - tstart # set measured used memory memstop = process.memory_info().vms - memstart self._mem_consumption = memstop # enable garbage collector # --------------------------------------------------------------------# gc.enable() gc.collect()
def do_test(rots=False, nodalp2=False, ms=100, order=3, reo=False, use_bddc=False, ho_wb=False): print('======= test 3d, ho, rots =', rots, ', reorder=', reo, ', nodalp2=', nodalp2, ', use_bddc=', use_bddc, ', order=', order, 'ho_wb=', ho_wb, "ms=", ms) # if reo is not False: # if (order==2) and (nodalp2 is False): # raise "order 2 + reorder only with nodalp2 (ho smoothing + reorder does now work)" # elif order>=3: # raise "ho smoothing does not work with reorder!" # if use_bddc and order==2 and nodalp2: # raise "bddc makes no sense here!" if not rots and reo == "sep": # "sep" and True are the same here return sys.stdout.flush() geo, mesh = gen_beam(dim=3, maxh=0.4, lens=[4, 1, 1], nref=0, comm=ngsolve.mpi_world) # geo, mesh = gen_beam(dim=3, maxh=0.3, lens=[5,1,1], nref=0, comm=ngsolve.mpi_world) fes_opts = dict() if nodalp2: if order >= 2: fes_opts["nodalp2"] = True elif use_bddc and not ho_wb: # do not take edge-bubbles to crs space fes_opts["wb_withedges"] = False V, a, f = setup_elast(mesh, order=order, fes_opts=fes_opts, rotations=rots, f_vol=ngsolve.CoefficientFunction((0, -0.005, 0)), diri="left", multidim=False, reorder=reo) ngsolve.ngsglobals.msg_level = 5 pc_opts = { "ngs_amg_max_coarse_size": 15, "ngs_amg_do_test": True, "ngs_amg_agg_wt_geom": False } # if nodalp2: # pc_opts["ngs_amg_crs_alg"] = "ecol" # rotations or no rotataions? if rots: pc_opts["ngs_amg_rots"] = True # subset of the low order space if use_bddc: if nodalp2 or order == 1 or not ho_wb: pc_opts["ngs_amg_on_dofs"] = "select" pc_opts["ngs_amg_subset"] = "free" else: # pc_opts["ngs_amg_on_dofs"] = "range" # per default ! # pc_opts["ngs_amg_lo"] = True # per default ! pass elif nodalp2: if order == 2: pc_opts["ngs_amg_lo"] = False else: pc_opts["ngs_amg_on_dofs"] = "select" pc_opts["ngs_amg_subset"] = "nodalp2" # ordering of DOFs within subset if reo is not False: if not rots: pc_opts["ngs_amg_dof_blocks"] = [3] elif reo == "sep": pc_opts["ngs_amg_dof_blocks"] = [3, 3] else: pc_opts["ngs_amg_dof_blocks"] = [6] if order >= 2 and nodalp2: pc_opts["ngs_amg_edge_thresh"] = 0.02 print("pc_opts: ", pc_opts) sys.stdout.flush() if use_bddc: c = ngsolve.Preconditioner(a, "bddc", coarsetype="ngs_amg.elast_3d", **pc_opts) else: c = ngsolve.Preconditioner(a, "ngs_amg.elast_3d", **pc_opts) Solve(a, f, c, ms=ms) print('======= completed test 3d, ho, rots =', rots, ', reorder=', reo, ', nodalp2=', nodalp2, ', use_bddc=', use_bddc, ', order=', order, 'ho_wb=', ho_wb, "ms=", ms) sys.stdout.flush()
# \mathsf{A}-k^2 \mathsf{M} & -\mathsf{M}_\Gamma\\ # \tfrac{1}{2}\mathsf{Id}-\mathsf{K} & \mathsf{V} # \end{bmatrix}. # $$ # In[8]: from scipy.sparse.linalg.interface import LinearOperator blocks = [[None, None], [None, None]] trace_op = LinearOperator(trace_matrix.shape, lambda x: trace_matrix * x) blfA = ngs.BilinearForm(ng_space) blfA += ngs.SymbolicBFI(ngs.grad(u) * ngs.grad(v) - k**2 * n**2 * u * v) c = ngs.Preconditioner(blfA, type="direct") blfA.Assemble() blocks[0][0] = NgOperator(blfA) blocks[0][1] = -trace_matrix.T * mass.weak_form().sparse_operator blocks[1][0] = (.5 * id_op - dlp).weak_form() * trace_op blocks[1][1] = slp.weak_form() blocked = bempp.api.BlockedDiscreteOperator(np.array(blocks)) # Next, we solve the system, then split the solution into the parts assosiated with u and λ. For an efficient solve, preconditioning is required. # In[9]: from scipy.sparse.linalg import LinearOperator
pc_opts["ngs_amg_lo"] = False else: pc_opts["ngs_amg_on_dofs"] = "select" pc_opts["ngs_amg_subset"] = "nodalp2" # ordering of DOFs within subset if reo is not False: if not rots: pc_opts["ngs_amg_dof_blocks"] = [3] elif reo == "sep": pc_opts["ngs_amg_dof_blocks"] = [3,3] else: pc_opts["ngs_amg_dof_blocks"] = [6] print("pc_opts: ", pc_opts) sys.stdout.flush() if use_bddc: c = ngsolve.Preconditioner(a, "bddc", coarsetype="ngs_amg.elast3d", **pc_opts) else: c = ngsolve.Preconditioner(a, "ngs_amg.elast3d", **pc_opts) Solve(a, f, c, ms=ms) print('======= completed test 3d, ho, rots =', rots, ', reorder=', reo, ', nodalp2=', nodalp2, ', use_bddc=', use_bddc, ', order=', order, 'ho_wb=', ho_wb) sys.stdout.flush()
def test_3d_ho(): geo, mesh = gen_cube(maxh=0.3, nref=0, comm=ngsolve.mpi_world) V, a, f = setup_poisson(mesh, order=5, diri="right|top") pc_opts = {"ngs_amg_max_coarse_size": 1, "ngs_amg_do_test": True} c = ngsolve.Preconditioner(a, "ngs_amg.h1_scal", **pc_opts) Solve(a, f, c, ms=250)
mesh = ngs.Mesh(unit_square.GenerateMesh(maxh=0.15)) fes = ngs.H1(mesh, dirichlet=[1, 2, 3], order=1, complex=True) u = fes.TrialFunction() v = fes.TestFunction() # rhs f = ngs.LinearForm(fes) f += ngs.SymbolicLFI(v) # lhs a = ngs.BilinearForm(fes, symmetric=True) a += ngs.SymbolicBFI(grad(u) * grad(v) + 1j * u * v) c = ngs.Preconditioner(a, 'h1amg2') # c = ngs.Preconditioner(a, 'direct') gfu = ngs.GridFunction(fes) bvp = ngs.BVP(bf=a, lf=f, gf=gfu, pre=c) while True: fes.Update() gfu.Update() a.Assemble() f.Assemble() bvp.Do() ngs.Draw(gfu, mesh, 'solution')
import ngsolve, ngs_amg, sys from amg_utils import * ro = False rots = False print('======= test 3d, lo, rots =', rots, ', reorder=', reo) sys.stdout.flush() geo, mesh = gen_beam(dim=3, maxh=0.2, lens=[7,1,1], nref=0, comm=ngsolve.mpi_world) V, a, f = setup_elast(mesh, rotations = rots, f_vol = ngsolve.CoefficientFunction( (0, -0.005, 0) ), diri="left", multidim = False, reorder = reo) print('V ndof', V.ndof) ngsolve.ngsglobals.msg_level = 5 pc_opts = { "ngs_amg_max_coarse_size" : 10 } if reo == "sep": pc_opts["ngs_amg_dof_blocks"] = [3,3] if rots else [3] elif reo is not False: pc_opts["ngs_amg_dof_blocks"] = [6] if rots else [3] if rots: pc_opts["ngs_amg_rots"] = True c = ngsolve.Preconditioner(a, "ngs_amg.elast3d", **pc_opts) Solve(a, f, c, ms=ms) print('======= completed test 3d, lo, rots =', rots, ', reorder=', reo, '\n\n') sys.stdout.flush()
mesh = ngs.Mesh(square_conductivity.GenerateMesh(maxh=0.05)) fes = ngs.H1(mesh, dirichlet='dirichlet', order=1) u = fes.TrialFunction() v = fes.TestFunction() # rhs f = ngs.LinearForm(fes) f += ngs.SymbolicLFI(v) # lhs lam = ngs.DomainConstantCF([1, 1000]) a = ngs.BilinearForm(fes, symmetric=True) a += ngs.SymbolicBFI(lam * grad(u) * grad(v)) c = ngs.Preconditioner(a, 'h1amg', flags={'test': True, 'levels': 10}) gfu = ngs.GridFunction(fes) bvp = ngs.BVP(bf=a, lf=f, gf=gfu, pre=c) while True: fes.Update() gfu.Update() a.Assemble() f.Assemble() bvp.Do() ngs.Draw(gfu, mesh, 'solution') input('Hit enter for refinement')