LORENE
tbl_val_interp.C
1 /*
2  * Methods for making interpolations with Godunov-type arrays.
3  *
4  * See the file tbl_val.h for documentation
5  *
6  */
7 
8 /*
9  * Copyright (c) 2001 Jerome Novak
10  *
11  * This file is part of LORENE.
12  *
13  * LORENE is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * LORENE is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with LORENE; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  */
28 
29 
30 
31 
32 /*
33  * $Id: tbl_val_interp.C,v 1.14 2016/12/05 16:18:20 j_novak Exp $
34  * $Log: tbl_val_interp.C,v $
35  * Revision 1.14 2016/12/05 16:18:20 j_novak
36  * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
37  *
38  * Revision 1.13 2014/10/13 08:53:48 j_novak
39  * Lorene classes and functions now belong to the namespace Lorene.
40  *
41  * Revision 1.12 2014/10/06 15:13:22 j_novak
42  * Modified #include directives to use c++ syntax.
43  *
44  * Revision 1.11 2013/02/28 16:15:00 j_novak
45  * Minor change.
46  *
47  * Revision 1.10 2007/12/21 10:46:29 j_novak
48  * In "from_spectral..." functions: better treatment of ETATZERO case.
49  *
50  * Revision 1.9 2007/11/02 16:49:12 j_novak
51  * Suppression of intermediate array for spectral summation.
52  *
53  * Revision 1.8 2005/06/23 13:40:08 j_novak
54  * The tests on the number of dimensions have been changed to handle better the
55  * axisymmetric case.
56  *
57  * Revision 1.7 2005/06/22 09:11:17 lm_lin
58  *
59  * Grid wedding: convert from the old C++ object "Cmp" to "Scalar".
60  *
61  * Revision 1.6 2003/12/19 15:05:15 j_novak
62  * Trying to avoid shadowed variables
63  *
64  * Revision 1.5 2003/10/03 16:17:17 j_novak
65  * Corrected some const qualifiers
66  *
67  * Revision 1.4 2002/11/13 11:22:57 j_novak
68  * Version "provisoire" de l'interpolation (sommation depuis la grille
69  * spectrale) aux interfaces de la grille de Valence.
70  *
71  * Revision 1.3 2002/10/16 14:37:15 j_novak
72  * Reorganization of #include instructions of standard C++, in order to
73  * use experimental version 3 of gcc.
74  *
75  * Revision 1.2 2001/11/23 16:03:07 j_novak
76  *
77  * minor modifications on the grid check.
78  *
79  * Revision 1.1 2001/11/22 13:41:54 j_novak
80  * Added all source files for manipulating Valencia type objects and making
81  * interpolations to and from Meudon grids.
82  *
83  *
84  * $Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val_interp.C,v 1.14 2016/12/05 16:18:20 j_novak Exp $
85  *
86  */
87 
88 // headers C
89 #include <cmath>
90 
91 // headers Lorene
92 #include "headcpp.h"
93 #include "tbl_val.h"
94 
95 
96 namespace Lorene {
97 Scalar Tbl_val::to_spectral(const Map& mp, const int lmax, const int lmin,
98  int type_inter) const {
99 
100  assert(etat != ETATNONDEF) ;
101  assert( gval->compatible(&mp, lmax, lmin) ) ;
102  Scalar resu(mp) ;
103 
104  if (etat == ETATZERO) {
105  resu.annule(lmin, lmax-1) ;
106  return resu ;
107  }
108  else {
109  int nzin = lmax - lmin ;
110  int dim_spec = 1 ;
111  const Mg3d* mgrid = mp.get_mg() ;
112  for (int i=lmin; i<lmax; i++) {
113  if ((mgrid->get_nt(i) > 1)&&(dim_spec==1)) dim_spec = 2;
114  if (mgrid->get_np(i) > 1) dim_spec = 3;
115  }
116  const Coord& rr = mp.r ;
117 
118  int* ntet = new int[nzin] ;
119  int ntetmax = 1 ;
120  int* nphi = new int[nzin] ;
121  int nphimax = 1 ;
122  for (int i=lmin; i<lmax; i++) {
123  int tmp = mgrid->get_np(i) ;
124  nphi[i-lmin] = tmp ;
125  nphimax = (tmp > nphimax ? tmp : nphimax) ;
126  tmp = mgrid->get_nt(i) ;
127  ntet[i-lmin] = tmp ;
128  ntetmax = (tmp > ntetmax ? tmp : ntetmax) ;
129  }
130  if (dim_spec > 1) {
131  for (int i=lmin; i<lmax; i++) {
132  if ((nphimax % nphi[i-lmin]) != 0) {
133  cout << "Tbl_val::to_spectral: The numbers of points in phi" << endl ;
134  cout << "in the different domains of Meudon grid are not" << endl;
135  cout << "well defined; see the documentation." << endl ;
136  abort() ;
137  }
138  assert((ntet[i-lmin]-1) > 0) ;
139  if (((ntetmax-1) % (ntet[i-lmin]-1)) != 0) {
140  cout <<"Tbl_val::to_spectral: The numbers of points in theta"<< endl ;
141  cout << "in the different domains of Meudon grid are not" << endl;
142  cout << "well defined; see the documentation." << endl ;
143  abort() ;
144  }
145  }
146  }
147 
148  resu.allocate_all() ;
149  if (lmin>0) resu.annule(0,lmin-1) ;
150  if (lmax < mgrid->get_nzone()) resu.annule(lmax, mgrid->get_nzone()-1) ;
151 
152  int fant = gval->get_fantome() ;
153  int flag = 1 ;
154  int nrarr = 0 ;
155  for (int l=lmin; l<lmax; l++) nrarr += mgrid->get_nr(l) -1 ;
156  nrarr++ ;
157  switch (dim_spec) {
158 
159  case 1: {
160  int tsize = dim->dim[0] + 2*fant ;
161  Tbl fdep(tsize) ;
162  fdep.set_etat_qcq() ;
163  for (int i=0; i<tsize; i++) fdep.set(i) = t[i] ;
164  Tbl farr(nrarr) ;
165  Tbl rarr(nrarr) ;
166  rarr.set_etat_qcq() ;
167  int inum = 0 ;
168  for (int l=lmin; l<lmax; l++) {
169  for (int i=0; i<mgrid->get_nr(l); i++) {
170  rarr.set(inum) = (+rr)(l,0,0,i) ;
171  inum++ ;
172  }
173  inum--;
174  }
175  farr = gval->interpol1(*gval->zr, rarr, fdep, flag, type_inter) ;
176  inum = 0 ;
177  for (int l=lmin; l<lmax; l++) {
178  for (int i=0; i<mgrid->get_nr(l); i++) {
179  resu.set_grid_point(l,0,0,i) = farr(inum) ;
180  inum++ ;
181  }
182  inum--;
183  }
184  break ;
185  }
186 
187  case 2: {
188  int tsizex = dim->dim[1] + 2*fant ;
189  int tsizez = dim->dim[0] + 2*fant ;
190  Tbl fdep(tsizex, tsizez) ;
191  fdep.set_etat_qcq() ;
192  for (int j=0; j<tsizex; j++) {
193  for (int i=0; i<tsizez; i++) {
194  int l = tsizez*j + i ;
195  fdep.t[l] = t[l] ;
196  }
197  }
198  Tbl farr(ntetmax, nrarr) ;
199  Tbl rarr(nrarr) ;
200  rarr.set_etat_qcq() ;
201  Tbl tetarr(ntetmax) ;
202  tetarr.set_etat_qcq() ;
203  int ltmax = 0 ;
204  int inum = 0 ;
205  for (int l=lmin; l<lmax; l++) {
206  if (ntetmax == ntet[l-lmin]) ltmax = l ;
207  for (int i=0; i<mgrid->get_nr(l); i++) {
208  rarr.set(inum) = (+rr)(l,0,0,i) ;
209  inum++ ;
210  }
211  inum--;
212  }
213  const Coord& tet = mp.tet ;
214  for (int j=0; j<ntetmax; j++)
215  tetarr.set(j) = (+tet)(ltmax,0,j,0) ;
216  farr = gval->interpol2(fdep, rarr, tetarr, type_inter) ;
217  inum = 0 ;
218  for (int l=lmin; l<lmax; l++) {
219  for (int j=0; j<ntet[l-lmin]; j++) {
220  int itet = (ntetmax-1)/(ntet[l-lmin]-1)*j ;
221  for (int i=0; i<mgrid->get_nr(l); i++) {
222  resu.set_grid_point(l,0,j,i) = farr(itet,inum) ;
223  inum++ ;
224  }
225  inum -= mgrid->get_nr(l) ;
226  }
227  inum += mgrid->get_nr(l) - 1;
228  }
229  break ;
230  }
231 
232  case 3: {
233  if (type_inter == 0) {
234  cout << "The use of routine INSMTS is not well suited" << endl ;
235  cout << "for 3D interpolation." << endl ;
236  // abort() ;
237  }
238  int tsizey = dim->dim[2] + 2*fant ;
239  int tsizex = dim->dim[1] + 2*fant ;
240  int tsizez = dim->dim[0] + 2*fant ;
241  Tbl fdep(tsizey, tsizex, tsizez) ;
242  fdep.set_etat_qcq() ;
243  for (int k=0; k<tsizey; k++) {
244  for (int j=0; j<tsizex; j++) {
245  for (int i=0; i<tsizez; i++) {
246  int l = (k*tsizex+j)*tsizez+i ;
247  fdep.t[l] = t[l];
248  }
249  }
250  }
251  Tbl farr(nphimax, ntetmax, nrarr) ;
252  Tbl rarr(nrarr) ;
253  rarr.set_etat_qcq() ;
254  Tbl tetarr(ntetmax) ;
255  tetarr.set_etat_qcq() ;
256  Tbl phiarr(nphimax) ;
257  phiarr.set_etat_qcq() ;
258  int lpmax = 0 ;
259  int ltmax = 0 ;
260  int inum = 0 ;
261  for (int l=lmin; l<lmax; l++) {
262  if (ntetmax == ntet[l-lmin]) ltmax = l ;
263  if (nphimax == nphi[l-lmin]) lpmax = l ;
264  for (int i=0; i<mgrid->get_nr(l); i++) {
265  rarr.set(inum) = (+rr)(l,0,0,i) ;
266  inum++ ;
267  }
268  inum-- ;
269  }
270  const Coord& tet = mp.tet ;
271  const Coord& phi = mp.phi ;
272  for (int k=0; k<nphimax; k++) {
273  phiarr.set(k) = (+phi)(lpmax,k,0,0) ;
274  }
275  for (int j=0; j<ntetmax; j++)
276  tetarr.set(j) = (+tet)(ltmax,0,j,0) ;
277  farr = gval->interpol3(fdep, rarr, tetarr, phiarr, type_inter) ;
278  inum = 0 ;
279  for (int l=lmin; l<lmax; l++) {
280  for (int k=0; k<nphi[l-lmin]; k++) {
281  int iphi = (nphimax-1)/(nphi[l-lmin]-1)*k ;
282  for (int j=0; j<ntet[l-lmin]; j++) {
283  int itet = (ntetmax-1)/(ntet[l-lmin]-1)*j ;
284  for (int i=0; i<mgrid->get_nr(l); i++) {
285  resu.set_grid_point(l,k,j,i) = farr(iphi,itet,inum) ;
286  inum++ ;
287  }
288  inum -= mgrid->get_nr(l) ;
289  }
290  }
291  inum += mgrid->get_nr(l) - 1 ;
292  }
293  break ;
294  }
295 
296  default:
297  cout << "Tbl_val::to_spectral:Strange error..." << endl ;
298  abort() ;
299  break ;
300 
301  }
302 
303  delete [] ntet ;
304  delete [] nphi ;
305  return resu ;
306  }
307 }
308 
309 void Tbl_val::from_spectral(const Scalar& meudon, int lmax, int lmin,
310  bool interfr, bool interft)
311 {
312  assert(meudon.get_etat() != ETATNONDEF) ;
313 #ifndef NDEBUG
314  const Map& mp = meudon.get_mp() ;
315 #endif
316  assert( gval->contenue_dans(mp, lmax, lmin) ) ;
317  if (lmin < 0) {
318  cout << "Tbl_val::from_spectral() : " << endl ;
319  cout << "lmin, lmax : " << lmin << ", " << lmax << endl ;
320  }
321 
322  if (meudon.get_etat() == ETATZERO) {
323  annule_hard() ;
324  return ;
325  }
326  else {
327  assert((meudon.get_etat() == ETATQCQ)||(meudon.get_etat() == ETATUN)) ;
328  set_etat_qcq() ;
329 
330  switch (gval->get_ndim()) {
331 
332  case 1: {
333  gval->somme_spectrale1(meudon, t, get_taille()) ;
334  break ;
335  }
336 
337  case 2: {
338  gval->somme_spectrale2(meudon, t, get_taille()) ;
339  if (interfr) {
340  delete [] tzri ;
341  const Gval_spher* gvs = dynamic_cast<const Gval_spher*>(gval) ; //## A modifier
342  assert (gvs != 0x0) ;
343  tzri = gvs->somme_spectrale2ri(meudon) ;
344  }
345  if (interft) {
346  delete [] txti ;
347  const Gval_spher* gvs = dynamic_cast<const Gval_spher*>(gval) ; //## A modifier
348  assert (gvs != 0x0) ;
349  txti = gvs->somme_spectrale2ti(meudon) ;
350  }
351  break ;
352  }
353 
354  case 3: {
355  gval->somme_spectrale3(meudon, t, get_taille()) ;
356  break ;
357  }
358 
359  default:
360  cout << "Tbl_val::from_spectral:Strange error..." << endl ;
361  abort() ;
362  break ;
363 
364  }
365  return ;
366  }
367 }
368 
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 }
virtual bool contenue_dans(const Map &mp, const int lmax, const int lmin=0) const =0
Checks if Grille_val is contained inside the spectral grid/mapping within the domains [lmin...
int get_fantome() const
Returns the number of hidden cells.
Definition: grille_val.h:168
int get_ndim() const
Returns the number of dimensions.
Definition: grille_val.h:183
int get_np(int l) const
Returns the number of points in the azimuthal direction ( ) in domain no. l.
Definition: grilles.h:479
Lorene prototypes.
Definition: app_hor.h:67
const Mg3d * get_mg() const
Gives the Mg3d on which the mapping is defined.
Definition: map.h:783
double & set(int i)
Read/write of a particular element (index i) (1D case)
Definition: tbl.h:301
Tensor field of valence 0 (or component of a tensorial field).
Definition: scalar.h:393
Base class for coordinate mappings.
Definition: map.h:688
double * somme_spectrale2ri(const Scalar &meudon) const
Same as before but at radial grid interfaces.
const Dim_tbl * dim
The Dim_tbl giving the dimensions and number of points (without the hidden cells).
Definition: tbl_val.h:108
int etat
logical state (ETATNONDEF , ETATQCQ or ETATZERO ).
Definition: tbl_val.h:103
int get_etat() const
Returns the logical state ETATNONDEF (undefined), ETATZERO (null) or ETATQCQ (ordinary).
Definition: scalar.h:560
virtual Tbl interpol3(const Tbl &fdep, const Tbl &rarr, const Tbl &tetarr, const Tbl &phiarr, const int type_inter) const =0
Performs 3D interpolation.
Coord tet
coordinate centered on the grid
Definition: map.h:737
Coord phi
coordinate centered on the grid
Definition: map.h:738
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: tbl.C:364
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: tbl_val.C:297
double * t
The array of double.
Definition: tbl.h:176
virtual void somme_spectrale2(const Scalar &meudon, double *t, int taille) const =0
Same as before but for the 2D case.
Tbl * zr
Arrays containing the values of coordinate z (or r) on the nodes.
Definition: grille_val.h:124
int get_nzone() const
Returns the number of domains.
Definition: grilles.h:465
double * tzri
The array at z (or r) interfaces.
Definition: tbl_val.h:116
double * somme_spectrale2ti(const Scalar &meudon) const
Same as before but at angular grid interfaces.
virtual Tbl interpol2(const Tbl &fdep, const Tbl &rarr, const Tbl &tetarr, const int type_inter) const =0
Performs 2D interpolation.
Active physical coordinates and mapping derivatives.
Definition: coord.h:90
int get_nr(int l) const
Returns the number of points in the radial direction ( ) in domain no. l.
Definition: grilles.h:469
Multi-domain grid.
Definition: grilles.h:279
virtual bool compatible(const Map *mp, const int lmax, const int lmin=0) const =0
Checks if the spectral grid and mapping are compatible with the Grille_val caracteristics for the int...
Class for spherical Godunov-type grids.
Definition: grille_val.h:579
void annule_hard()
Sets the Tbl_val to zero in a hard way.
Definition: tbl_val.C:314
double * t
The array of double at the nodes.
Definition: tbl_val.h:114
virtual void somme_spectrale3(const Scalar &meudon, double *t, int taille) const =0
Same as before but for the 3D case.
int get_taille() const
Gives the size of the node array (including the hidden cells)
Definition: tbl_val.h:462
Basic array class.
Definition: tbl.h:164
int get_nt(int l) const
Returns the number of points in the co-latitude direction ( ) in domain no. l.
Definition: grilles.h:474
Tbl interpol1(const Tbl &rdep, const Tbl &rarr, const Tbl &fdep, int flag, const int type_inter) const
Performs 1D interpolation.
const Map & get_mp() const
Returns the mapping.
Definition: tensor.h:874
void from_spectral(const Scalar &meudon, int lmax, int lmin=0, bool interfr=false, bool interft=false)
Interpolation from a Scalar to a Tbl_val (spectral summation).
Scalar to_spectral(const Map &map, const int lmax, const int lmin=0, int type_inter=2) const
Interpolation from a Tbl_val to a Scalar .
double * txti
The array at x (or ) interfaces.
Definition: tbl_val.h:118
const Grille_val * gval
The Grille_val (cartesian or spherical) on which the array is defined.
Definition: tbl_val.h:110
int * dim
Array of dimensions (size: ndim).
Definition: dim_tbl.h:102
Coord r
r coordinate centered on the grid
Definition: map.h:736
void somme_spectrale1(const Scalar &meudon, double *t, int taille) const
Makes the sommation of the spectral basis functions to know the values of the function described by t...