Debian Codegraph @

School05/Friday/sol_wave_impl.C

raw
1 /*****************************************************************************
2
3 Implicit wave-equation solver in #D with Sommerfeld boundary conditions
4
5 ******************************************************************************/
6
7 #include <cmath>
8
9 // Lorene headers
10 #include "metric.h"
11 #include "proto.h"
12 #include "graphique.h"
13 #include "diff.h"
14
15 using namespace Lorene ;
16
17 int main() {
18
19 // Construction of a multi-grid (Mg3d)
20 // -----------------------------------
21
22 const int nz = 1 ; // Only a nucleus
23 int nr;
24 cout << "Enter nr: " << endl ;
25 cin >> nr ; // Number of collocation points in r
26 int nt = 5 ; // Number of collocation points in theta
27 int np = 6 ; // Number of collocation points in phi
28 int symmetry_theta = SYM ; // symmetry with respect to the equatorial plane
29 int symmetry_phi = SYM ; // symmetry in phi
30
31 Mg3d mgrid(nz, nr, nt, np, symmetry_theta, symmetry_phi, false) ;
32
33 // Construction of an affine mapping (Map_af)
34 // ------------------------------------------
35
36 double Rlim = 2. ;
37
38 // Boundaries of each domains
39 double r_limits[] = {0., Rlim} ;
40
41 Map_af map(mgrid, r_limits) ;
42 double alpha = map.get_alpha()[0] ;
43
44 // Coordinate fields
45 const Coord& r = map.r ;
46 const Coord& x = map.x ;
47 const Coord& y = map.y ;
48
49 // Flat metric in spherical components
50 //------------------------------------
51 const Metric_flat& mets = map.flat_met_spher() ;
52
53 // Initial data
54 //-------------
55 double sigma = 4;
56 Scalar phim1(map) ;
57 phim1 = x*y*(exp(-sigma*r*r) - exp(-sigma*Rlim*Rlim)) ;
58 phim1.std_spectral_base() ;
59 Scalar phi = phim1 ;
60
61 // Time-step & boundary conditions
62 //--------------------------------
63 double dt ;
64 cout << "Enter dt: " << endl ;
65 cin >> dt ;
66 int iter = 0 ;
67 int ndes = int(Rlim / dt) / 200 + 1;
68 double a_bound = 3./(2.*dt) + 1./Rlim ;
69 double b_bound = 1. ;
70 double c_bound = 0. ;
71
72 // Some initializations
73 //---------------------
74 int l_q, m_q, base_r ;
75 double emax = -1.e33 ;
76 double emin = 1.e33 ;
77 phi.set_spectral_va().ylm() ;
78 const Base_val& base = phi.get_spectral_base() ;
79
80 //----------------------------
81 // Main loop
82 //----------------------------
83 for (double tps=0.; tps<3.*Rlim; tps += dt) {
84
85 // Crank-Nicholson 2nd-order scheme
86 //---------------------------------
87 Scalar source = 2*phi - phim1 + 0.5*dt*dt*phim1.laplacian(0) ;
88 source.set_spectral_va().ylm() ;
89 phi.set_spectral_va().ylm() ;
90 phim1.set_spectral_va().ylm() ;
91
92 Mtbl_cf resu(mgrid, base) ;
93 resu.annule_hard() ; //Initialized to zero
94
95 // Loop on theta, phi
96 //-------------------
97 for (int k=0; k<np; k++)
98 for (int j=0; j<nt; j++)
99 if (nullite_plm(j, nt, k, np, base) == 1) {
100 base.give_quant_numbers(0, k, j, m_q, l_q, base_r) ;
101
102 // Recovering the elementary operators
103 //------------------------------------
104 Diff_sxdsdx dx(base_r, nr) ; const Matrice& mdx = dx.get_matrice() ;
105 Diff_dsdx2 dx2(base_r, nr) ; const Matrice& md2 = dx2.get_matrice() ;
106 Diff_sx2 sx2(base_r, nr) ; const Matrice& ms2 = sx2.get_matrice() ;
107 Diff_id id(base_r, nr) ; const Matrice& mid = id.get_matrice() ;
108
109 // Global operator (without BCs)
110 //------------------------------
111 Matrice ope = mid
112 - 0.5*dt*dt*(md2 + 2*mdx - l_q*(l_q+1)*ms2)/(alpha*alpha) ;
113
114 // Boundary conditions on the last line
115 //-------------------------------------
116 for (int col = 0; col<nr; col++)
117 ope.set(nr-1, col) = a_bound + b_bound*4*col*col / alpha ;
118
119 Tbl rhs(nr) ; rhs.set_etat_qcq() ;
120 Tbl fi(nr) ; fi.set_etat_qcq() ;
121 Tbl fim1(nr) ; fim1.set_etat_qcq() ;
122
123 for (int i=0; i<nr; i++) {
124 rhs.set(i) = (*source.get_spectral_va().c_cf)(0, k, j, i) ;
125 fi.set(i) = (*phi.get_spectral_va().c_cf)(nz-1, k, j, i) ;
126 fim1.set(i) = (*phim1.get_spectral_va().c_cf)(nz-1, k, j, i) ;
127 }
128 c_bound = (4*val1_dern_1d(0, fi, base_r)
129 - val1_dern_1d(0, fim1, base_r)) / (2*dt) ;
130 rhs.set(nr-1) = c_bound ;
131
132 // Regularity conditions at the center (last but one line)
133 //--------------------------------------------------------
134 if ((l_q > 1)&&(base_r==R_CHEBP)) {
135 int pari = 1 ;
136 for (int col=0; col<nr; col++) {
137 ope.set(nr-2, col) = pari ;
138 pari *= -1 ;
139 }
140 rhs.set(nr-2) = 0 ;
141 }
142 if ((l_q > 1)&&(base_r==R_CHEBI)) {
143 int pari = 1 ;
144 for (int col=0; col<nr; col++) {
145 ope.set(nr-2, col) = pari*(2*col+1) ;
146 pari *= -1 ;
147 }
148 rhs.set(nr-2) = 0 ;
149 }
150
151 // Solution of the system
152 //-----------------------
153 ope.set_lu() ;
154 Tbl sol = ope.inverse(rhs) ;
155 for (int i=0; i<nr; i++)
156 resu.set(0, k, j, i) = sol(i) ;
157 }
158
159 // Putting everything into place
160 //------------------------------
161 Scalar phip1(map) ;
162 phip1.set_etat_qcq() ;
163 phip1.set_spectral_va() = resu ;
164 phip1.set_spectral_va().ylm_i() ;
165 phi.set_spectral_va().ylm_i() ;
166 phim1.set_spectral_va().ylm_i() ;
167
168 // Energy calculation
169 //-------------------
170 Scalar dtphi = (phip1 - phim1)/(2*dt) ;
171 double ener = (dtphi*dtphi
172 + contract(phi.derive_con(mets), 0,
173 phi.derive_cov(mets), 0)).integrale() ;
174
175 emax = (ener > emax) ? ener : emax ;
176 emin = (ener < emin) ? ener : emin ;
177
178 cout << "Energy inside the grid: " << ener << endl ;
179
180 // Drawing
181 //--------
182 if (iter%ndes == 0) {
183 des_meridian(phip1, 0., Rlim, "\\gf", 1) ;
184 }
185
186 // Preparation for next step
187 //--------------------------
188 iter++ ;
189 phim1 = phi ;
190 phi = phip1 ;
191 }
192 cout << "Emin, Emax: " << emin << ", " << emax << endl ;
193 cout << "Difference: " << (emax - emin) / emax << endl ;
194
195 return EXIT_SUCCESS ;
196 }