Debian Codegraph @

C++/Source/Mg3d/mg3d.C

raw
1 /*
2 * Methods of class Mg3d
3 *
4 */
5
6 /*
7 * Copyright (c) 1999-2000 Jean-Alain Marck
8 * Copyright (c) 1999-2001 Eric Gourgoulhon
9 *
10 * This file is part of LORENE.
11 *
12 * LORENE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * LORENE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with LORENE; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28
29 char mg3d_C[] = "$Header: /cvsroot/Lorene/C++/Source/Mg3d/mg3d.C,v 1.18 2014/10/13 08:53:07 j_novak Exp $" ;
30
31 /*
32 * $Id: mg3d.C,v 1.18 2014/10/13 08:53:07 j_novak Exp $
33 * $Log: mg3d.C,v $
34 * Revision 1.18 2014/10/13 08:53:07 j_novak
35 * Lorene classes and functions now belong to the namespace Lorene.
36 *
37 * Revision 1.17 2014/10/06 15:13:14 j_novak
38 * Modified #include directives to use c++ syntax.
39 *
40 * Revision 1.16 2013/06/05 15:00:26 j_novak
41 * Suppression of all classes derived from Grille3d. Now Grille3d is no
42 * longer an abstract class. r-samplings are only one of RARE, FIN or
43 * UNSURR (FINJAC has been removed). Instead, Mg3d possesses a new member
44 * colloc_r[nzone] defining the type of collocation points (spectral
45 * bases) in each domain.
46 *
47 * Revision 1.15 2012/01/17 10:37:42 j_penner
48 * added a constructor that treats all domains as type FIN
49 *
50 * Revision 1.14 2008/08/19 06:42:00 j_novak
51 * Minor modifications to avoid warnings with gcc 4.3. Most of them concern
52 * cast-type operations, and constant strings that must be defined as const char*
53 *
54 * Revision 1.13 2007/12/11 15:28:15 jl_cornou
55 * Jacobi(0,2) polynomials partially implemented
56 *
57 * Revision 1.12 2006/05/17 13:17:03 j_novak
58 * New member g_angu_1dom, the one-domain angular grid associated with the
59 * current grid.
60 *
61 * Revision 1.11 2005/10/07 08:47:21 j_novak
62 * Addition of the pointer g_non_axi on a grid, with at least 5 points in the
63 * theta direction and 4 in the phi one (for tensor rotations).
64 *
65 * Revision 1.10 2004/07/06 13:36:29 j_novak
66 * Added methods for desaliased product (operator |) only in r direction.
67 *
68 * Revision 1.9 2003/10/27 16:21:54 e_gourgoulhon
69 * Treated the special case nz=1 in the simplified constructor.
70 *
71 * Revision 1.8 2003/06/20 14:50:15 f_limousin
72 * Add the operator==
73 *
74 * Revision 1.7 2003/06/18 08:45:27 j_novak
75 * In class Mg3d: added the member get_radial, returning only a radial grid
76 * For dAlembert solver: the way the coefficients of the operator are defined has been changed.
77 *
78 * Revision 1.6 2002/10/16 14:36:42 j_novak
79 * Reorganization of #include instructions of standard C++, in order to
80 * use experimental version 3 of gcc.
81 *
82 * Revision 1.5 2002/05/07 07:36:03 e_gourgoulhon
83 * Compatibilty with xlC compiler on IBM SP2:
84 * suppressed the parentheses around argument of instruction new:
85 * e.g. t = new (Tbl *[nzone]) --> t = new Tbl*[nzone]
86 *
87 * Revision 1.4 2001/12/12 09:23:46 e_gourgoulhon
88 * Parameter compact added to the simplified constructor of class Mg3d
89 *
90 * Revision 1.3 2001/12/11 06:48:30 e_gourgoulhon
91 * Addition of the simplified constructor
92 *
93 * Revision 1.2 2001/12/04 21:27:54 e_gourgoulhon
94 *
95 * All writing/reading to a binary file are now performed according to
96 * the big endian convention, whatever the system is big endian or
97 * small endian, thanks to the functions fwrite_be and fread_be
98 *
99 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
100 * LORENE
101 *
102 * Revision 2.10 2001/05/26 14:50:46 eric
103 * *** empty log message ***
104 *
105 * Revision 2.9 2001/05/26 13:25:59 eric
106 * Ajout du membre g_twice (grille double pour le desaliasing)
107 * Modif de la declaration de g_angu (pointeur mutable)
108 * g_twice et g_angu ne sont calcules que si necessaire (cad si
109 * on appelle la fonction get_twice() ou get_angu()).
110 *
111 * Revision 2.8 2000/03/22 13:38:51 eric
112 * Remplacement des iendl par endl dans <<
113 *
114 * Revision 2.7 1999/10/12 15:04:29 eric
115 * *** empty log message ***
116 *
117 * Revision 2.6 1999/10/12 15:03:30 eric
118 * *** empty log message ***
119 *
120 * Revision 2.5 1999/09/30 14:58:16 eric
121 * Operator!= declare const
122 *
123 * Revision 2.4 1999/09/30 14:12:04 eric
124 * sauve declaree const.
125 *
126 * Revision 2.3 1999/09/30 12:52:52 eric
127 * Depoussierage.
128 * Documentation.
129 *
130 * Revision 2.2 1999/03/01 14:35:21 eric
131 * Modif affichage (operator<<)
132 *
133 *
134 * $Header: /cvsroot/Lorene/C++/Source/Mg3d/mg3d.C,v 1.18 2014/10/13 08:53:07 j_novak Exp $
135 *
136 */
137
138 // C Headers
139 #include <cstdlib>
140 #include <cassert>
141
142 // Lorene headers
143 #include "grilles.h"
144 #include "type_parite.h"
145 #include "utilitaires.h"
146
147 //--------------//
148 // Multi-grille //
149 //--------------//
150
151
152 //=============================================================================
153 // General constructor
154 //=============================================================================
155
156
157 namespace Lorene {
158 Mg3d::Mg3d(int nz, int nbr[], int typr[], int nbt[], int typt, int nbp[],
159 int typp, int* base_r)
160 : nzone(nz), type_t(typt), type_p(typp)
161 {
162
163 // Type d'echantillonnage dans chaque zone
164 type_r = new int[nz];
165 colloc_r = new int[nz] ;
166 bool cheb = (base_r == 0x0) ;
167 for (int i=0 ; i<nz ; i++) {
168 type_r[i] = typr[i];
169 colloc_r[i] = cheb ? BASE_CHEB : base_r[i] ;
170 }
171
172 // Nombre de points
173 nr = new int[nz];
174 nt = new int[nz];
175 np = new int[nz];
176 for (int i=0 ; i<nz ; i++) {
177 nr[i] = nbr[i] ;
178 nt[i] = nbt[i] ;
179 np[i] = nbp[i] ;
180 }
181
182 // Les grilles
183 // -----------
184 g = new Grille3d*[nz] ;
185
186 for (int i=0; i<nz; i++) {
187
188 g[i] = new Grille3d(nr[i], nt[i], np[i], type_r[i], type_t, type_p,
189 colloc_r[i]) ;
190 } // fin de la boucle sur les zones
191
192 // Pointers on derived grids initiated to 0x0:
193 // -------------------------------------------
194
195 set_deriv_0x0() ;
196
197
198 }
199
200 //=============================================================================
201 // Simplified constructor
202 //=============================================================================
203
204 Mg3d::Mg3d(int nz, int nbr, int nbt, int nbp, int typt, int typp,
205 bool compact, bool leg)
206 : nzone(nz),
207 type_t(typt),
208 type_p(typp) {
209
210 // Type of r sampling in each domain:
211 type_r = new int[nz];
212 colloc_r = new int[nz];
213 type_r[0] = RARE ;
214 colloc_r[0] = leg ? BASE_LEG : BASE_CHEB ;
215 for (int l=1; l<nz-1; l++) {
216 type_r[l] = FIN ;
217 colloc_r[l] = leg ? BASE_LEG : BASE_CHEB ;
218 }
219 if (nz > 1) {
220 if (compact) {
221 type_r[nz-1] = UNSURR ;
222 colloc_r[nz-1] = BASE_CHEB ;
223 }
224 else {
225 type_r[nz-1] = FIN ;
226 colloc_r[nz-1] = leg ? BASE_LEG : BASE_CHEB ;
227 }
228 }
229
230 // Same number of points in all domains:
231 nr = new int[nz];
232 nt = new int[nz];
233 np = new int[nz];
234 for (int l=0 ; l<nz ; l++) {
235 nr[l] = nbr ;
236 nt[l] = nbt ;
237 np[l] = nbp ;
238 }
239
240 // Les grilles
241 // -----------
242 g = new Grille3d*[nz] ;
243
244 for (int i=0; i<nz; i++) {
245
246 g[i] = new Grille3d(nr[i], nt[i], np[i], type_r[i], type_t, type_p,
247 colloc_r[i]) ;
248 } // fin de la boucle sur les zones
249
250 // Pointers on derived grids initiated to 0x0:
251 // -------------------------------------------
252
253 set_deriv_0x0() ;
254
255 }
256
257 //=============================================================================
258 // Simplified shell constructor
259 // Note: This does not handle the nucleus or the CED!
260 //=============================================================================
261
262 Mg3d::Mg3d(int nz, int nbr, int nbt, int nbp, int typt, int typp)
263 : nzone(nz),
264 type_t(typt),
265 type_p(typp) {
266
267 // Type of r sampling in each domain:
268 type_r = new int[nz];
269 colloc_r = new int[nz] ;
270 for (int l=0; l<nz; l++) {
271 type_r[l] = FIN ;
272 colloc_r[l] = BASE_CHEB ;
273 }
274
275 // Same number of points in all domains:
276 nr = new int[nz];
277 nt = new int[nz];
278 np = new int[nz];
279 for (int l=0 ; l<nz ; l++) {
280 nr[l] = nbr ;
281 nt[l] = nbt ;
282 np[l] = nbp ;
283 }
284
285 // Les grilles
286 // -----------
287 g = new Grille3d*[nz] ;
288
289 for (int i=0; i<nz; i++) {
290
291 g[i] = new Grille3d(nr[i], nt[i], np[i], type_r[i], type_t, type_p,
292 colloc_r[i]) ;
293
294 } // fin de la boucle sur les zones
295
296 // Pointers on derived grids initiated to 0x0:
297 // -------------------------------------------
298
299 set_deriv_0x0() ;
300
301 }
302
303
304 //=============================================================================
305 // Constructor from a file
306 //=============================================================================
307
308 /*
309 * Construction a partir d'un fichier.
310 * Cette facon de faire est abominable. Cependant je ne vois pas comment
311 * faire autrement... j.a.
312 */
313 Mg3d::Mg3d(FILE* fd, bool read_base)
314 {
315 // Lecture sur le fichier
316 fread_be(&nzone, sizeof(int), 1, fd) ; // nzone
317 nr = new int[nzone] ;
318 fread_be(nr, sizeof(int), nzone, fd) ; // nr
319 nt = new int[nzone] ;
320 fread_be(nt, sizeof(int), nzone, fd) ; // nt
321 np = new int[nzone] ;
322 fread_be(np, sizeof(int), nzone, fd) ; // np
323 type_r = new int[nzone] ;
324 fread_be(type_r, sizeof(int), nzone, fd) ; // type_r
325 fread_be(&type_t, sizeof(int), 1, fd) ; // type_t
326 fread_be(&type_p, sizeof(int), 1, fd) ; // type_p
327 colloc_r = new int[nzone] ;
328 if (read_base)
329 fread_be(colloc_r, sizeof(int), nzone, fd) ; // colloc_r
330
331 // Les grilles
332 // -----------
333
334 g = new Grille3d*[nzone] ;
335 for (int i=0; i<nzone; i++) {
336 if (!read_base) colloc_r[i] = BASE_CHEB ;
337 g[i] = new Grille3d(nr[i], nt[i], np[i], type_r[i], type_t, type_p,
338 colloc_r[i]) ;
339
340 } // fin de la boucle sur les zones
341
342 // Pointers on derived grids initiated to 0x0:
343 // -------------------------------------------
344
345 set_deriv_0x0() ;
346
347 }
348
349 // Destructeur
350 // -----------
351 Mg3d::~Mg3d() {
352
353 del_deriv() ; // Deletes the derived quantities
354
355 delete [] nr ;
356 delete [] nt ;
357 delete [] np ;
358 delete [] type_r ;
359 delete [] colloc_r ;
360 for (int i=0 ; i<nzone ; i++) {
361 delete g[i] ;
362 }
363 delete [] g ;
364
365 }
366
367 //==================================================================
368 // Write in a file
369 //==================================================================
370
371 void Mg3d::sauve(FILE* fd, bool save_base) const {
372 fwrite_be(&nzone, sizeof(int), 1, fd) ; // nzone
373 fwrite_be(nr, sizeof(int), nzone, fd) ; // nr
374 fwrite_be(nt, sizeof(int), nzone, fd) ; // nt
375 fwrite_be(np, sizeof(int), nzone, fd) ; // np
376 fwrite_be(type_r, sizeof(int), nzone, fd) ; // type_r
377 fwrite_be(&type_t, sizeof(int), 1, fd) ; // type_t
378 fwrite_be(&type_p, sizeof(int), 1, fd) ; // type_p
379 if (save_base) {
380 fwrite_be(colloc_r, sizeof(int), nzone, fd) ; // colloc_r
381 }
382 else
383 for (int l=0; l<nzone; l++)
384 if (colloc_r[l] != BASE_CHEB) {
385 cout << "Mg3d::sauve(FILE*, bool) : " << endl ;
386 cout << "The multi-grid is not with Chebyshev basis!!" << endl ;
387 cout << "Consider setting the 'save_base' flaf to 'true'!!"
388 << endl ;
389 arrete() ;
390
391 }
392 }
393
394 //--------------------------//
395 // Surcharge des operateurs //
396 //--------------------------//
397
398 // Operateur <<
399 ostream& operator<<(ostream& o, const Mg3d& g) {
400 const char* tr[3] ;
401 tr[FIN] = "FIN" ; tr[RARE] = "RARE" ; tr[UNSURR] = "UNSURR" ;
402 const char* tang[2] ;
403 tang[NONSYM] = "NONSYM" ; tang[SYM] = "SYM" ;
404 const char* tbase[3] ;
405 tbase[BASE_CHEB] = "Chebyshev" ; tbase[BASE_LEG] = "Legendre" ;
406 tbase[BASE_JAC02] = "Jacobi(0,2)" ;
407 o << "Number of domains: " << g.nzone << endl ;
408 for (int i=0 ; i< g.nzone ; i++) {
409 o << " Domain #" << i << ": "
410 << "nr = " << g.nr[i] << ", " << tr[g.type_r[i]] << "; "
411 << "nt = " << g.nt[i] << ", " << tang[g.type_t] << "; "
412 << "np = " << g.np[i] << ", " << tang[g.type_p] << "; "
413 << "Collocation points type : " << tbase[g.colloc_r[i]] << endl ;
414 }
415 o << endl ;
416 return o ;
417 }
418
419 // Operateur !=
420 bool Mg3d::operator!=(const Mg3d & titi) const {
421
422 if (nzone != titi.nzone) return true ; // C'est vrai que c'est faux...
423
424 for (int i=0 ; i<nzone ; i++) {
425 if (nr[i] != titi.nr[i]) return true ;
426 if (nt[i] != titi.nt[i]) return true ;
427 if (np[i] != titi.np[i]) return true ;
428
429 if (type_r[i] != titi.type_r[i]) return true ;
430 if (colloc_r[i] != titi.colloc_r[i]) return true ;
431 }
432
433 if (type_t != titi.type_t) return true ;
434 if (type_p != titi.type_p) return true ;
435
436 // C'est faux que c'est vrai...
437 return false ;
438 }
439
440
441 //----------------------------------//
442 // Management of derived quantities //
443 //----------------------------------//
444
445 void Mg3d::del_deriv() const {
446
447 if (g_angu != 0x0) delete g_angu ;
448 if (g_angu_1dom != 0x0) delete g_angu_1dom ;
449 if (g_radial != 0x0) delete g_radial ;
450 if (g_twice != 0x0) delete g_twice ;
451 if (g_plus_half != 0x0) delete g_plus_half ;
452 if (g_non_axi != 0x0) delete g_non_axi ;
453
454 set_deriv_0x0() ;
455
456 }
457
458 void Mg3d::set_deriv_0x0() const {
459
460 g_angu = 0x0 ;
461 g_angu_1dom = 0x0 ;
462 g_radial = 0x0 ;
463 g_twice = 0x0 ;
464 g_plus_half = 0x0 ;
465 g_non_axi = 0x0 ;
466 }
467
468
469 //--------------//
470 // Angular grid //
471 //--------------//
472
473 const Mg3d* Mg3d::get_angu() const {
474
475 if (g_angu == 0x0) { // The construction is required
476
477 int* nbr_angu = new int[nzone] ;
478 for (int i=0 ; i<nzone ; i++) {
479 nbr_angu[i] = 1 ;
480 }
481 g_angu = new Mg3d(nzone, nbr_angu, type_r, nt, type_t, np, type_p,
482 colloc_r) ;
483 delete [] nbr_angu ;
484 }
485
486 return g_angu ;
487
488 }
489
490 //-----------------------------//
491 // Angular grid for one domain //
492 //-----------------------------//
493
494 const Mg3d* Mg3d::get_angu_1dom() const {
495
496 if (g_angu_1dom == 0x0) { // The construction is required
497 int* nbr_angu = new int(1) ;
498 int* nbt_angu = new int(nt[0]) ;
499 int* nbp_angu = new int(np[0]) ;
500 int* type_r_angu = new int(FIN) ;
501
502 g_angu_1dom = new Mg3d(1, nbr_angu, type_r_angu, nbt_angu, type_t,
503 nbp_angu, type_p) ;
504 delete nbr_angu ;
505 delete nbt_angu ;
506 delete nbp_angu ;
507 delete type_r_angu ;
508 }
509
510 return g_angu_1dom ;
511
512 }
513
514 //--------------//
515 // Radial grid //
516 //--------------//
517
518 const Mg3d* Mg3d::get_radial() const {
519
520 if (g_radial == 0x0) { // The construction is required
521
522 int* nbr_radial = new int[nzone] ;
523 for (int i=0 ; i<nzone ; i++) {
524 nbr_radial[i] = 1 ;
525 }
526 g_radial = new Mg3d(nzone, nr, type_r, nbr_radial, SYM, nbr_radial,
527 SYM, colloc_r) ;
528 delete [] nbr_radial ;
529 }
530
531 return g_radial ;
532
533 }
534
535 //--------------------------------------//
536 // Grid with twice the number of points //
537 //--------------------------------------//
538
539 const Mg3d* Mg3d::get_twice() const {
540
541 if (g_twice == 0x0) { // The construction is required
542
543 int* nbr = new int[nzone] ;
544 int* nbt = new int[nzone] ;
545 int* nbp = new int[nzone] ;
546
547 for (int l=0; l<nzone; l++) {
548 if (nr[l] == 1) {
549 nbr[l] = 1 ;
550 }
551 else {
552 nbr[l] = 2*nr[l] - 1 ;
553 }
554
555 if (nt[l] == 1) {
556 nbt[l] = 1 ;
557 }
558 else {
559 nbt[l] = 2*nt[l] - 1 ;
560 }
561
562 if (np[l] == 1) {
563 nbp[l] = 1 ;
564 }
565 else {
566 nbp[l] = 2*np[l] ;
567 }
568 }
569
570 g_twice = new Mg3d(nzone, nbr, type_r, nbt, type_t, nbp, type_p, colloc_r) ;
571
572 delete [] nbr ;
573 delete [] nbt ;
574 delete [] nbp ;
575
576 }
577
578 return g_twice ;
579
580 }
581
582
583 //--------------------------------------//
584 // Grid with 50% additional points in r //
585 //--------------------------------------//
586
587 const Mg3d* Mg3d::plus_half() const {
588
589 if (g_plus_half == 0x0) { // The construction is required
590
591 int* nbr = new int[nzone] ;
592
593 for (int l=0; l<nzone; l++) {
594 if (nr[l] == 1)
595 nbr[l] = 1 ;
596 else
597 nbr[l] = (3*nr[l])/2 ;
598 }
599
600 g_plus_half = new Mg3d(nzone, nbr, type_r, nt, type_t, np, type_p, colloc_r) ;
601
602 delete [] nbr ;
603
604
605 }
606
607 return g_plus_half ;
608
609 }
610
611 //----------------------------------------------//
612 // Grid for rotations (5/4 points in theta/phi) //
613 //----------------------------------------------//
614
615 const Mg3d* Mg3d::get_non_axi() const {
616
617 if (g_non_axi == 0x0) { // The construction is required
618
619 int* nbt = new int[nzone] ;
620 int* nbp = new int[nzone] ;
621
622 for (int l=0; l<nzone; l++) {
623 if (nt[l] < 5)
624 nbt[l] = 5 ;
625 else
626 nbt[l] = nt[l] ;
627 if (np[l] < 4)
628 nbp[l] = 4 ;
629 else
630 nbp[l] = np[l] ;
631 }
632
633 g_non_axi = new Mg3d(nzone, nr, type_r, nbt, type_t, nbp, type_p, colloc_r) ;
634
635 delete [] nbt ;
636 delete [] nbp ;
637
638
639 }
640
641 return g_non_axi ;
642
643 }
644
645
646 bool Mg3d::operator==(const Mg3d& mgi) const {
647
648 bool resu = true ;
649
650 if (mgi.get_nzone() != nzone) {
651 resu = false ;
652 }
653 else {
654 for (int i=0; i<nzone; i++) {
655 if (mgi.get_nr(i) != nr[i]) resu = false ;
656 if (mgi.get_np(i) != np[i]) resu = false ;
657 if (mgi.get_nt(i) != nt[i]) resu = false ;
658 if (mgi.get_type_r(i) != type_r[i]) resu =false ;
659 if (mgi.get_colloc_r(i) != colloc_r[i]) resu = false ;
660 }
661 }
662
663 if (mgi.get_type_t() != type_t) resu = false ;
664 if (mgi.get_type_p() != type_p) resu = false ;
665
666 return resu ;
667
668 }
669 }