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