KADATH
domain_spheric_time_shell.cpp
1 /*
2  Copyright 2017 Philippe Grandclement
3 
4  This file is part of Kadath.
5 
6  Kadath is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Kadath is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Kadath. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "headcpp.hpp"
21 #include "utilities.hpp"
22 #include "spheric_time.hpp"
23 #include "array_math.hpp"
24 #include "val_domain.hpp"
25 
26 namespace Kadath {
27 void coef_1d (int, Array<double>&) ;
28 void coef_i_1d (int, Array<double>&) ;
29 int der_1d (int, Array<double>&) ;
30 
31 // Standard constructor
32 Domain_spheric_time_shell::Domain_spheric_time_shell (int num, int ttype, double tmmin, double tmmax, double rrmin, double rrmax, const Dim_array& nbr) :
33  Domain(num, ttype, nbr), alpha((rrmax-rrmin)/2.), beta ((rrmax+rrmin)/2.), tmin(tmmin), tmax(tmmax) {
34  assert (nbr.get_ndim()==2) ;
35  do_coloc() ;
36 }
37 
38 // Constructor by copy
39 Domain_spheric_time_shell::Domain_spheric_time_shell (const Domain_spheric_time_shell& so) : Domain(so), alpha(so.alpha), beta (so.beta), tmin(so.tmin), tmax(so.tmax) {
40 }
41 
43  fread_be (&alpha, sizeof(double), 1, fd) ;
44  fread_be (&beta, sizeof(double), 1, fd) ;
45  fread_be (&tmin, sizeof(double), 1, fd) ;
46  fread_be (&tmax, sizeof(double), 1, fd) ;
47  do_coloc() ;
48 }
49 
50 // Destructor
51 Domain_spheric_time_shell::~Domain_spheric_time_shell() {}
52 
53 void Domain_spheric_time_shell::save (FILE* fd) const {
54  nbr_points.save(fd) ;
55  nbr_coefs.save(fd) ;
56  fwrite_be (&ndim, sizeof(int), 1, fd) ;
57  fwrite_be (&type_base, sizeof(int), 1, fd) ;
58  fwrite_be (&alpha, sizeof(double), 1, fd) ;
59  fwrite_be (&beta, sizeof(double), 1, fd) ;
60  fwrite_be (&tmin, sizeof(double), 1, fd) ;
61  fwrite_be (&tmax, sizeof(double), 1, fd) ;
62 }
63 
64 ostream& Domain_spheric_time_shell::print (ostream& o) const {
65  o << "Spheric-time shell" << endl ;
66  o << "time goes from " << tmin << " to " << tmax << endl ;
67  o << "R goes from " << -alpha+beta << " to " << alpha+beta << endl ;
68  o << "Nbr pts = " << nbr_points << endl ;
69  o << endl ;
70  return o ;
71 }
72 
73 
75 
76  switch (bound) {
77  case OUTER_BC : {
78  Val_domain res(so.der_var(1)/alpha) ;
79  return res ;
80  }
81  case INNER_BC : {
82  Val_domain res(so.der_var(1)/alpha) ;
83  return res ;
84  }
85  case TIME_INIT : {
86  Val_domain res(so.der_var(2)*2./(tmax - tmin)) ;
87  return res ;
88  }
89  default:
90  cerr << "Unknown boundary case in Domain_spheric_time_shell::der_normal" << endl ;
91  abort() ;
92  }
93 }
94 
95 // Computes the cartesian coordinates
97  for (int i=0 ; i<2 ; i++)
98  assert (coloc[i] != 0x0) ;
99  for (int i=0 ; i<2 ; i++)
100  assert (absol[i] == 0x0) ;
101  for (int i=0 ; i<2 ; i++) {
102  absol[i] = new Val_domain(this) ;
103  absol[i]->allocate_conf() ;
104  }
105  Index index (nbr_points) ;
106  do {
107  absol[0]->set(index) = alpha* ((*coloc[0])(index(0))) + beta ;
108  absol[1]->set(index) = (*coloc[1])(index(1)) * (tmax-tmin)/2. + (tmax+tmin)/2. ;
109  }
110  while (index.inc()) ;
111 
112 }
113 
114 // Computes the radius
116 
117  for (int i=0 ; i<2 ; i++)
118  assert (coloc[i] != 0x0) ;
119  assert (radius == 0x0) ;
120  radius = new Val_domain(this) ;
121  radius->allocate_conf() ;
122  Index index (nbr_points) ;
123  do
124  radius->set(index) = alpha* ((*coloc[0])(index(0))) + beta;
125  while (index.inc()) ;
126 }
127 
128 
129 // Is a point inside this domain ?
130 bool Domain_spheric_time_shell::is_in (const Point& xx, double prec) const {
131 
132  assert (xx.get_ndim()==2) ;
133 
134  bool res = true ;
135  if ((xx(1)<-alpha+beta-prec) || (xx(1)>alpha+beta + prec))
136  res= false ;
137  if ((xx(2)<tmin-prec) || (xx(2)>tmax + prec))
138  res = false ;
139  return res ;
140 }
141 
142 // Convert absolute coordinates to numerical ones
144 
145  assert (is_in(abs)) ;
146  Point num(2) ;
147 
148  num.set(1) = (abs(1)-beta) / alpha ;
149  num.set(2) = 2./(tmax-tmin)*(abs(2)- (tmax+tmin)/2.) ;
150 
151  return num ;
152 }
153 
154 double coloc_leg(int, int) ;
156 
157  switch (type_base) {
158  case CHEB_TYPE:
160  del_deriv() ;
161  for (int i=0 ; i<ndim ; i++)
162  coloc[i] = new Array<double> (nbr_points(i)) ;
163  for (int i=0 ; i<nbr_points(0) ; i++)
164  coloc[0]->set(i) = -cos(M_PI*i/(nbr_points(0)-1)) ;
165  for (int j=0 ; j<nbr_points(1) ; j++)
166  coloc[1]->set(j) = -cos(M_PI*j/(nbr_points(1)-1)) ;
167  break ;
168  case LEG_TYPE:
170  del_deriv() ;
171  for (int i=0 ; i<ndim ; i++)
172  coloc[i] = new Array<double> (nbr_points(i)) ;
173  for (int i=0 ; i<nbr_points(0) ; i++)
174  coloc[0]->set(i) = coloc_leg (i, nbr_points(0)) ;
175  for (int j=0 ; j<nbr_points(1) ; j++)
176  coloc[1]->set(j) = coloc_leg(j, nbr_points(1)) ;
177  break ;
178  default :
179  cerr << "Unknown type of basis in Domain_spheric_time_shell::do_coloc" << endl ;
180  abort() ;
181  }
182 }
183 
184 // Base for a function symetric in z, using Chebyshev
186 
187  assert (type_base == CHEB_TYPE) ;
188  base.allocate(nbr_coefs) ;
189 
190  Index index(base.bases_1d[0]->get_dimensions()) ;
191 
192  base.def=true ;
193  base.bases_1d[1]->set(0) = CHEB ;
194  for (int j=0 ; j<nbr_coefs(1) ; j++)
195  base.bases_1d[0]->set(j) = CHEB ;
196 }
197 
198 // Base for a function symetric in z, using Legendre
200 
201  assert (type_base == LEG_TYPE) ;
202  base.allocate(nbr_coefs) ;
203 
204  Index index(base.bases_1d[0]->get_dimensions()) ;
205  base.def=true ;
206  base.bases_1d[1]->set(0) = LEG ;
207  for (int j=0 ; j<nbr_coefs(1) ; j++)
208  base.bases_1d[0]->set(j) = LEG ;
209  }
210 
211 // Computes the derivatives with respect to rho,Z as a function of the numerical ones.
212 void Domain_spheric_time_shell::do_der_abs_from_der_var(const Val_domain *const *const der_var, Val_domain **const der_abs) const {
213  // d/dr
214  der_abs[0] = new Val_domain (*der_var[0]/alpha) ;
215  // d/dt :
216  der_abs[1] = new Val_domain (*der_var[1]*2./(tmax-tmin)) ;
217 }
218 
219 // Rules for the multiplication of two basis.
221 
222  assert (a.ndim==2) ;
223  assert (b.ndim==2) ;
224 
225  Base_spectral res(2) ;
226  bool res_def = true ;
227 
228  if (!a.def)
229  res_def=false ;
230  if (!b.def)
231  res_def=false ;
232 
233  if (res_def) {
234 
235 
236  // Bases in time :
237  res.bases_1d[1] = new Array<int> (a.bases_1d[1]->get_dimensions()) ;
238  switch ((*a.bases_1d[1])(0)) {
239  case CHEB:
240  switch ((*b.bases_1d[1])(0)) {
241  case CHEB:
242  res.bases_1d[1]->set(0) = CHEB ;
243  break ;
244  default:
245  res_def = false ;
246  break ;
247  }
248  break ;
249  case LEG:
250  switch ((*b.bases_1d[1])(0)) {
251  case LEG:
252  res.bases_1d[1]->set(0) = LEG ;
253  break ;
254  default:
255  res_def = false ;
256  break ;
257  }
258  break ;
259 
260  default:
261  res_def = false ;
262  break ;
263  }
264 
265  // Base in r :
266  Index index_0 (a.bases_1d[0]->get_dimensions()) ;
267  res.bases_1d[0] = new Array<int> (a.bases_1d[0]->get_dimensions()) ;
268  do {
269  switch ((*a.bases_1d[0])(index_0)) {
270  case CHEB:
271  switch ((*b.bases_1d[0])(index_0)) {
272  case CHEB:
273  res.bases_1d[0]->set(index_0) = CHEB ;
274  break ;
275  default:
276  res_def = false ;
277  break ;
278  }
279  break ;
280  case LEG:
281  switch ((*b.bases_1d[0])(index_0)) {
282  case LEG:
283  res.bases_1d[0]->set(index_0) = LEG ;
284  break ;
285  default:
286  res_def = false ;
287  break ;
288  }
289  break ;
290  default:
291  res_def = false ;
292  break ;
293  }
294  }
295  while (index_0.inc()) ;
296  }
297  if (!res_def)
298  for (int dim=0 ; dim<a.ndim ; dim++)
299  if (res.bases_1d[dim]!= 0x0) {
300  delete res.bases_1d[dim] ;
301  res.bases_1d[dim] = 0x0 ;
302  }
303  res.def = res_def ;
304  return res ;
305 }
306 }
Class for storing the basis of decompositions of a field.
Bases_container bases_1d
Arrays containing the various basis of decomposition.
void allocate(const Dim_array &nbr_coefs)
Allocates the various arrays, for a given number of coefficients.
bool def
true if the Base_spectral is defined and false otherwise.
int ndim
Number of dimensions.
Class for storing the dimensions of an array.
Definition: dim_array.hpp:34
int get_ndim() const
Returns the number of dimensions.
Definition: dim_array.hpp:63
void save(FILE *) const
Save function.
Definition: dim_array.cpp:32
Class for a 2-dimensional spherical domain bounded between two finite radii and a symetry with respec...
virtual void do_der_abs_from_der_var(const Val_domain *const *const der_var, Val_domain **const der_abs) const
Computes the derivative with respect to the absolute Cartesian coordinates from the derivative with r...
Domain_spheric_time_shell(int num, int ttype, double tmmin, double tmmax, double r_int, double r_ext, const Dim_array &nbr)
Standard constructor :
virtual void set_legendre_base(Base_spectral &) const
Gives the standard base for Legendre polynomials.
double beta
Relates the numerical to the physical radii.
virtual void do_radius() const
Computes the generalized radius.
double alpha
Relates the numerical to the physical radii.
virtual void do_coloc()
Computes the colocation points.
virtual Val_domain der_normal(const Val_domain &, int) const
Normal derivative with respect to a given surface.
virtual const Point absol_to_num(const Point &) const
Computes the numerical coordinates from the physical ones.
virtual void save(FILE *) const
Saving function.
virtual bool is_in(const Point &xx, double prec=1e-13) const
Check whether a point lies inside Domain.
virtual void do_absol() const
Computes the absolute coordinates.
virtual Base_spectral mult(const Base_spectral &, const Base_spectral &) const
Method for the multiplication of two Base_spectral.
virtual ostream & print(ostream &o) const
Delegate function to virtualize the << operator.
virtual void set_cheb_base(Base_spectral &) const
Gives the standard base for Chebyshev polynomials.
Abstract class that implements the fonctionnalities common to all the type of domains.
Definition: space.hpp:60
virtual void del_deriv()
Destroys the derivated members (like coloc, cart and radius), when changing the type of colocation po...
Definition: domain.cpp:77
Val_domain * radius
The generalized radius.
Definition: space.hpp:78
Memory_mapped_array< Val_domain * > absol
Asbolute coordinates (if defined ; usually Cartesian-like)
Definition: space.hpp:76
int ndim
Number of dimensions.
Definition: space.hpp:64
Dim_array nbr_coefs
Number of coefficients.
Definition: space.hpp:66
Dim_array nbr_points
Number of colocation points.
Definition: space.hpp:65
int type_base
Type of colocation point :
Definition: space.hpp:73
Memory_mapped_array< Array< double > * > coloc
Colocation points in each dimension (stored in ndim 1d- arrays)
Definition: space.hpp:75
Class that gives the position inside a multi-dimensional Array.
Definition: index.hpp:38
bool inc(int increm, int var=0)
Increments the position of the Index.
Definition: index.hpp:99
The class Point is used to store the coordinates of a point.
Definition: point.hpp:30
const int & get_ndim() const
Returns the number of dimensions.
Definition: point.hpp:51
double & set(int i)
Read/write of a coordinate.
Definition: point.hpp:47
Class for storing the basis of decompositions of a field and its values on both the configuration and...
Definition: val_domain.hpp:69
double & set(const Index &pos)
Read/write the value of the field in the configuration space.
Definition: val_domain.cpp:171
Val_domain der_var(int i) const
Computes the derivative with respect to a numerical coordinate.
Definition: val_domain.cpp:670
void allocate_conf()
Allocates the values in the configuration space and destroys the values in the coefficients space.
Definition: val_domain.cpp:209