MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
acoustics.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11//
12// MFEM Ultraweak DPG example for acoustics (Helmholtz)
13//
14// Compile with: make acoustics
15//
16// Sample runs
17//
18// acoustics -ref 4 -o 1 -rnum 1.0
19// acoustics -m ../../data/inline-tri.mesh -ref 4 -o 2 -sc -rnum 3.0
20// acoustics -m ../../data/amr-quad.mesh -ref 3 -o 3 -sc -rnum 4.5 -prob 1
21// acoustics -m ../../data/inline-quad.mesh -ref 2 -o 4 -sc -rnum 11.5 -prob 1
22// acoustics -m ../../data/inline-hex.mesh -ref 1 -o 2 -sc -rnum 1.0
23
24// Description:
25// This example code demonstrates the use of MFEM to define and solve
26// the "ultraweak" (UW) DPG formulation for the Helmholtz problem
27
28// - Δ p - ω² p = f̃ , in Ω
29// p = p₀, on ∂Ω
30
31// It solves two kinds of problems
32// a) f̃ = 0 and p₀ is a plane wave
33// b) A manufactured solution problem where p_exact is a gaussian beam
34// This example computes and prints out convergence rates for the L² error.
35
36// The DPG UW deals with the First Order System
37// ∇ p + i ω u = 0, in Ω
38// ∇⋅u + i ω p = f, in Ω (1)
39// p = p_0, in ∂Ω
40// where f:=f̃/(i ω)
41
42// Ultraweak-DPG is obtained by integration by parts of both equations and the
43// introduction of trace unknowns on the mesh skeleton
44//
45// p ∈ L²(Ω), u ∈ (L²(Ω))ᵈⁱᵐ
46// p̂ ∈ H^1/2(Ω), û ∈ H^-1/2(Ω)
47// -(p, ∇⋅v) + i ω (u , v) + < p̂, v⋅n> = 0, ∀ v ∈ H(div,Ω)
48// -(u , ∇ q) + i ω (p , q) + < û, q > = (f,q) ∀ q ∈ H¹(Ω)
49// p̂ = p₀ on ∂Ω
50
51// Note:
52// p̂ := p, û := u on the mesh skeleton
53
54// For more information see https://doi.org/10.1016/j.camwa.2017.06.044
55
56// -------------------------------------------------------------
57// | | p | u | p̂ | û | RHS |
58// -------------------------------------------------------------
59// | v | -(p, ∇⋅v) | i ω (u,v) | < p̂, v⋅n> | | |
60// | | | | | | |
61// | q | i ω (p,q) |-(u , ∇ q) | | < û,q > | (f,q) |
62
63// where (q,v) ∈ H¹(Ω) × H(div,Ω)
64
65// Here we use the "Adjoint Graph" norm on the test space i.e.,
66// ||(q,v)||²ᵥ = ||A^*(q,v)||² + ||(q,v)||² where A is the
67// acoustics operator defined by (1)
68
69#include "mfem.hpp"
72#include <fstream>
73#include <iostream>
74
75using namespace std;
76using namespace mfem;
77using namespace mfem::common;
78
79complex<real_t> acoustics_solution(const Vector & X);
80void acoustics_solution_grad(const Vector & X,vector<complex<real_t>> &dp);
81complex<real_t> acoustics_solution_laplacian(const Vector & X);
82
83real_t p_exact_r(const Vector &x);
84real_t p_exact_i(const Vector &x);
85void u_exact_r(const Vector &x, Vector & u);
86void u_exact_i(const Vector &x, Vector & u);
87real_t rhs_func_r(const Vector &x);
88real_t rhs_func_i(const Vector &x);
89void gradp_exact_r(const Vector &x, Vector &gradu);
90void gradp_exact_i(const Vector &x, Vector &gradu);
91real_t divu_exact_r(const Vector &x);
92real_t divu_exact_i(const Vector &x);
93real_t d2_exact_r(const Vector &x);
94real_t d2_exact_i(const Vector &x);
95real_t hatp_exact_r(const Vector & X);
96real_t hatp_exact_i(const Vector & X);
97void hatu_exact_r(const Vector & X, Vector & hatu);
98void hatu_exact_i(const Vector & X, Vector & hatu);
99
100int dim;
102
108
110
111int main(int argc, char *argv[])
112{
113 const char *mesh_file = "../../data/inline-quad.mesh";
114 int order = 1;
115 int delta_order = 1;
116 bool visualization = true;
117 real_t rnum=1.0;
118 int ref = 0;
119 bool static_cond = false;
120 int visport = 19916;
121 int iprob = 0;
122
123 OptionsParser args(argc, argv);
124 args.AddOption(&mesh_file, "-m", "--mesh",
125 "Mesh file to use.");
126 args.AddOption(&order, "-o", "--order",
127 "Finite element order (polynomial degree)");
128 args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
129 "--no-visualization",
130 "Enable or disable GLVis visualization.");
131 args.AddOption(&rnum, "-rnum", "--number_of_wavelengths",
132 "Number of wavelengths");
133 args.AddOption(&iprob, "-prob", "--problem", "Problem case"
134 " 0: plane wave, 1: Gaussian beam");
135 args.AddOption(&delta_order, "-do", "--delta_order",
136 "Order enrichment for DPG test space.");
137 args.AddOption(&ref, "-ref", "--refinements",
138 "Number of serial refinements.");
139 args.AddOption(&static_cond, "-sc", "--static-condensation", "-no-sc",
140 "--no-static-condensation", "Enable static condensation.");
141 args.AddOption(&visport, "-p", "--send-port", "Socket for GLVis.");
142 args.Parse();
143 if (!args.Good())
144 {
145 args.PrintUsage(cout);
146 return 1;
147 }
148 args.PrintOptions(cout);
149
150 if (iprob > 1) { iprob = 0; }
151 prob = (prob_type)iprob;
152
153 omega = 2.*M_PI*rnum;
154
155 Mesh mesh(mesh_file, 1, 1);
156 dim = mesh.Dimension();
157 MFEM_VERIFY(dim > 1, "Dimension = 1 is not supported in this example");
158
159 // Define spaces
160 enum TrialSpace
161 {
162 p_space = 0,
163 u_space = 1,
164 hatp_space = 2,
165 hatu_space = 3
166 };
167 enum TestSpace
168 {
169 q_space = 0,
170 v_space = 1
171 };
172
173 // L2 space for p
174 FiniteElementCollection *p_fec = new L2_FECollection(order-1,dim);
175 FiniteElementSpace *p_fes = new FiniteElementSpace(&mesh,p_fec);
176
177 // Vector L2 space for u
178 FiniteElementCollection *u_fec = new L2_FECollection(order-1,dim);
179 FiniteElementSpace *u_fes = new FiniteElementSpace(&mesh,u_fec, dim);
180
181 // H^1/2 space for p̂
182 FiniteElementCollection * hatp_fec = new H1_Trace_FECollection(order,dim);
183 FiniteElementSpace *hatp_fes = new FiniteElementSpace(&mesh,hatp_fec);
184
185 // H^-1/2 space for û
186 FiniteElementCollection * hatu_fec = new RT_Trace_FECollection(order-1,dim);
187 FiniteElementSpace *hatu_fes = new FiniteElementSpace(&mesh,hatu_fec);
188
189 // testspace fe collections
190 int test_order = order+delta_order;
191 FiniteElementCollection * q_fec = new H1_FECollection(test_order, dim);
192 FiniteElementCollection * v_fec = new RT_FECollection(test_order-1, dim);
193
194 // Coefficients
195 ConstantCoefficient one(1.0);
196 ConstantCoefficient zero(0.0);
197 Vector vec0(dim); vec0 = 0.;
198 VectorConstantCoefficient vzero(vec0);
199 ConstantCoefficient negone(-1.0);
202 ConstantCoefficient negomeg(-omega);
203
206
207 trial_fes.Append(p_fes);
208 trial_fes.Append(u_fes);
209 trial_fes.Append(hatp_fes);
210 trial_fes.Append(hatu_fes);
211
212 test_fec.Append(q_fec);
213 test_fec.Append(v_fec);
214
215 ComplexDPGWeakForm * a = new ComplexDPGWeakForm(trial_fes,test_fec);
216
217 // i ω (p,q)
218 a->AddTrialIntegrator(nullptr,new MixedScalarMassIntegrator(omeg),
219 TrialSpace::p_space,TestSpace::q_space);
220 // -(u , ∇ q)
221 a->AddTrialIntegrator(new TransposeIntegrator(new GradientIntegrator(negone)),
222 nullptr,TrialSpace::u_space,TestSpace::q_space);
223 // -(p, ∇⋅v)
224 a->AddTrialIntegrator(new MixedScalarWeakGradientIntegrator(one),nullptr,
225 TrialSpace::p_space,TestSpace::v_space);
226 // i ω (u,v)
227 a->AddTrialIntegrator(nullptr,
229 TrialSpace::u_space,TestSpace::v_space);
230 // < p̂, v⋅n>
231 a->AddTrialIntegrator(new NormalTraceIntegrator,nullptr,
232 TrialSpace::hatp_space,TestSpace::v_space);
233 // < û,q >
234 a->AddTrialIntegrator(new TraceIntegrator,nullptr,
235 TrialSpace::hatu_space,TestSpace::q_space);
236
237 // test space integrators (Adjoint graph norm)
238 // (∇q,∇δq)
239 a->AddTestIntegrator(new DiffusionIntegrator(one),nullptr,
240 TestSpace::q_space, TestSpace::q_space);
241 // (q,δq)
242 a->AddTestIntegrator(new MassIntegrator(one),nullptr,
243 TestSpace::q_space, TestSpace::q_space);
244 // (∇⋅v,∇⋅δv)
245 a->AddTestIntegrator(new DivDivIntegrator(one),nullptr,
246 TestSpace::v_space, TestSpace::v_space);
247 // (v,δv)
248 a->AddTestIntegrator(new VectorFEMassIntegrator(one),nullptr,
249 TestSpace::v_space, TestSpace::v_space);
250 // -i ω (∇q,δv)
251 a->AddTestIntegrator(nullptr,new MixedVectorGradientIntegrator(negomeg),
252 TestSpace::q_space, TestSpace::v_space);
253 // i ω (v,∇ δq)
254 a->AddTestIntegrator(nullptr,new MixedVectorWeakDivergenceIntegrator(negomeg),
255 TestSpace::v_space, TestSpace::q_space);
256 // ω^2 (v,δv)
257 a->AddTestIntegrator(new VectorFEMassIntegrator(omeg2),nullptr,
258 TestSpace::v_space, TestSpace::v_space);
259 // - i ω (∇⋅v,δq)
260 a->AddTestIntegrator(nullptr,new VectorFEDivergenceIntegrator(negomeg),
261 TestSpace::v_space, TestSpace::q_space);
262 // i ω (q,∇⋅v)
263 a->AddTestIntegrator(nullptr,new MixedScalarWeakGradientIntegrator(negomeg),
264 TestSpace::q_space, TestSpace::v_space);
265 // ω^2 (q,δq)
266 a->AddTestIntegrator(new MassIntegrator(omeg2),nullptr,
267 TestSpace::q_space, TestSpace::q_space);
268
269 // RHS
272 if (prob == prob_type::gaussian_beam)
273 {
274 a->AddDomainLFIntegrator(new DomainLFIntegrator(f_rhs_r),
275 new DomainLFIntegrator(f_rhs_i), TestSpace::q_space);
276 }
277
282
283 socketstream p_out_r;
284 socketstream p_out_i;
285
286 real_t err0 = 0.;
287 int dof0 = 0; // init to suppress gcc warning
288
289 std::cout << "\n Ref |"
290 << " Dofs |"
291 << " ω |"
292 << " L2 Error |"
293 << " Rate |"
294 << " PCG it |" << endl;
295 std::cout << std::string(60,'-')
296 << endl;
297
298 for (int it = 0; it<=ref; it++)
299 {
300 if (static_cond) { a->EnableStaticCondensation(); }
301 a->Assemble();
302
303 Array<int> ess_tdof_list;
304 Array<int> ess_bdr;
305 if (mesh.bdr_attributes.Size())
306 {
307 ess_bdr.SetSize(mesh.bdr_attributes.Max());
308 ess_bdr = 1;
309 hatp_fes->GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
310 }
311
312 // shift the ess_tdofs
313 for (int j = 0; j < ess_tdof_list.Size(); j++)
314 {
315 ess_tdof_list[j] += p_fes->GetTrueVSize() + u_fes->GetTrueVSize();
316 }
317
318 Array<int> offsets(5);
319 offsets[0] = 0;
320 offsets[1] = p_fes->GetVSize();
321 offsets[2] = u_fes->GetVSize();
322 offsets[3] = hatp_fes->GetVSize();
323 offsets[4] = hatu_fes->GetVSize();
324 offsets.PartialSum();
325
326 Vector x(2*offsets.Last());
327 x = 0.;
328
329 GridFunction hatp_gf_r(hatp_fes, x, offsets[2]);
330 GridFunction hatp_gf_i(hatp_fes, x, offsets.Last()+ offsets[2]);
331 hatp_gf_r.ProjectBdrCoefficient(hatpex_r, ess_bdr);
332 hatp_gf_i.ProjectBdrCoefficient(hatpex_i, ess_bdr);
333
334 OperatorPtr Ah;
335 Vector X,B;
336 a->FormLinearSystem(ess_tdof_list,x,Ah, X,B);
337
338 // Setup operator and preconditioner
339 ComplexOperator * Ahc = Ah.As<ComplexOperator>();
340
341 BlockMatrix * A_r = dynamic_cast<BlockMatrix *>(&Ahc->real());
342 BlockMatrix * A_i = dynamic_cast<BlockMatrix *>(&Ahc->imag());
343
344 int num_blocks = A_r->NumRowBlocks();
345 Array<int> tdof_offsets(2*num_blocks+1);
346 tdof_offsets[0] = 0;
347 int k = (static_cond) ? 2 : 0;
348 for (int i=0; i<num_blocks; i++)
349 {
350 tdof_offsets[i+1] = trial_fes[i+k]->GetTrueVSize();
351 tdof_offsets[num_blocks+i+1] = trial_fes[i+k]->GetTrueVSize();
352 }
353 tdof_offsets.PartialSum();
354
355 BlockOperator A(tdof_offsets);
356 for (int i = 0; i<num_blocks; i++)
357 {
358 for (int j = 0; j<num_blocks; j++)
359 {
360 A.SetBlock(i,j,&A_r->GetBlock(i,j));
361 A.SetBlock(i,j+num_blocks,&A_i->GetBlock(i,j), -1.0);
362 A.SetBlock(i+num_blocks,j+num_blocks,&A_r->GetBlock(i,j));
363 A.SetBlock(i+num_blocks,j,&A_i->GetBlock(i,j));
364 }
365 }
366
367 BlockDiagonalPreconditioner M(tdof_offsets);
368 M.owns_blocks = 1;
369 for (int i = 0; i<num_blocks; i++)
370 {
371 M.SetDiagonalBlock(i, new GSSmoother((SparseMatrix&)A_r->GetBlock(i,i)));
372 M.SetDiagonalBlock(num_blocks+i, new GSSmoother((SparseMatrix&)A_r->GetBlock(i,
373 i)));
374 }
375
376 CGSolver cg;
377 cg.SetRelTol(1e-10);
378 cg.SetMaxIter(2000);
379 cg.SetPrintLevel(0);
380 cg.SetPreconditioner(M);
381 cg.SetOperator(A);
382 cg.Mult(B, X);
383
384 a->RecoverFEMSolution(X,x);
385
386 GridFunction p_r(p_fes, x, 0);
387 GridFunction p_i(p_fes, x, offsets.Last());
388
389 GridFunction u_r(u_fes, x, offsets[1]);
390 GridFunction u_i(u_fes, x, offsets.Last() + offsets[1]);
391
394
397
398 int dofs = 0;
399 for (int i = 0; i<trial_fes.Size(); i++)
400 {
401 dofs += trial_fes[i]->GetTrueVSize();
402 }
403
404 real_t p_err_r = p_r.ComputeL2Error(p_ex_r);
405 real_t p_err_i = p_i.ComputeL2Error(p_ex_i);
406 real_t u_err_r = u_r.ComputeL2Error(u_ex_r);
407 real_t u_err_i = u_i.ComputeL2Error(u_ex_i);
408
409 real_t L2Error = sqrt(p_err_r*p_err_r + p_err_i*p_err_i
410 +u_err_r*u_err_r + u_err_i*u_err_i);
411
412 real_t rate_err = (it) ? dim*log(err0/L2Error)/log((real_t)dof0/dofs) : 0.0;
413
414 err0 = L2Error;
415 dof0 = dofs;
416
417 std::ios oldState(nullptr);
418 oldState.copyfmt(std::cout);
419 std::cout << std::right << std::setw(5) << it << " | "
420 << std::setw(10) << dof0 << " | "
421 << std::setprecision(1) << std::fixed
422 << std::setw(4) << 2*rnum << " π | "
423 << std::setprecision(3)
424 << std::setw(10) << std::scientific << err0 << " | "
425 << std::setprecision(2)
426 << std::setw(6) << std::fixed << rate_err << " | "
427 << std::setw(6) << std::fixed << cg.GetNumIterations() << " | "
428 << std::endl;
429 std::cout.copyfmt(oldState);
430
431 if (visualization)
432 {
433 const char * keys = (it == 0 && dim == 2) ? "jRcml\n" : nullptr;
434 char vishost[] = "localhost";
435 VisualizeField(p_out_r,vishost, visport, p_r,
436 "Numerical presure (real part)", 0, 0, 500, 500, keys);
437 VisualizeField(p_out_i,vishost, visport, p_i,
438 "Numerical presure (imaginary part)", 501, 0, 500, 500, keys);
439 }
440
441 if (it == ref)
442 {
443 break;
444 }
445
446 mesh.UniformRefinement();
447 for (int i =0; i<trial_fes.Size(); i++)
448 {
449 trial_fes[i]->Update(false);
450 }
451 a->Update();
452 }
453
454 delete a;
455 delete q_fec;
456 delete v_fec;
457 delete hatp_fes;
458 delete hatp_fec;
459 delete hatu_fes;
460 delete hatu_fec;
461 delete u_fec;
462 delete p_fec;
463 delete u_fes;
464 delete p_fes;
465
466 return 0;
467}
468
470{
471 return acoustics_solution(x).real();
472}
473
475{
476 return acoustics_solution(x).imag();
477}
478
480{
481 return p_exact_r(X);
482}
483
485{
486 return p_exact_i(X);
487}
488
489void gradp_exact_r(const Vector &x, Vector &grad_r)
490{
491 grad_r.SetSize(x.Size());
492 vector<complex<real_t>> grad;
494 for (unsigned i = 0; i < grad.size(); i++)
495 {
496 grad_r[i] = grad[i].real();
497 }
498}
499
500void gradp_exact_i(const Vector &x, Vector &grad_i)
501{
502 grad_i.SetSize(x.Size());
503 vector<complex<real_t>> grad;
505 for (unsigned i = 0; i < grad.size(); i++)
506 {
507 grad_i[i] = grad[i].imag();
508 }
509}
510
512{
513
514 return acoustics_solution_laplacian(x).real();
515}
516
518{
519 return acoustics_solution_laplacian(x).imag();
520}
521
522// u = - ∇ p / (i ω )
523// = i (∇ p_r + i * ∇ p_i) / ω
524// = - ∇ p_i / ω + i ∇ p_r / ω
525void u_exact_r(const Vector &x, Vector & u)
526{
527 gradp_exact_i(x,u);
528 u *= -1./omega;
529}
530
531void u_exact_i(const Vector &x, Vector & u)
532{
533 gradp_exact_r(x,u);
534 u *= 1./omega;
535}
536
537void hatu_exact_r(const Vector & X, Vector & hatu)
538{
539 u_exact_r(X,hatu);
540}
541void hatu_exact_i(const Vector & X, Vector & hatu)
542{
543 u_exact_i(X,hatu);
544}
545
546// ∇⋅u = i Δ p / ω
547// = i (Δ p_r + i * Δ p_i) / ω
548// = - Δ p_i / ω + i Δ p_r / ω
550{
551 return -d2_exact_i(x)/omega;
552}
553
555{
556 return d2_exact_r(x)/omega;
557}
558
559// f = ∇⋅u + i ω p
560// f_r = ∇⋅u_r - ω p_i
562{
563 real_t p = p_exact_i(x);
564 real_t divu = divu_exact_r(x);
565 return divu - omega * p;
566}
567
568// f_i = ∇⋅u_i + ω p_r
570{
571 real_t p = p_exact_r(x);
572 real_t divu = divu_exact_i(x);
573 return divu + omega * p;
574}
575
576complex<real_t> acoustics_solution(const Vector & X)
577{
578 complex<real_t> zi = complex<real_t>(0., 1.);
579 switch (prob)
580 {
581 case plane_wave:
582 {
583 real_t beta = omega/std::sqrt((real_t)X.Size());
584 complex<real_t> alpha = beta * zi * X.Sum();
585 return exp(alpha);
586 }
587 break;
588 default:
589 {
590 real_t rk = omega;
591 real_t degrees = 45;
592 real_t alpha = (180+degrees) * M_PI/180.;
593 real_t sina = sin(alpha);
594 real_t cosa = cos(alpha);
595 // shift the origin
596 real_t xprim=X(0) + 0.1;
597 real_t yprim=X(1) + 0.1;
598
599 real_t x = xprim*sina - yprim*cosa;
600 real_t y = xprim*cosa + yprim*sina;
601 //wavelength
602 real_t rl = 2.*M_PI/rk;
603 // beam waist radius
604 real_t w0 = 0.05;
605 // function w
606 real_t fact = rl/M_PI/(w0*w0);
607 real_t aux = 1. + (fact*y)*(fact*y);
608 real_t w = w0*sqrt(aux);
609 real_t phi0 = atan(fact*y);
610 real_t r = y + 1./y/(fact*fact);
611
612 // pressure
613 complex<real_t> ze = - x*x/(w*w) - zi*rk*y - zi * real_t(M_PI) * x * x/rl/r +
614 zi*phi0/2_r;
615
616 real_t pf = pow(2.0/M_PI/(w*w),0.25);
617 return pf*exp(ze);
618 }
619 break;
620 }
621}
622
623void acoustics_solution_grad(const Vector & X, vector<complex<real_t>> & dp)
624{
625 dp.resize(X.Size());
626 complex<real_t> zi = complex<real_t>(0., 1.);
627 switch (prob)
628 {
629 case plane_wave:
630 {
631 real_t beta = omega/std::sqrt((real_t)X.Size());
632 complex<real_t> alpha = beta * zi * X.Sum();
633 complex<real_t> p = exp(alpha);
634 for (int i = 0; i<X.Size(); i++)
635 {
636 dp[i] = zi * beta * p;
637 }
638 }
639 break;
640 default:
641 {
642 real_t rk = omega;
643 real_t degrees = 45;
644 real_t alpha = (180+degrees) * M_PI/180.;
645 real_t sina = sin(alpha);
646 real_t cosa = cos(alpha);
647 // shift the origin
648 real_t xprim=X(0) + 0.1;
649 real_t yprim=X(1) + 0.1;
650
651 real_t x = xprim*sina - yprim*cosa;
652 real_t y = xprim*cosa + yprim*sina;
653 real_t dxdxprim = sina, dxdyprim = -cosa;
654 real_t dydxprim = cosa, dydyprim = sina;
655 //wavelength
656 real_t rl = 2.*M_PI/rk;
657
658 // beam waist radius
659 real_t w0 = 0.05;
660
661 // function w
662 real_t fact = rl/M_PI/(w0*w0);
663 real_t aux = 1. + (fact*y)*(fact*y);
664
665 real_t w = w0*sqrt(aux);
666 real_t dwdy = w0*fact*fact*y/sqrt(aux);
667
668 real_t phi0 = atan(fact*y);
669 real_t dphi0dy = cos(phi0)*cos(phi0)*fact;
670
671 real_t r = y + 1./y/(fact*fact);
672 real_t drdy = 1. - 1./(y*y)/(fact*fact);
673
674 constexpr real_t r2 = 2.0;
675 const real_t rPI = M_PI;
676
677 // pressure
678 complex<real_t> ze = - x*x/(w*w) - zi*rk*y - zi * rPI * x * x/rl/r +
679 zi*phi0/r2;
680
681 complex<real_t> zdedx = -r2*x/(w*w) - r2*zi*rPI*x/rl/r;
682 complex<real_t> zdedy = r2*x*x/(w*w*w)*dwdy - zi*rk + zi*rPI*x*x/rl/
683 (r*r)*drdy + zi*dphi0dy/r2;
684
685 real_t pf = pow(2.0/M_PI/(w*w),0.25);
686 real_t dpfdy = -pow(2./M_PI/(w*w),-0.75)/M_PI/(w*w*w)*dwdy;
687
688 complex<real_t> zp = pf*exp(ze);
689 complex<real_t> zdpdx = zp*zdedx;
690 complex<real_t> zdpdy = dpfdy*exp(ze)+zp*zdedy;
691
692 dp[0] = (zdpdx*dxdxprim + zdpdy*dydxprim);
693 dp[1] = (zdpdx*dxdyprim + zdpdy*dydyprim);
694 if (dim == 3) { dp[2] = 0.0; }
695 }
696 break;
697 }
698}
699
700complex<real_t> acoustics_solution_laplacian(const Vector & X)
701{
702 complex<real_t> zi = complex<real_t>(0., 1.);
703 switch (prob)
704 {
705 case plane_wave:
706 {
707 real_t beta = omega/std::sqrt((real_t)X.Size());
708 complex<real_t> alpha = beta * zi * X.Sum();
709 complex<real_t> p = exp(alpha);
710 return dim * beta * beta * p;
711 }
712 break;
713 default:
714 {
715 real_t rk = omega;
716 real_t degrees = 45;
717 real_t alpha = (180+degrees) * M_PI/180.;
718 real_t sina = sin(alpha);
719 real_t cosa = cos(alpha);
720 // shift the origin
721 real_t xprim=X(0) + 0.1;
722 real_t yprim=X(1) + 0.1;
723
724 real_t x = xprim*sina - yprim*cosa;
725 real_t y = xprim*cosa + yprim*sina;
726 real_t dxdxprim = sina, dxdyprim = -cosa;
727 real_t dydxprim = cosa, dydyprim = sina;
728 //wavelength
729 real_t rl = 2.*M_PI/rk;
730
731 // beam waist radius
732 real_t w0 = 0.05;
733
734 // function w
735 real_t fact = rl/M_PI/(w0*w0);
736 real_t aux = 1. + (fact*y)*(fact*y);
737
738 real_t w = w0*sqrt(aux);
739 real_t dwdy = w0*fact*fact*y/sqrt(aux);
740 real_t d2wdydy = w0*fact*fact*(1. - (fact*y)*(fact*y)/aux)/sqrt(aux);
741
742 real_t phi0 = atan(fact*y);
743 real_t dphi0dy = cos(phi0)*cos(phi0)*fact;
744 real_t d2phi0dydy = -2.*cos(phi0)*sin(phi0)*fact*dphi0dy;
745
746 real_t r = y + 1./y/(fact*fact);
747 real_t drdy = 1. - 1./(y*y)/(fact*fact);
748 real_t d2rdydy = 2./(y*y*y)/(fact*fact);
749
750 constexpr real_t r2 = 2.0;
751 const real_t rPI = M_PI;
752
753 // pressure
754 complex<real_t> ze = - x*x/(w*w) - zi*rk*y - zi * rPI * x * x/rl/r +
755 zi*phi0/r2;
756
757 complex<real_t> zdedx = -r2*x/(w*w) - r2*zi*rPI*x/rl/r;
758 complex<real_t> zdedy = r2*x*x/(w*w*w)*dwdy - zi*rk + zi*rPI*x*x/rl/
759 (r*r)*drdy + zi*dphi0dy/r2;
760 complex<real_t> zd2edxdx = -r2/(w*w) - r2*zi*rPI/rl/r;
761 complex<real_t> zd2edxdy = 4_r*x/(w*w*w)*dwdy + r2*zi*rPI*x/rl/(r*r)*drdy;
762 complex<real_t> zd2edydx = zd2edxdy;
763 complex<real_t> zd2edydy = -6_r*x*x/(w*w*w*w)*dwdy*dwdy + r2*x*x/
764 complex<real_t>(w*w*w)*d2wdydy - r2*zi*rPI*x*x/rl/(r*r*r)*drdy*drdy
765 + zi*rPI*x*x/rl/(r*r)*d2rdydy + zi/r2*d2phi0dydy;
766
767 real_t pf = pow(2.0/M_PI/(w*w),0.25);
768 real_t dpfdy = -pow(2./M_PI/(w*w),-0.75)/M_PI/(w*w*w)*dwdy;
769 real_t d2pfdydy = -1./M_PI*pow(2./M_PI,-0.75)*(-1.5*pow(w,-2.5)
770 *dwdy*dwdy + pow(w,-1.5)*d2wdydy);
771
772 complex<real_t> zp = pf*exp(ze);
773 complex<real_t> zdpdx = zp*zdedx;
774 complex<real_t> zdpdy = dpfdy*exp(ze)+zp*zdedy;
775 complex<real_t> zd2pdxdx = zdpdx*zdedx + zp*zd2edxdx;
776 complex<real_t> zd2pdxdy = zdpdy*zdedx + zp*zd2edxdy;
777 complex<real_t> zd2pdydx = dpfdy*exp(ze)*zdedx + zdpdx*zdedy + zp*zd2edydx;
778 complex<real_t> zd2pdydy = d2pfdydy*exp(ze) + dpfdy*exp(
779 ze)*zdedy + zdpdy*zdedy + zp*zd2edydy;
780
781
782 return (zd2pdxdx*dxdxprim + zd2pdydx*dydxprim)*dxdxprim
783 + (zd2pdxdy*dxdxprim + zd2pdydy*dydxprim)*dydxprim
784 + (zd2pdxdx*dxdyprim + zd2pdydx*dydyprim)*dxdyprim
785 + (zd2pdxdy*dxdyprim + zd2pdydy*dydyprim)*dydyprim;
786 }
787 break;
788 }
789}
real_t hatp_exact_r(const Vector &X)
void hatu_exact_i(const Vector &X, Vector &hatu)
real_t omega
real_t p_exact_r(const Vector &x)
complex< real_t > acoustics_solution(const Vector &X)
void gradp_exact_i(const Vector &x, Vector &gradu)
void gradp_exact_r(const Vector &x, Vector &gradu)
real_t rhs_func_r(const Vector &x)
complex< real_t > acoustics_solution_laplacian(const Vector &X)
real_t divu_exact_i(const Vector &x)
int dim
void u_exact_r(const Vector &x, Vector &u)
real_t divu_exact_r(const Vector &x)
real_t d2_exact_r(const Vector &x)
real_t hatp_exact_i(const Vector &X)
real_t d2_exact_i(const Vector &x)
real_t rhs_func_i(const Vector &x)
void acoustics_solution_grad(const Vector &X, vector< complex< real_t > > &dp)
prob_type prob
prob_type
@ gaussian_beam
@ plane_wave
real_t p_exact_i(const Vector &x)
void hatu_exact_r(const Vector &X, Vector &hatu)
void u_exact_i(const Vector &x, Vector &u)
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Definition array.cpp:68
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
Definition array.hpp:758
int Size() const
Return the logical size of the array.
Definition array.hpp:147
void PartialSum()
Fill the entries of the array with the cumulative sum of the entries.
Definition array.cpp:103
int Append(const T &el)
Append element 'el' to array, resize if necessary.
Definition array.hpp:830
T & Last()
Return the last element in the array.
Definition array.hpp:863
A class to handle Block diagonal preconditioners in a matrix-free implementation.
void SetDiagonalBlock(int iblock, Operator *op)
Add a square block op in the block-entry (iblock, iblock).
SparseMatrix & GetBlock(int i, int j)
Return a reference to block (i,j). Reference may be invalid if Aij(i,j) == NULL.
int NumRowBlocks() const
Return the number of row blocks.
A class to handle Block systems in a matrix-free implementation.
void SetBlock(int iRow, int iCol, Operator *op, real_t c=1.0)
Add a block op in the block-entry (iblock, jblock).
Conjugate gradient method.
Definition solvers.hpp:538
void Mult(const Vector &b, Vector &x) const override
Iterative solution of the linear system using the Conjugate Gradient method.
Definition solvers.cpp:751
void SetOperator(const Operator &op) override
Set/update the solver for the given operator.
Definition solvers.hpp:551
Class representing the DPG weak formulation for complex valued systems (see the class DPGWeakForm).
Mimic the action of a complex operator using two real operators.
virtual Operator & imag()
virtual Operator & real()
Real or imaginary part accessor methods.
A coefficient that is constant across space and time.
for Raviart-Thomas elements
Class for domain integration .
Definition lininteg.hpp:106
Collection of finite elements from the same family in multiple dimensions. This class is used to matc...
Definition fe_coll.hpp:27
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition fespace.hpp:244
virtual int GetTrueVSize() const
Return the number of vector true (conforming) dofs.
Definition fespace.hpp:851
virtual void GetEssentialTrueDofs(const Array< int > &bdr_attr_is_ess, Array< int > &ess_tdof_list, int component=-1) const
Get a list of essential true dofs, ess_tdof_list, corresponding to the boundary attributes marked in ...
Definition fespace.cpp:658
int GetVSize() const
Return the number of vector dofs, i.e. GetNDofs() x GetVDim().
Definition fespace.hpp:848
A general function coefficient.
Data type for Gauss-Seidel smoother of sparse matrix.
Class for grid function - Vector with associated FE space.
Definition gridfunc.hpp:31
virtual real_t ComputeL2Error(Coefficient *exsol[], const IntegrationRule *irs[]=NULL, const Array< int > *elems=NULL) const
Returns ||exsol - u_h||_L2 for scalar or vector H1 or L2 elements.
void ProjectBdrCoefficient(Coefficient &coeff, const Array< int > &attr)
Project a Coefficient on the GridFunction, modifying only DOFs on the boundary associated with the bo...
Definition gridfunc.hpp:481
Arbitrary order H1-conforming (continuous) finite elements.
Definition fe_coll.hpp:275
Arbitrary order "H^{1/2}-conforming" trace finite elements defined on the interface between mesh elem...
Definition fe_coll.hpp:338
void SetRelTol(real_t rtol)
Definition solvers.hpp:229
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition solvers.cpp:174
int GetNumIterations() const
Returns the number of iterations taken during the last call to Mult()
Definition solvers.hpp:280
virtual void SetPrintLevel(int print_lvl)
Legacy method to set the level of verbosity of the solver output.
Definition solvers.cpp:72
void SetMaxIter(int max_it)
Definition solvers.hpp:231
Arbitrary order "L2-conforming" discontinuous finite elements.
Definition fe_coll.hpp:346
Mesh data type.
Definition mesh.hpp:64
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition mesh.hpp:290
int Dimension() const
Dimension of the reference space used within the elements.
Definition mesh.hpp:1216
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition mesh.cpp:11295
Pointer to an Operator of a specified type.
Definition handle.hpp:34
OpType * As() const
Return the Operator pointer statically cast to a specified OpType. Similar to the method Get().
Definition handle.hpp:104
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set 'var' to receive the value. Enable/disable tags are used to set the bool...
Definition optparser.hpp:82
bool Good() const
Return true if the command line options were parsed successfully.
Arbitrary order H(div)-conforming Raviart-Thomas finite elements.
Definition fe_coll.hpp:403
Arbitrary order "H^{-1/2}-conforming" face finite elements defined on the interface between mesh elem...
Definition fe_coll.hpp:462
Data type sparse matrix.
Definition sparsemat.hpp:51
Vector coefficient that is constant in space and time.
A general vector function coefficient.
Vector data type.
Definition vector.hpp:82
int Size() const
Returns the size of the vector.
Definition vector.hpp:226
real_t Sum() const
Return the sum of the vector entries.
Definition vector.cpp:1204
void SetSize(int s)
Resize the vector to size s.
Definition vector.hpp:558
Vector beta
const real_t alpha
Definition ex15.cpp:369
prob_type
Definition ex25.cpp:149
int main()
real_t a
Definition lissajous.cpp:41
void VisualizeField(socketstream &sock, const char *vishost, int visport, GridFunction &gf, const char *title, int x, int y, int w, int h, const char *keys, bool vec)
real_t u(const Vector &xvec)
Definition lor_mms.hpp:22
float real_t
Definition config.hpp:43
const char vishost[]
real_t p(const Vector &x, real_t t)