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