LORENE
tbl_val.C
1 /*
2  * Methods for the class Tbl_val.
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 char TBL_VAL_C[] = "$Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val.C,v 1.7 2014/10/13 08:53:48 j_novak Exp $" ;
31 
32 /*
33  * $Id: tbl_val.C,v 1.7 2014/10/13 08:53:48 j_novak Exp $
34  * $Log: tbl_val.C,v $
35  * Revision 1.7 2014/10/13 08:53:48 j_novak
36  * Lorene classes and functions now belong to the namespace Lorene.
37  *
38  * Revision 1.6 2014/10/06 15:13:22 j_novak
39  * Modified #include directives to use c++ syntax.
40  *
41  * Revision 1.5 2008/02/18 13:53:48 j_novak
42  * Removal of special indentation instructions.
43  *
44  * Revision 1.4 2007/11/02 15:45:58 j_novak
45  * Added an ugly method "append_array", which substitutes the argument to the
46  * main array t.
47  *
48  * Revision 1.3 2002/10/16 14:37:15 j_novak
49  * Reorganization of #include instructions of standard C++, in order to
50  * use experimental version 3 of gcc.
51  *
52  * Revision 1.2 2001/12/04 21:27:54 e_gourgoulhon
53  *
54  * All writing/reading to a binary file are now performed according to
55  * the big endian convention, whatever the system is big endian or
56  * small endian, thanks to the functions fwrite_be and fread_be
57  *
58  * Revision 1.1 2001/11/22 13:41:54 j_novak
59  * Added all source files for manipulating Valencia type objects and making
60  * interpolations to and from Meudon grids.
61  *
62  *
63  * $Header: /cvsroot/Lorene/C++/Source/Valencia/tbl_val.C,v 1.7 2014/10/13 08:53:48 j_novak Exp $
64  *
65  */
66 
67 // headers C
68 #include <cmath>
69 
70 // headers Lorene
71 #include "headcpp.h"
72 #include "tbl_val.h"
73 #include "utilitaires.h"
74 
75 
76  //---------------//
77  // Constructeurs //
78  //---------------//
79 
80 
81 // Constructeur a partir d'une grille de Valence
82 namespace Lorene {
83 Tbl_val::Tbl_val(const Grille_val* g) : etat(ETATNONDEF),
84  dim(g->get_dim_tbl()), gval(g), t(0x0), tzri(0x0), txti(0x0), typi(0x0) {}
85 
86 // Copie
87 Tbl_val::Tbl_val(const Tbl_val& tc) : etat(tc.etat), dim(tc.dim),
88  gval(tc.gval) {
89 
90  // La valeur eventuelle
91  if (tc.etat == ETATQCQ) {
92  t = new double[get_taille()] ;
93  for (int i=0 ; i<get_taille() ; i++) {
94  t[i] = tc.t[i] ;
95  }
96 
97  tzri = new double[get_taille_i(0)] ;
98  for (int i=0 ; i<get_taille_i(0) ; i++) {
99  tzri[i] = tc.tzri[i] ;
100  }
101 
102  if (get_ndim() > 1) {
103  txti= new double[get_taille_i(1)] ;
104  for (int i=0 ; i<get_taille_i(1) ; i++) txti[i] = tc.txti[i] ;
105  }
106  else txti = 0x0 ;
107  if (get_ndim() > 2) {
108  typi = new double[get_taille_i(2)] ;
109  for (int i=0; i<get_taille_i(2); i++) typi[i] = tc.typi[i] ;
110  }
111  else typi = 0x0 ;
112  }
113  else{
114  t = 0x0 ;
115  tzri = 0x0 ;
116  txti = 0x0 ;
117  typi = 0x0 ;
118  }
119 }
120 
121 // From file
122 Tbl_val::Tbl_val(const Grille_val* g, FILE* fd) : dim(g->get_dim_tbl()),
123  gval(g) {
124 
125  fread_be(&etat, sizeof(int), 1, fd) ; // etat
126 
127  // Le tableau
128  if (etat == ETATQCQ) {
129  t = new double[get_taille()] ;
130  fread_be(t, sizeof(double), get_taille(), fd) ; // le tableau
131  tzri = new double[get_taille_i(0)] ;
132  fread_be(tzri, sizeof(double), get_taille_i(0), fd) ;
133  if (get_ndim() > 1) {
134  txti = new double[get_taille_i(1)] ;
135  fread_be(txti, sizeof(double), get_taille_i(1), fd) ; }
136  else txti = 0x0 ;
137  if (get_ndim() > 2) {
138  typi = new double[get_taille_i(2)] ;
139  fread_be(typi, sizeof(double), get_taille_i(2), fd) ; }
140  else typi = 0x0 ;
141  }
142  else{
143  t = 0x0 ;
144  tzri = 0x0 ;
145  txti = 0x0 ;
146  typi = 0x0 ;
147  }
148 }
149 
150  //-------------//
151  // Destructeur //
152  //-------------//
153 
155  del_t() ;
156 }
157 
158  //-------------//
159  // Affectation //
160  //-------------//
161 
162 // From Tbl_val
164 {
165  // Protection
166  assert( gval == tx.gval ) ;
167  assert(tx.get_etat() != ETATNONDEF) ;
168 
169  int n = get_taille() ;
170  int ndim = get_ndim() ;
171  switch (tx.etat) {
172  case ETATZERO:
173  set_etat_zero() ;
174  break ;
175 
176  case ETATQCQ:
177  set_etat_qcq() ;
178  for (int i=0 ; i<n ; i++) {
179  t[i] = tx.t[i] ;
180  }
181  for (int i=0; i<get_taille_i(0); i++) tzri[i] = tx.tzri[i] ;
182  if (ndim > 1) for(int i=0; i < get_taille_i(1); i++)
183  txti[i] = tx.txti[i] ;
184  if (ndim > 2) for(int i=0; i < get_taille_i(2); i++)
185  typi[i] = tx.typi[i] ;
186  break ;
187 
188  default:
189  cout << "Erreur bizarre !" << endl ;
190  abort() ;
191  break ;
192  }
193 }
194 
195 // From double
196 void Tbl_val::operator=(double a)
197 {
198  if ( a == double(0) ) {
199  set_etat_zero() ;
200  }
201  else {
202  int n = get_taille() ;
203  set_etat_qcq() ;
204  for (int i=0 ; i<n ; i++) {
205  t[i] = a ;
206  }
207  for (int i=0 ; i < get_taille_i(0) ; i++)
208  tzri[i] = a ;
209 
210  if (txti != 0x0) for (int i=0 ; i < get_taille_i(1) ; i++)
211  txti[i] = a ;
212 
213  if (typi != 0x0) for (int i=0 ; i < get_taille_i(2) ; i++)
214  typi[i] = a ;
215  }
216 }
217 
218 // From int
220 {
221  if (m == 0) {
222  set_etat_zero() ;
223  }
224  else {
225  int n = get_taille() ;
226  set_etat_qcq() ;
227  for (int i=0 ; i<n ; i++) {
228  t[i] = m ;
229  }
230  for (int i=0 ; i < get_taille_i(0) ; i++)
231  tzri[i] = m ;
232 
233  if (txti != 0x0) for (int i=0 ; i < get_taille_i(1) ; i++)
234  txti[i] = m ;
235 
236  if (typi != 0x0) for (int i=0 ; i < get_taille_i(2) ; i++)
237  typi[i] = m ;
238 
239  }
240 }
241 
242 
243  //------------//
244  // Sauvegarde //
245  //------------//
246 
247 // save in a file
248 
249 void Tbl_val::sauve(FILE* fd) const {
250 
251  fwrite_be(&etat, sizeof(int), 1, fd) ; // etat
252  if (etat == ETATQCQ) {
253  fwrite_be(t, sizeof(double), get_taille(), fd) ; // le tableau
254  fwrite_be(tzri, sizeof(double), get_taille_i(0), fd) ;
255  if (get_ndim() > 1)
256  fwrite_be(txti, sizeof(double), get_taille_i(1), fd) ;
257  if (get_ndim() > 2)
258  fwrite_be(typi, sizeof(double), get_taille_i(2), fd) ;
259  }
260 }
261 
262 //-----------------//
263 // Gestion memoire //
264 //-----------------//
265 
266 // Destructeur logique
268  if (t != 0x0) delete [] t ;
269  t = 0x0 ;
270  if (tzri != 0x0) delete [] tzri ;
271  tzri = 0x0 ;
272  if (txti != 0x0) delete [] txti ;
273  txti = 0x0 ;
274  if (typi != 0x0) delete [] typi ;
275  typi = 0x0 ;
276  etat = ETATNONDEF ;
277 }
278 
279 // ETATZERO
281  if (etat == ETATZERO) return ;
282  del_t() ;
283  etat = ETATZERO ;
284 }
285 
286 // ETATNONDEF
288  if (etat == ETATNONDEF) return ;
289  del_t() ;
290  etat = ETATNONDEF ;
291 }
292 
293 // ETATQCQ
295  if (etat == ETATQCQ) return ;
296 
297  // Protection
298  assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
299 
300  t = new double[get_taille()] ;
301  tzri = new double[get_taille_i(0)] ;
302  int ndim = get_ndim() ;
303  if (ndim > 1) {txti = new double[get_taille_i(1)] ;}
304  else txti = 0x0 ;
305  if (ndim > 2) {typi = new double[get_taille_i(2)] ;}
306  else typi = 0x0 ;
307  etat = ETATQCQ ;
308 }
309 
310 // ZERO hard
312  if (t == 0x0) {
313  t = new double[get_taille()] ;
314  }
315  for (int i=0 ; i<get_taille() ; i++) {
316  t[i] = 0. ;
317  }
318  tzri = new double[get_taille_i(0)] ;
319  for (int i=0 ; i < get_taille_i(0) ; i++)
320  tzri[i] = 0 ;
321  int ndim = get_ndim() ;
322  if (ndim > 1) {
323  txti = new double[get_taille_i(1)] ;
324  for (int i=0 ; i < get_taille_i(1) ; i++) txti[i] = 0 ;
325  }
326  else txti = 0x0 ;
327  if (ndim > 2) {
328  typi = new double[get_taille_i(2)] ;
329  for (int i=0 ; i < get_taille_i(2) ; i++) typi[i] = 0 ;
330  }
331  else typi = 0x0 ;
332 
333  etat = ETATQCQ ;
334 }
335 
336 void Tbl_val::append_array(double* t_in) {
337  assert (t_in != 0x0) ;
338  del_t() ;
339  t = t_in ;
340  etat = ETATQCQ ;
341 }
342 
343  //------------------------//
344  // Display //
345  //------------------------//
346 
347 //-----------
348 // Operator<<
349 //-----------
350 
351 ostream& operator<<(ostream& o, const Tbl_val& t) {
352 
353  int ndim = t.get_ndim() ;
354  o.precision(4);
355  o.setf(ios::showpoint);
356  o << "*** Tbl_val " << ndim << "D" << " size: " ;
357  for (int i = 0; i<ndim-1; i++) {
358  o << t.get_dim(ndim-1-i) ;
359  if (ndim-i == 3) o << "(Y)" << " x " ;
360  if (ndim-i == 2) o << "(X)" << " x " ;
361  }
362  o << t.get_dim(0) << "(Z)" << " + " << t.gval->get_fantome() <<
363  " hidden cells on each side = " << t.get_taille() << endl ;
364 
365  if (t.get_etat() == ETATZERO) {
366  o << "Identically ZERO" << endl ;
367  return o ;
368  }
369 
370  if (t.get_etat() == ETATNONDEF) {
371  o << "UNDEFINED STATE" << endl ;
372  return o ;
373  }
374 
375  assert(t.etat == ETATQCQ) ;
376  switch (ndim) {
377 
378  case 1 : {
379  for (int i=0 ; i<t.get_dim(0) ; i++) {
380  o << " " << t(i) ;
381  }
382  o << endl ;
383  break ;
384  }
385 
386 
387  case 2 : {
388  for (int j=0 ; j<t.get_dim(1) ; j++) {
389  o << " J_x " << j << " : " << endl ;
390  for (int i=0 ; i<t.get_dim(0) ; i++) {
391  o << " " << t(j, i) ;
392  }
393  o << endl ;
394  }
395  o << endl ;
396  break ;
397  }
398 
399  case 3 : {
400  for (int k=0 ; k<t.get_dim(2) ; k++) {
401  o << " K_y = " << k << " : " << endl ;
402  for (int j=0 ; j<t.get_dim(1) ; j++) {
403  o << " J_x = " << j << " : " ;
404  for (int i=0 ; i<t.get_dim(0) ; i++) {
405  o << " " << t(k, j, i) ;
406  }
407  o << endl ;
408  }
409  o << endl ;
410  }
411  o << endl ;
412  break ;
413  }
414 
415  default : {
416  cout << "operator<< Tbl_val : unexpected dimension !" << endl ;
417  cout << " ndim = " << ndim << endl ;
418  abort() ;
419  break ;
420  }
421  }
422  return o ;
423 }
424 
425 //---------------
426 // Affiche_seuil
427 //---------------
428 
429 void Tbl_val::affiche_seuil(ostream& ost, int precis, double seuil) const {
430 
431  int ndim = get_ndim() ;
432  ost << "*** Tbl_val " << ndim << "D" << " size: " ;
433  for (int i = 0; i<ndim-1; i++) {
434  ost << get_dim(i) << " x " ;
435  }
436  ost << get_dim(ndim-1) << " + " << gval->get_fantome() <<
437  " hidden cells on each side = " << get_taille() << endl ;
438 
439  // Cas particuliers
440  //-----------------
441 
442  if (etat == ETATNONDEF) {
443  ost << " state: UNDEFINED" << endl ;
444  return ;
445  }
446 
447  if (etat == ETATZERO) {
448  ost << " state: ZERO" << endl ;
449  return ;
450  }
451 
452  // Affichage des elements du tableau
453  //----------------------------------
454 
455  ost << " threshold for display : " << seuil << endl ;
456  ost.precision(precis);
457  ost.setf(ios::showpoint);
458 
459  ost << " Values on the nodes, without hidden cells:" << endl ;
460  switch (get_ndim()) {
461  case 1 : { // cas 1-D
462 
463  for (int i=0; i<get_dim(0); i++) {
464  ost << " " << setw(precis) << (*this)(i) ;
465  }
466  ost << endl ;
467  break ;
468  }
469 
470  case 2 : { // cas 2-D
471 
472  for (int j=0; j<get_dim(1); j++) {
473  ost << " #j=" << j << " : " ;
474  for (int i=0; i<get_dim(0); i++){
475  ost << " " << setw(precis) << (*this)(j, i) ;
476  }
477  ost << endl;
478  }
479  ost << endl;
480  break;
481  }
482 
483  case 3 : { // cas 3-D
484  for (int k=0; k<get_dim(2); k++) {
485  for (int j=0; j<get_dim(1); j++){
486  int test_imp = 0 ;
487  for (int i=0; i<get_dim(0); i++){
488  if ( fabs( (*this)(k, j, i) ) >= seuil )
489  test_imp = 1 ;
490  }
491  if (test_imp == 1 ) {
492  ost << " #k=" << k <<",j=" << j << " : " ;
493  for (int i=0; i<get_dim(0); i++){
494  ost << " " << setw(precis) << (*this)(k, j, i) ;
495  }
496  ost << endl ;
497  }
498  }
499  }
500  ost << endl;
501  break;
502  }
503 
504  default : {
505  cout << "Tbl_val:affiche_seuil : unexpected dimension !" << endl ;
506  cout << " get_ndim() = " << get_ndim() << endl ;
507  abort() ;
508  break;
509  }
510 
511  } // fin du switch sur le nombre de dimensions
512 
513  // On restaure l'etat du flot ost a ses valeurs standards:
514  ost.precision(6);
515  ost.unsetf(ios::showpoint);
516 }
517 
518 
519 
520 }
int get_fantome() const
Returns the number of hidden cells.
Definition: grille_val.h:168
Lorene prototypes.
Definition: app_hor.h:64
void append_array(double *t_in)
Appends an array of doubles as the main array t of this (DO NOT use it, unless you REALLY know how it...
Definition: tbl_val.C:336
int get_ndim() const
Gives the number of dimensions (ie dim->ndim )
Definition: tbl_val.h:482
void operator=(const Tbl_val &)
Assignment to another Tbl_val.
Definition: tbl_val.C:163
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
void set_etat_nondef()
Sets the logical state to ETATNONDEF (undefined).
Definition: tbl_val.C:287
void del_t()
Logical destructor: dellocates the memory occupied by the array t and sets the logical state to ETATN...
Definition: tbl_val.C:267
Base class for Godunov-type grids.
Definition: grille_val.h:94
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: tbl_val.C:294
int get_taille_i(int i) const
Gives the size of the interface arrays (including the hidden cells)
Definition: tbl_val.h:469
int get_etat() const
Gives the logical state.
Definition: tbl_val.h:459
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition: tbl_val.C:280
double * tzri
The array at z (or r) interfaces.
Definition: tbl_val.h:116
Finite-difference array intended to store field values.
Definition: tbl_val.h:97
int fwrite_be(const int *aa, int size, int nb, FILE *fich)
Writes integer(s) into a binary file according to the big endian convention.
Definition: fwrite_be.C:70
void affiche_seuil(ostream &ostr, int precision=4, double threshold=1.e-7) const
Prints only the values greater than a given threshold.
Definition: tbl_val.C:429
double * typi
The array at y (or ) interfaces.
Definition: tbl_val.h:120
int fread_be(int *aa, int size, int nb, FILE *fich)
Reads integer(s) from a binary file according to the big endian convention.
Definition: fread_be.C:69
friend ostream & operator<<(ostream &, const Tbl_val &)
Display.
Definition: tbl_val.C:351
int get_dim(int i) const
Gives the i th dimension (ie dim->dim[i] , without hidden cells)
Definition: tbl_val.h:485
void annule_hard()
Sets the Tbl_val to zero in a hard way.
Definition: tbl_val.C:311
double * t
The array of double at the nodes.
Definition: tbl_val.h:114
void sauve(FILE *) const
Save in a file.
Definition: tbl_val.C:249
int get_taille() const
Gives the size of the node array (including the hidden cells)
Definition: tbl_val.h:462
~Tbl_val()
Destructor.
Definition: tbl_val.C:154
Tbl_val(const Grille_val *)
Constructor from a 3D grid.
Definition: tbl_val.C:83
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