Debian Codegraph @

School05/Wednesday/sol_fields.C

raw
1 // Lorene headers
2 #include "metric.h"
3 #include "nbr_spx.h"
4 #include "utilitaires.h"
5 #include "graphique.h"
6
7 using namespace Lorene ;
8
9 int main() {
10
11 // Construction of a multi-grid (Mg3d)
12 // -----------------------------------
13
14 const int nz = 3 ; // Number of domains
15 int nr = 33 ; // Number of collocation points in r in each domain
16 int nt = 9 ; // Number of collocation points in theta in each domain
17 int np = 8 ; // Number of collocation points in phi in each domain
18 int symmetry_theta = SYM ; // symmetry with respect to the equatorial plane
19 int symmetry_phi = NONSYM ; // no symmetry in phi
20 bool compact = true ;
21 assert(nz > 1) ;
22
23 Mg3d mgrid(nz, nr, nt, np, symmetry_theta, symmetry_phi, compact) ;
24 cout << "Grid: " << endl << mgrid ;
25
26 // Construction of an affine mapping (Map_af)
27 // ------------------------------------------
28
29 // Boundaries of each domains
30 double Rlim = 4. ;
31 Tbl r_limits(nz+1) ;
32 r_limits.set_etat_qcq() ;
33 for (int i=0; i<nz; i++)
34 r_limits.set(i) = double(i)/double(nz-1) *Rlim ;
35 r_limits.set(nz) = __infinity ;
36
37 Map_af map(mgrid, r_limits) ;
38 cout << "Mapping: " << endl << map ;
39
40 // Definition of coordinate fields
41 //--------------------------------
42 const Coord& r = map.r ;
43 const Coord& x = map.x ;
44 const Coord& y = map.y ;
45 const Coord& sint = map.sint ;
46 const Coord& cosp = map.cosp ;
47 const Coord& sinp = map.sinp ;
48
49 // Setup of a regular scalar field
50 //--------------------------------
51 Scalar phi(map) ;
52 phi = x*exp(-r*r-y*y) ;
53 phi.set_outer_boundary(nz-1, 0) ;
54 phi.std_spectral_base() ;
55
56 // Drawings
57 //---------
58 des_coupe_z(phi, 0, 2) ;
59 des_meridian(phi, 0, 1.3*Rlim, "\\gf", 1) ;
60 arrete() ;
61
62 // Computation of the radial derivative
63 //-------------------------------------
64 Scalar dphidr = phi.dsdr() ;
65 dphidr.dec_dzpuis(2) ;
66 des_coupe_z(dphidr, 0, 2) ;
67 des_meridian(dphidr, 0, 1.3*Rlim, "\\gf", 2) ;
68 arrete() ;
69
70 // Comparison with analytic formula
71 //---------------------------------
72 Scalar dphi_anal(map) ;
73 dphi_anal = exp(-r*r-y*y)*(sint*cosp*(1 - 2*r*r)
74 - 2*sint*sinp*x*y);
75 dphi_anal.set_outer_boundary(nz-1, 0.) ;
76 dphi_anal.set_spectral_base(dphidr.get_spectral_base()) ;
77
78 maxabs(dphidr - dphi_anal, "Absolute error on the derivative of phi:") ;
79
80 // Flat metric in spherical components
81 //------------------------------------
82 const Metric_flat& mets = map.flat_met_spher() ;
83
84 // Vector field defined as the gradient of a scalar field
85 //-------------------------------------------------------
86 Vector grad_phi = phi.derive_con(mets) ;
87
88 // Change to cartesian triad for drawing
89 grad_phi.change_triad(map.get_bvect_cart()) ;
90 des_coupe_vect_z(grad_phi, 0., -2, 0.5, 1) ;
91
92 // Vector field defined by its cartesian components
93 //-------------------------------------------------
94 Vector v_cart(map, CON, map.get_bvect_cart()) ;
95 v_cart.set(1) = y / (1. + r*r) ;
96 v_cart.set(1).set_outer_boundary(nz-1, 0) ;
97 v_cart.set(2) = -x*exp(-r*r) ;
98 v_cart.set(2).set_outer_boundary(nz-1, 0) ;
99 v_cart.set(3) = 0 ;
100 v_cart.std_spectral_base() ;
101 des_coupe_vect_z(v_cart, 0., -2, 0.5, 1) ;
102
103 // Computation of the divergence w/ flat metric in cartesian components
104 //---------------------------------------------------------------------
105 Scalar dive = v_cart.divergence(map.flat_met_cart()) ;
106 dive.dec_dzpuis(2) ;
107 des_coupe_z(dive, 0, 2) ;
108
109 return EXIT_SUCCESS ;
110 }