LORENE
tbl.C
1/*
2 * Methods of class Tbl
3 *
4 * (see file tbl.h for documentation)
5 *
6 */
7
8/*
9 * Copyright (c) 1999-2000 Jean-Alain Marck
10 * Copyright (c) 1999-2001 Eric Gourgoulhon
11 *
12 * This file is part of LORENE.
13 *
14 * LORENE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * LORENE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with LORENE; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30
31
32
33/*
34 * $Id: tbl.C,v 1.12 2016/12/05 16:18:16 j_novak Exp $
35 * $Log: tbl.C,v $
36 * Revision 1.12 2016/12/05 16:18:16 j_novak
37 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
38 *
39 * Revision 1.11 2014/10/13 08:53:41 j_novak
40 * Lorene classes and functions now belong to the namespace Lorene.
41 *
42 * Revision 1.10 2014/10/06 15:13:18 j_novak
43 * Modified #include directives to use c++ syntax.
44 *
45 * Revision 1.9 2008/02/18 13:53:47 j_novak
46 * Removal of special indentation instructions.
47 *
48 * Revision 1.8 2006/09/25 10:01:50 p_grandclement
49 * Addition of N-dimensional Tbl
50 *
51 * Revision 1.7 2003/11/03 13:53:20 j_novak
52 * Yet another efficiency improvement.
53 *
54 * Revision 1.6 2003/10/19 19:58:56 e_gourgoulhon
55 * Slightly improved operator<<.
56 *
57 * Revision 1.5 2003/02/26 10:47:11 j_novak
58 * The copy of a Tbl to another has been improved in speed.
59 *
60 * Revision 1.4 2002/10/16 14:37:13 j_novak
61 * Reorganization of #include instructions of standard C++, in order to
62 * use experimental version 3 of gcc.
63 *
64 * Revision 1.3 2002/09/24 08:32:07 e_gourgoulhon
65 *
66 * Added constructor from Matrice.
67 *
68 * Revision 1.2 2001/12/04 21:27:54 e_gourgoulhon
69 *
70 * All writing/reading to a binary file are now performed according to
71 * the big endian convention, whatever the system is big endian or
72 * small endian, thanks to the functions fwrite_be and fread_be
73 *
74 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
75 * LORENE
76 *
77 * Revision 2.13 1999/11/24 16:00:43 eric
78 * Modif affichage dimensions dans affiche_seuil.
79 *
80 * Revision 2.12 1999/11/23 13:32:44 eric
81 * Le constructeur Tbl::Tbl(const Dim_tbl ) devient Tbl::Tbl(const Dim_tbl& ).
82 * Le constructeur Tbl::Tbl(const Grille3d* ) devient
83 * Tbl(const Grille3d& ).
84 * Modif affichage.
85 *
86 * Revision 2.11 1999/11/15 16:36:36 eric
87 * Le membre dim est desormais un Dim_tbl et non plus un pointeur sur un
88 * Dim_tbl.
89 *
90 * Revision 2.10 1999/10/29 15:45:44 eric
91 * Ajout des cas 1-D et 2-D dans operator<<
92 *
93 * Revision 2.9 1999/10/29 15:05:16 eric
94 * Suppression des fonctions membres min() et max():
95 * elles deviennent des fonctions externes.
96 *
97 * Revision 2.8 1999/10/21 14:37:34 eric
98 * *** empty log message ***
99 *
100 * Revision 2.7 1999/10/18 15:06:13 eric
101 * La fonction membre annule() est rebaptisee annule_hard().
102 *
103 * Revision 2.6 1999/10/15 13:58:39 eric
104 * Modification de l'affichage (operator<<).
105 *
106 * Revision 2.5 1999/10/01 12:36:08 eric
107 * Ajout de la fonction affiche_seuil.
108 *
109 * Revision 2.4 1999/10/01 10:09:52 eric
110 * 0 -> double(0)
111 *
112 * Revision 2.3 1999/09/30 12:51:46 eric
113 * Enleve l'include "grilles.h" non necessaire.
114 *
115 * Revision 2.2 1999/09/24 14:24:23 eric
116 * Depoussierage, changement de prototypes, etc...
117 *
118 * Revision 2.1 1999/03/02 16:49:42 eric
119 * *** empty log message ***
120 *
121 * Revision 2.0 1999/02/15 10:42:45 hyc
122 * *** empty log message ***
123 *
124 *
125 * $Header: /cvsroot/Lorene/C++/Source/Tbl/tbl.C,v 1.12 2016/12/05 16:18:16 j_novak Exp $
126 *
127 */
128
129// headers C
130#include <cmath>
131
132// headers Lorene
133#include "itbl.h"
134#include "tbl.h"
135#include "grilles.h"
136#include "matrice.h"
137#include "utilitaires.h"
138
139
140 //---------------//
141 // Constructeurs //
142 //---------------//
143
144
145// Constructeur 1D
146namespace Lorene {
147Tbl::Tbl(int n1) : etat(ETATNONDEF), dim(n1), t(0x0) {}
148
149// Constructeur 2D
150Tbl::Tbl(int n1, int n0) : etat(ETATNONDEF), dim(n1, n0), t(0x0) {}
151
152// Constructeur 3D
153Tbl::Tbl(int n2, int n1, int n0) : etat(ETATNONDEF), dim(n2, n1, n0), t(0x0) {}
154
155// Constructeur a partir d'une grille 3D
156Tbl::Tbl(const Grille3d& g) : etat(ETATNONDEF),
157 dim(g.get_np(), g.get_nt(), g.get_nr()),
158 t(0x0) {}
159
160// Constructeur a partir d'un Itbl
161Tbl::Tbl(Itbl sizes) : etat(ETATNONDEF),
162 dim(1),
163 t(0x0) {
164
165 int n = sizes.get_dim(0) ;
166 int* dims = new int[n] ;
167 for (int i=0 ; i<n ; i++)
168 dims[i] = sizes(i) ;
169 Dim_tbl new_dim (n, dims) ;
170 dim = new_dim ;
171 delete [] dims ;
172}
173
174// Constructeur a partir d'un Dim_tbl
175Tbl::Tbl(const Dim_tbl& dt) : etat(ETATNONDEF), dim(dt), t(0x0) {}
176
177// Copie
178Tbl::Tbl(const Tbl& tc) : etat(tc.etat), dim(tc.dim) {
179
180 // La valeur eventuelle
181 if (tc.etat == ETATQCQ) {
182 int n = dim.taille ;
183 t = new double[n] ;
184 double* tin = tc.t ;
185 double* tout = t ;
186 for (int i=0 ; i<n ; i++) {
187 *tout = *tin ;
188 tout++ ; tin++ ;
189 }
190 }
191 else{
192 t = 0x0 ;
193 }
194
195}
196
197// From file
198Tbl::Tbl(FILE* fd) : dim(fd) {
199
200 fread_be(&etat, sizeof(int), 1, fd) ; // etat
201
202 // Le tableau
203 if (etat == ETATQCQ) {
204 t = new double[get_taille()] ;
205 fread_be(t, sizeof(double), get_taille(), fd) ; // le tableau
206 }
207 else{
208 t = 0x0 ;
209 }
210}
211
212// From a matrix
213Tbl::Tbl(const Matrice& aa) : etat(aa.get_etat()),
214 dim( (aa.get_array()).dim ) {
215
216 int nbl = dim.dim[1] ;
217 int nbc = dim.dim[0] ;
218
219 // Special case of one row :
220 if (nbl == 1) {
221 dim.ndim = 1 ;
222 }
223
224 // Special case of one column :
225 if (nbc == 1) {
226 dim.ndim = 1 ;
227 dim.dim[0] = dim.dim[1] ;
228 dim.dim[1] = 1 ;
229 }
230
231 if (etat == ETATQCQ) {
232
233 t = new double[get_taille()] ;
234
235 Tbl taa = aa.get_array() ;
236 double* ta = taa.t ;
237
238 for (int i=0 ; i<get_taille() ; i++) {
239 t[i] = ta[i] ;
240 }
241
242 }
243 else{
244 t = 0x0 ;
245 }
246
247}
248
249 //-------------//
250 // Destructeur //
251 //-------------//
252
254 delete [] t ;
255}
256
257 //-------------//
258 // Affectation //
259 //-------------//
260
261// From Tbl
262void Tbl::operator=(const Tbl& tx)
263{
264 // Protection
265 assert( dim == tx.dim ) ;
266 assert(tx.get_etat() != ETATNONDEF) ;
267
268 switch (tx.etat) {
269 case ETATZERO:
270 set_etat_zero() ;
271 break ;
272
273 case ETATQCQ: {
274 set_etat_qcq() ;
275 int n = get_taille() ;
276 double* tin = tx.t ;
277 double* tout = t ;
278 for (int i=0 ; i<n ; i++) {
279 *tout = *tin ;
280 tout++ ;
281 tin ++ ;
282 }
283 break ;
284 }
285 default:
286 cout << "Erreur bizarre !" << endl ;
287 abort() ;
288 break ;
289 }
290}
291
292// From double
293void Tbl::operator=(double a)
294{
295 if ( a == double(0) ) {
296 set_etat_zero() ;
297 }
298 else {
299 int n = get_taille() ;
300 set_etat_qcq() ;
301 for (int i=0 ; i<n ; i++) {
302 t[i] = a ;
303 }
304 }
305}
306
307// From int
308void Tbl::operator=(int m)
309{
310 if (m == 0) {
311 set_etat_zero() ;
312 }
313 else {
314 int n = get_taille() ;
315 set_etat_qcq() ;
316 for (int i=0 ; i<n ; i++) {
317 t[i] = m ;
318 }
319 }
320}
321
322
323 //------------//
324 // Sauvegarde //
325 //------------//
326
327// save in a file
328
329void Tbl::sauve(FILE* fd) const {
330
331 dim.sauve(fd) ; // dim
332 fwrite_be(&etat, sizeof(int), 1, fd) ; // etat
333 if (etat == ETATQCQ) {
334 fwrite_be(t, sizeof(double), get_taille(), fd) ; // le tableau
335 }
336}
337
338 //-----------------//
339 // Gestion memoire //
340 //-----------------//
341
342// Destructeur logique
344 delete [] t ;
345 t = 0x0 ;
346 etat = ETATNONDEF ;
347}
348
349// ETATZERO
351 if (etat == ETATZERO) return ;
352 del_t() ;
353 etat = ETATZERO ;
354}
355
356// ETATNONDEF
358 if (etat == ETATNONDEF) return ;
359 del_t() ;
360 etat = ETATNONDEF ;
361}
362
363// ETATQCQ
365 if (etat == ETATQCQ) return ;
366
367 // Protection
368 assert( (etat == ETATZERO) || (etat == ETATNONDEF) ) ; // sinon...
369
370 t = new double[get_taille()] ;
371 etat = ETATQCQ ;
372}
373
374// ZERO hard
376 if (t == 0x0) {
377 t = new double[get_taille()] ;
378 }
379 for (int i=0 ; i<get_taille() ; i++) {
380 t[i] = 0. ;
381 }
382 etat = ETATQCQ ;
383}
384
385
386 //------------------------//
387 // Display //
388 //------------------------//
389
390//-----------
391// Operator<<
392//-----------
393
394ostream& operator<<(ostream& o, const Tbl& t) {
395
396 int ndim = t.get_ndim() ;
397 o.precision(4);
398 o.setf(ios::showpoint);
399 o << "*** Tbl " << ndim << "D" << " size: " ;
400 for (int i = 0; i<ndim-1; i++) {
401 o << t.get_dim(i) << " x " ;
402 }
403 o << t.get_dim(ndim-1) << " = " << t.get_taille()
404 << endl ;
405
406
407 if (t.get_etat() == ETATZERO) {
408 o << "Identically ZERO" << endl ;
409 return o ;
410 }
411
412 if (t.get_etat() == ETATNONDEF) {
413 o << "UNDEFINED STATE" << endl ;
414 return o ;
415 }
416
417 assert(t.etat == ETATQCQ) ;
418 switch (ndim) {
419
420 case 1 : {
421 for (int i=0 ; i<t.get_dim(0) ; i++) {
422 o << " " << t(i) ;
423 }
424 o << endl ;
425 break ;
426 }
427
428
429 case 2 : {
430 for (int j=0 ; j<t.get_dim(1) ; j++) {
431 o << " j = " << j << " : " << endl ;
432 for (int i=0 ; i<t.get_dim(0) ; i++) {
433 o << " " << t(j, i) ;
434 }
435 o << endl ;
436 }
437 o << endl ;
438 break ;
439 }
440
441 case 3 : {
442 for (int k=0 ; k<t.get_dim(2) ; k++) {
443 o << "k = " << k << " : " << endl ;
444 for (int j=0 ; j<t.get_dim(1) ; j++) {
445 o << "j = " << j << " : " ;
446 for (int i=0 ; i<t.get_dim(0) ; i++) {
447 o << " " << t(k, j, i) ;
448 }
449 o << endl ;
450 }
451 o << endl ;
452 }
453 o << endl ;
454 break ;
455 }
456
457 default : {
458 cout << "operator<< Tbl : unexpected dimension !" << endl ;
459 cout << " ndim = " << ndim << endl ;
460 abort() ;
461 break ;
462 }
463 }
464 return o ;
465}
466
467//---------------
468// Affiche_seuil
469//---------------
470
471void Tbl::affiche_seuil(ostream& ost, int precis, double seuil) const {
472
473 int ndim = get_ndim() ;
474 ost << "*** Tbl " << ndim << "D" << " size: " ;
475 for (int i = 0; i<ndim-1; i++) {
476 ost << get_dim(i) << " x " ;
477 }
478 ost << get_dim(ndim-1) << " = " << get_taille() << endl ;
479
480 // Cas particuliers
481 //-----------------
482
483 if (etat == ETATNONDEF) {
484 ost << " state: UNDEFINED" << endl ;
485 return ;
486 }
487
488 if (etat == ETATZERO) {
489 ost << " state: ZERO" << endl ;
490 return ;
491 }
492
493 // Affichage des elements du tableau
494 //----------------------------------
495
496 ost << " threshold for display : " << seuil << endl ;
497 ost.precision(precis);
498 ost.setf(ios::showpoint);
499
500 switch (get_ndim()) {
501 case 1 : { // cas 1-D
502
503 for (int i=0; i<get_dim(0); i++) {
504 ost << " " << setw(precis) << (*this)(i) ;
505 }
506 ost << endl ;
507 break ;
508 }
509
510 case 2 : { // cas 2-D
511
512 for (int j=0; j<get_dim(1); j++) {
513 ost << " #j=" << j << " : " ;
514 for (int i=0; i<get_dim(0); i++){
515 ost << " " << setw(precis) << (*this)(j, i) ;
516 }
517 ost << endl;
518 }
519 ost << endl;
520 break;
521 }
522
523 case 3 : { // cas 3-D
524 for (int k=0; k<get_dim(2); k++) {
525 for (int j=0; j<get_dim(1); j++){
526 int test_imp = 0 ;
527 for (int i=0; i<get_dim(0); i++){
528 if ( fabs( (*this)(k, j, i) ) >= seuil )
529 test_imp = 1 ;
530 }
531 if (test_imp == 1 ) {
532 ost << " #k=" << k <<",j=" << j << " : " ;
533 for (int i=0; i<get_dim(0); i++){
534 ost << " " << setw(precis) << (*this)(k, j, i) ;
535 }
536 ost << endl ;
537 }
538 }
539 }
540 ost << endl;
541 break;
542 }
543
544 default : {
545 cout << "Tbl:affiche_seuil : unexpected dimension !" << endl ;
546 cout << " get_ndim() = " << get_ndim() << endl ;
547 abort() ;
548 break;
549 }
550
551 } // fin du switch sur le nombre de dimensions
552
553 // On restaure l'etat du flot ost a ses valeurs standards:
554 ost.precision(6);
555 ost.unsetf(ios::showpoint);
556}
557
558
559
560}
Storage of array dimensions.
Definition dim_tbl.h:99
3D grid class in one domain.
Definition grilles.h:200
Basic integer array class.
Definition itbl.h:122
int get_dim(int i) const
Gives the i th dimension (ie {tt dim.dim[i] ).
Definition itbl.h:326
Tbl get_array() const
Returns the array of matrix elements.
Definition matrice.h:251
Dim_tbl dim
Number of dimensions, size,...
Definition tbl.h:172
void operator=(const Tbl &)
Assignment to another Tbl.
Definition tbl.C:262
int get_ndim() const
Gives the number of dimensions (ie dim.ndim).
Definition tbl.h:400
Tbl(int size0)
1D constructor
Definition tbl.C:147
int get_etat() const
Gives the logical state.
Definition tbl.h:394
int etat
logical state (ETATNONDEF, ETATQCQ or ETATZERO).
Definition tbl.h:169
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.C:471
void sauve(FILE *) const
Save in a file.
Definition tbl.C:329
void annule_hard()
Sets the Tbl to zero in a hard way.
Definition tbl.C:375
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition tbl.C:350
void del_t()
Logical destructor: dellocates the memory occupied by the array t and sets the logical state to ETATN...
Definition tbl.C:343
~Tbl()
Destructor.
Definition tbl.C:253
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition tbl.C:364
void set_etat_nondef()
Sets the logical state to ETATNONDEF (undefined).
Definition tbl.C:357
int get_taille() const
Gives the total size (ie dim.taille).
Definition tbl.h:397
friend ostream & operator<<(ostream &, const Tbl &)
Display.
Definition tbl.C:394
double * t
The array of double.
Definition tbl.h:173
int get_dim(int i) const
Gives the i-th dimension (ie dim.dim[i]).
Definition tbl.h:403
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 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
Lorene prototypes.
Definition app_hor.h:67