Debian Codegraph @

C++/Source/Grille_cart/grille_cart.C

raw
1 /*
2 * Methods of class Grille_cart
3 *
4 */
5
6 /*
7 * Copyright (c) 2002 Nicolas Chamel
8 * Copyright (c) 2002 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 version 2
14 * as published by the Free Software Foundation.
15 *
16 * LORENE is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with LORENE; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27 char grille_cart_C[] = "$Header: /cvsroot/Lorene/C++/Source/Grille_cart/grille_cart.C,v 1.3 2002/10/16 14:36:37 j_novak Exp $" ;
28
29 /*
30 * $Id: grille_cart.C,v 1.3 2002/10/16 14:36:37 j_novak Exp $
31 * $Log: grille_cart.C,v $
32 * Revision 1.3 2002/10/16 14:36:37 j_novak
33 * Reorganization of #include instructions of standard C++, in order to
34 * use experimental version 3 of gcc.
35 *
36 * Revision 1.2 2002/03/15 13:14:56 n_chamel
37 * Introduced the private method init_grid_points, to be called by
38 * the constructors
39 *
40 * Revision 1.1 2002/03/07 15:37:05 n_chamel
41 * First version
42 *
43 *
44 *
45 *
46 * $Header: /cvsroot/Lorene/C++/Source/Grille_cart/grille_cart.C,v 1.3 2002/10/16 14:36:37 j_novak Exp $
47 *
48 */
49
50 // C headers
51 #include <stdlib.h>
52 #include <math.h>
53 #include <assert.h>
54
55
56 // Lorene headers
57 #include "headcpp.h"
58 #include "grille_cart.h"
59 #include "type_parite.h"
60 #include "utilitaires.h"
61
62 //-------------------------//
63 // Constructors //
64 //-------------------------//
65
66
67 // Standard constructor
68 // ---------------
69 Grille_cart::Grille_cart(int nx_i, int ny_i, int nz_i, int type_x_i, int type_y_i, int type_z_i) :
70 nx(nx_i),
71 ny(ny_i),
72 nz(nz_i),
73 type_x(type_x_i),
74 type_y(type_y_i),
75 type_z(type_z_i)
76 {
77
78 init_grid_points() ;
79
80 }
81
82 // Copy constructor
83 // ------------
84 Grille_cart::Grille_cart(const Grille_cart& grid) :
85 nx(grid.nx),
86 ny(grid.ny),
87 nz(grid.nz),
88 type_x(grid.type_x),
89 type_y(grid.type_y),
90 type_z(grid.type_z)
91 {
92
93 x = new double [nx] ;
94 y = new double [ny] ;
95 z = new double [nz] ;
96
97 for (int i=0; i<nx; i++) {
98 x[i] = grid.x[i] ;
99 }
100
101 for (int i=0; i<ny; i++) {
102 y[i] = grid.y[i] ;
103 }
104
105 for (int i=0; i<nz; i++) {
106 z[i] = grid.z[i] ;
107 }
108
109 }
110
111
112 // Constructor from a file
113 // ----------------
114 Grille_cart::Grille_cart(FILE* fich) {
115
116 fread_be(&nx, sizeof(int), 1, fich) ;
117 fread_be(&ny, sizeof(int), 1, fich) ;
118 fread_be(&nz, sizeof(int), 1, fich) ;
119 fread_be(&type_x, sizeof(int), 1, fich) ;
120 fread_be(&type_y, sizeof(int), 1, fich) ;
121 fread_be(&type_z, sizeof(int), 1, fich) ;
122
123 init_grid_points() ;
124 }
125
126 //--------------------//
127 // Destructor //
128 //--------------------//
129
130 Grille_cart::~Grille_cart(){
131
132 delete [] x ;
133 delete [] y ;
134 delete [] z ;
135
136 }
137
138 //---------------------------//
139 // Initialisation of the arrays x, y and z //
140 //---------------------------//
141
142
143 void Grille_cart::init_grid_points() {
144
145 x = new double [nx] ;
146 y = new double [ny] ;
147 z = new double [nz] ;
148
149 // Computation of x[i]
150 // --------------
151
152 switch (type_x) {
153
154 case UNIFORM : {
155
156 if (nx == 1) {
157 x[0] = 0. ;
158 }
159 else {
160 double dx = 2. / double(nx-1) ;
161 for (int i=0; i<nx; i++) {
162 x[i] = -1. + i * dx ;
163 }
164 }
165 break ;
166 }
167
168 case FIN : {
169 assert( nx > 1) ;
170 double xx = M_PI/double(nx-1) ;
171 for (int i=0; i<nx; i++) {
172 x[i] = -cos(xx*i) ;
173 }
174 break ;
175 }
176
177 case RARE : {
178 assert( nx > 1) ;
179 double xx = M_PI/double(2*(nx-1)) ;
180 for (int i=0; i<nx; i++) {
181 x[i] = sin(xx*i) ;
182 }
183 break ;
184 }
185
186 default : {
187 cout << " Grille_cart::Grille_cart : unknown sampling type in x !" << endl ;
188 abort() ;
189 }
190 }
191
192 // Computation of y[i]
193 // --------------
194
195 switch (type_y) {
196
197 case UNIFORM : {
198
199 if (ny == 1) {
200 y[0] = 0. ;
201 }
202 else {
203 double dy = 2. / double(ny-1) ;
204 for (int i=0; i<ny; i++) {
205 y[i] = -1. + i * dy ;
206 }
207 }
208 break ;
209 }
210
211 case FIN : {
212 assert( ny > 1) ;
213 double yy = M_PI/double(ny-1) ;
214 for (int i=0; i<ny; i++) {
215 y[i] = -cos(yy*i) ;
216 }
217 break ;
218 }
219
220 case RARE : {
221 assert( ny > 1) ;
222 double yy = M_PI/double(2*(ny-1)) ;
223 for (int i=0; i<ny; i++) {
224 y[i] = sin(yy*i) ;
225 }
226 break ;
227 }
228
229 default : {
230 cout << " Grille_cart::Grille_cart : unknown sampling type in y !" << endl ;
231 abort() ;
232 }
233 }
234
235 // Computation of z[i]
236 // --------------
237
238 switch (type_z) {
239
240 case UNIFORM : {
241
242 if (nz == 1) {
243 z[0] = 0. ;
244 }
245 else {
246 double dz = 2. / double(nz-1) ;
247 for (int i=0; i<nz; i++) {
248 z[i] = -1. + i * dz ;
249 }
250 }
251 break ;
252 }
253
254 case FIN : {
255 assert( nz > 1) ;
256 double zz = M_PI/double(nz-1) ;
257 for (int i=0; i<nz; i++) {
258 z[i] = -cos(zz*i) ;
259 }
260 break ;
261 }
262
263 case RARE : {
264 assert( nz > 1) ;
265 double zz = M_PI/double(2*(nz-1)) ;
266 for (int i=0; i<nz; i++) {
267 z[i] = sin(zz*i) ;
268 }
269 break ;
270 }
271
272 default : {
273 cout << " Grille_cart::Grille_cart : unknown sampling type in z !" << endl ;
274 abort() ;
275 }
276 }
277 }
278
279
280 //--------------------//
281 // Outputs //
282 //--------------------//
283
284 // Write in a binary file
285 void Grille_cart::sauve(FILE* fich) const {
286
287 fwrite_be(&nx, sizeof(int), 1, fich) ;
288 fwrite_be(&ny, sizeof(int), 1, fich) ;
289 fwrite_be(&nz, sizeof(int), 1, fich) ;
290 fwrite_be(&type_x, sizeof(int), 1, fich) ;
291 fwrite_be(&type_y, sizeof(int), 1, fich) ;
292 fwrite_be(&type_z, sizeof(int), 1, fich) ;
293
294 }
295
296
297 // Formatted output
298 ostream& operator<<(ostream& ost, const Grille_cart& grid) {
299
300 ost << "Cartesian grid:" << endl ;
301 ost << " Number of points and type of sampling in x: " << grid.get_nx() << " "
302 << grid.get_type_x() << endl ;
303 ost << " Number of points and type of sampling in y: " << grid.get_ny() << " "
304 << grid.get_type_y() << endl ;
305 ost << " Number of points and type of sampling in z: " << grid.get_nz() << " "
306 << grid.get_type_z() << endl ;
307
308 return ost ;
309 }
310
311
312
313
314
315