Debian Codegraph @

Export/C++/Source/mag_ns.C

raw
1 /*
2 * Methods of class Mag_NS (magnetized neutron star exportation)
3 *
4 * (see file mag_ns.h for documentation).
5 */
6
7 /*
8 * Copyright (c) 2002 Eric Gourgoulhon
9 * Copyright (c) 2009 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 version 2
15 * as published by the Free Software Foundation.
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 mag_ns_C[] = "$Header: /cvsroot/Lorene/Export/C++/Source/mag_ns.C,v 1.3 2014/10/13 08:54:06 j_novak Exp $" ;
30
31 /*
32 * $Id: mag_ns.C,v 1.3 2014/10/13 08:54:06 j_novak Exp $
33 * $Log: mag_ns.C,v $
34 * Revision 1.3 2014/10/13 08:54:06 j_novak
35 * Lorene classes and functions now belong to the namespace Lorene.
36 *
37 * Revision 1.2 2009/11/22 16:05:05 j_novak
38 * *** empty log message ***
39 *
40 * Revision 1.1 2009/11/19 16:15:21 j_novak
41 * Export class for magnetized neutron stars.
42 *
43 *
44 *
45 * $Header: /cvsroot/Lorene/Export/C++/Source/mag_ns.C,v 1.3 2014/10/13 08:54:06 j_novak Exp $
46 *
47 */
48
49 #include "../Include/mag_ns.h"
50
51 namespace Lorene {
52 void write_lines(ostream& fich, int dpl, const double* pdata, int np) ;
53
54
55 //-----------------------------------------//
56 // Constructor from a binary file //
57 //-----------------------------------------//
58
59 Mag_NS::Mag_NS(FILE* fich) {
60
61 fread(eos_name, sizeof(char), 100, fich) ;
62 fread(&gamma_poly, sizeof(double), 1, fich) ;
63 fread(&kappa_poly, sizeof(double), 1, fich) ;
64 fread(&omega, sizeof(double), 1, fich) ;
65 fread(&rho_c, sizeof(double), 1, fich) ;
66 fread(&eps_c, sizeof(double), 1, fich) ;
67 fread(&mass_b, sizeof(double), 1, fich) ;
68 fread(&mass_g, sizeof(double), 1, fich) ;
69 fread(&r_eq, sizeof(double), 1, fich) ;
70 fread(&r_p, sizeof(double), 1, fich) ;
71 fread(&angu_mom, sizeof(double), 1, fich) ;
72 fread(&T_over_W, sizeof(double), 1, fich) ;
73 fread(&magn_mom, sizeof(double), 1, fich) ;
74 fread(&b_z_pole, sizeof(double), 1, fich) ;
75 fread(&b_z_eq, sizeof(double), 1, fich) ;
76 fread(&np, sizeof(int), 1, fich) ;
77
78 alloc_memory() ;
79
80 fread(xx, sizeof(double), np, fich) ;
81 fread(yy, sizeof(double), np, fich) ;
82 fread(zz, sizeof(double), np, fich) ;
83 fread(nnn, sizeof(double), np, fich) ;
84 fread(beta_x, sizeof(double), np, fich) ;
85 fread(beta_y, sizeof(double), np, fich) ;
86 fread(beta_z, sizeof(double), np, fich) ;
87 fread(g_xx, sizeof(double), np, fich) ;
88 fread(g_xy, sizeof(double), np, fich) ;
89 fread(g_xz, sizeof(double), np, fich) ;
90 fread(g_yy, sizeof(double), np, fich) ;
91 fread(g_yz, sizeof(double), np, fich) ;
92 fread(g_zz, sizeof(double), np, fich) ;
93 fread(k_xx, sizeof(double), np, fich) ;
94 fread(k_xy, sizeof(double), np, fich) ;
95 fread(k_xz, sizeof(double), np, fich) ;
96 fread(k_yy, sizeof(double), np, fich) ;
97 fread(k_yz, sizeof(double), np, fich) ;
98 fread(k_zz, sizeof(double), np, fich) ;
99 fread(nbar, sizeof(double), np, fich) ;
100 fread(ener_spec, sizeof(double), np, fich) ;
101 fread(u_euler_x, sizeof(double), np, fich) ;
102 fread(u_euler_y, sizeof(double), np, fich) ;
103 fread(u_euler_z, sizeof(double), np, fich) ;
104 fread(bb_x, sizeof(double), np, fich) ;
105 fread(bb_y, sizeof(double), np, fich) ;
106 fread(bb_z, sizeof(double), np, fich) ;
107 fread(jj_t, sizeof(double), np, fich) ;
108 fread(jj_x, sizeof(double), np, fich) ;
109 fread(jj_y, sizeof(double), np, fich) ;
110 fread(jj_z, sizeof(double), np, fich) ;
111
112
113 }
114
115 //--------------------------------------------//
116 // Constructor from a formatted file //
117 //--------------------------------------------//
118
119 Mag_NS::Mag_NS(ifstream& fich) {
120
121
122 char comment[120] ;
123
124 fich.getline(eos_name,100) ;
125 fich >> gamma_poly ; fich.getline(comment, 120) ;
126 fich >> kappa_poly ; fich.getline(comment, 120) ;
127 fich >> omega ; fich.getline(comment, 120) ;
128 fich >> rho_c ; fich.getline(comment, 120) ;
129 fich >> eps_c ; fich.getline(comment, 120) ;
130 fich >> mass_b ; fich.getline(comment, 120) ;
131 fich >> mass_g ; fich.getline(comment, 120) ;
132 fich >> r_eq ; fich.getline(comment, 120) ;
133 fich >> r_p ; fich.getline(comment, 120) ;
134 fich >> angu_mom ; fich.getline(comment, 120) ;
135 fich >> T_over_W ; fich.getline(comment, 120) ;
136 fich >> magn_mom ; fich.getline(comment, 120) ;
137 fich >> b_z_pole ; fich.getline(comment, 120) ;
138 fich >> b_z_eq ; fich.getline(comment, 120) ;
139 fich >> np ; fich.getline(comment, 120) ;
140
141 alloc_memory() ;
142
143 double* field[] = {xx, yy, zz,
144 nnn, beta_x, beta_y, beta_z,
145 g_xx, g_xy, g_xz, g_yy, g_yz, g_zz,
146 k_xx, k_xy, k_xz, k_yy, k_yz, k_zz,
147 nbar, ener_spec, u_euler_x, u_euler_y, u_euler_z,
148 bb_x, bb_y, bb_z,
149 jj_t, jj_x, jj_y, jj_z} ;
150
151 for (int j=0; j<31; j++) {
152
153 double* pdata = field[j] ;
154
155 for (int i=0; i<np; i++) {
156 fich >> *pdata ;
157 pdata++ ;
158 }
159 }
160
161 }
162
163
164
165
166 //--------------------------//
167 // Destructor //
168 //--------------------------//
169
170
171 Mag_NS::~Mag_NS() {
172
173 delete [] xx ;
174 delete [] yy ;
175 delete [] zz ;
176
177 delete [] nnn ;
178
179 delete [] beta_x ;
180 delete [] beta_y ;
181 delete [] beta_z ;
182
183 delete [] g_xx ;
184 delete [] g_xy ;
185 delete [] g_xz ;
186 delete [] g_yy ;
187 delete [] g_yz ;
188 delete [] g_zz ;
189
190 delete [] k_xx ;
191 delete [] k_xy ;
192 delete [] k_xz ;
193 delete [] k_yy ;
194 delete [] k_yz ;
195 delete [] k_zz ;
196
197 delete [] nbar ;
198 delete [] ener_spec ;
199
200 delete [] u_euler_x ;
201 delete [] u_euler_y ;
202 delete [] u_euler_z ;
203
204 delete [] bb_x ;
205 delete [] bb_y ;
206 delete [] bb_z ;
207
208 delete [] jj_t ;
209 delete [] jj_x ;
210 delete [] jj_y ;
211 delete [] jj_z ;
212
213 }
214
215
216 //--------------------------//
217 // Memory allocation //
218 //--------------------------//
219
220 void Mag_NS::alloc_memory() {
221
222 xx = new double[np] ;
223 yy = new double[np] ;
224 zz = new double[np] ;
225
226 nnn = new double[np] ;
227
228 beta_x = new double[np] ;
229 beta_y = new double[np] ;
230 beta_z = new double[np] ;
231
232 g_xx = new double[np] ;
233 g_xy = new double[np] ;
234 g_xz = new double[np] ;
235 g_yy = new double[np] ;
236 g_yz = new double[np] ;
237 g_zz = new double[np] ;
238
239 k_xx = new double[np] ;
240 k_xy = new double[np] ;
241 k_xz = new double[np] ;
242 k_yy = new double[np] ;
243 k_yz = new double[np] ;
244 k_zz = new double[np] ;
245
246 nbar = new double[np] ;
247 ener_spec = new double[np] ;
248
249 u_euler_x = new double[np] ;
250 u_euler_y = new double[np] ;
251 u_euler_z = new double[np] ;
252
253 bb_x = new double[np] ;
254 bb_y = new double[np] ;
255 bb_z = new double[np] ;
256
257 jj_t = new double[np] ;
258 jj_x = new double[np] ;
259 jj_y = new double[np] ;
260 jj_z = new double[np] ;
261
262 }
263
264 //--------------------------//
265 // Outputs //
266 //--------------------------//
267
268 ostream& operator<<(ostream& ost, const Mag_NS& mns) {
269
270 ost << "Magnetized neutron star :" << endl ;
271 ost << "-------------------------" << endl ;
272 ost << " EOS : " << mns.eos_name << endl ;
273 ost << " gamma = " << mns.gamma_poly << endl ;
274 ost << " kappa = " << mns.kappa_poly << " rho_nuc c^2 / n_nuc^gamma" << endl ;
275 ost << " Omega : " << mns.omega << " rad/s" << endl ;
276 ost << " Central density : " << mns.rho_c << " kg/m^3" << endl ;
277 ost << " Central specific energy : " << mns.eps_c << " c^2" << endl ;
278 ost << " Baryon mass : " << mns.mass_b
279 << " M_sol" << endl ;
280 ost << " Gravitational mass : " << mns.mass_g
281 << " M_sol" << endl ;
282 ost << " Coordinate equatorial radius : " << mns.r_eq << " km" << endl ;
283 ost << " Coordinate polar radius : " << mns.r_p << " km" << endl ;
284 ost << " Total angular momentum : " << mns.angu_mom
285 << " G M_sol^2/c" << endl ;
286 ost << " T/W : " << mns.T_over_W << endl ;
287 ost << " Magnetic momentum : " << mns.magn_mom
288 << " A m^2" << endl ;
289 ost << " Magnetic field at north pole : " << mns.b_z_pole
290 << " x 10^9 T" << endl ;
291 ost << " Tangent magnetic field equatorial value : " << mns.b_z_eq
292 << " x 10^9 T" << endl ;
293
294 return ost ;
295 }
296
297
298 //----------------------------------//
299 // Save in a binary file //
300 //----------------------------------//
301
302 void Mag_NS::save_bin(FILE* fresu) const {
303
304 fwrite(eos_name, sizeof(char), 100, fresu) ;
305 fwrite(&gamma_poly, sizeof(double), 1, fresu) ;
306 fwrite(&kappa_poly, sizeof(double), 1, fresu) ;
307 fwrite(&omega, sizeof(double), 1, fresu) ;
308 fwrite(&rho_c, sizeof(double), 1, fresu) ;
309 fwrite(&eps_c, sizeof(double), 1, fresu) ;
310 fwrite(&mass_b, sizeof(double), 1, fresu) ;
311 fwrite(&mass_g, sizeof(double), 1, fresu) ;
312 fwrite(&r_eq, sizeof(double), 1, fresu) ;
313 fwrite(&r_p, sizeof(double), 1, fresu) ;
314 fwrite(&angu_mom, sizeof(double), 1, fresu) ;
315 fwrite(&T_over_W, sizeof(double), 1, fresu) ;
316 fwrite(&magn_mom, sizeof(double), 1, fresu) ;
317 fwrite(&b_z_pole, sizeof(double), 1, fresu) ;
318 fwrite(&b_z_eq, sizeof(double), 1, fresu) ;
319 fwrite(&np, sizeof(int), 1, fresu) ;
320
321 fwrite(xx, sizeof(double), np, fresu) ;
322 fwrite(yy, sizeof(double), np, fresu) ;
323 fwrite(zz, sizeof(double), np, fresu) ;
324 fwrite(nnn, sizeof(double), np, fresu) ;
325 fwrite(beta_x, sizeof(double), np, fresu) ;
326 fwrite(beta_y, sizeof(double), np, fresu) ;
327 fwrite(beta_z, sizeof(double), np, fresu) ;
328 fwrite(g_xx, sizeof(double), np, fresu) ;
329 fwrite(g_xy, sizeof(double), np, fresu) ;
330 fwrite(g_xz, sizeof(double), np, fresu) ;
331 fwrite(g_yy, sizeof(double), np, fresu) ;
332 fwrite(g_yz, sizeof(double), np, fresu) ;
333 fwrite(g_zz, sizeof(double), np, fresu) ;
334 fwrite(k_xx, sizeof(double), np, fresu) ;
335 fwrite(k_xy, sizeof(double), np, fresu) ;
336 fwrite(k_xz, sizeof(double), np, fresu) ;
337 fwrite(k_yy, sizeof(double), np, fresu) ;
338 fwrite(k_yz, sizeof(double), np, fresu) ;
339 fwrite(k_zz, sizeof(double), np, fresu) ;
340 fwrite(nbar, sizeof(double), np, fresu) ;
341 fwrite(ener_spec, sizeof(double), np, fresu) ;
342 fwrite(u_euler_x, sizeof(double), np, fresu) ;
343 fwrite(u_euler_y, sizeof(double), np, fresu) ;
344 fwrite(u_euler_z, sizeof(double), np, fresu) ;
345 fwrite(bb_x, sizeof(double), np, fresu) ;
346 fwrite(bb_y, sizeof(double), np, fresu) ;
347 fwrite(bb_z, sizeof(double), np, fresu) ;
348 fwrite(jj_t, sizeof(double), np, fresu) ;
349 fwrite(jj_x, sizeof(double), np, fresu) ;
350 fwrite(jj_y, sizeof(double), np, fresu) ;
351 fwrite(jj_z, sizeof(double), np, fresu) ;
352
353 }
354
355
356 //----------------------------------//
357 // Save in a formatted file //
358 //----------------------------------//
359
360 void Mag_NS::save_form(ofstream& fich) const {
361
362 fich.precision(13) ;
363 fich.setf(ios::scientific, ios::floatfield) ;
364
365 fich << eos_name << endl ;
366 fich << gamma_poly << " gamma_poly" << endl ;
367 fich << kappa_poly << " kappa_poly [rho_nuc c^2 / n_nuc^gamma]" << endl ;
368 fich << omega << " Omega [rad/s]" << endl ;
369 fich << rho_c << " central density [kg.m^{-3}]" << endl ;
370 fich << eps_c << " central specific internal energy [c^2]" << endl ;
371 fich << mass_b << " baryon mass [M_sol]" << endl ;
372 fich << mass_g << " gravitational mass [M_sol]" << endl ;
373 fich << r_eq << " equatorial coordinate radius [km]" << endl ;
374 fich << r_p << " polar coordinate radius [km]" << endl ;
375 fich << angu_mom << " angular momentum [G M_sol^2/c]" << endl ;
376 fich << T_over_W << " T/W" << endl ;
377 fich << magn_mom << " Magnetic momentum [A m^2]" << endl ;
378 fich << b_z_pole << " Magnetic field at the pole [10^9 T]" << endl ;
379 fich << b_z_eq << " Magnetic field at the equator [10^9 T]" << endl ;
380 fich << np << " Number of grid points" << endl ;
381
382 int dpl = 6 ; // number of data per line
383
384 const double* field[] = {xx, yy, zz,
385 nnn, beta_x, beta_y, beta_z,
386 g_xx, g_xy, g_xz, g_yy, g_yz, g_zz,
387 k_xx, k_xy, k_xz, k_yy, k_yz, k_zz,
388 nbar, ener_spec, u_euler_x, u_euler_y, u_euler_z,
389 bb_x, bb_y, bb_z,
390 jj_t, jj_x, jj_y, jj_z} ;
391
392 for (int j=0; j<31; j++) {
393
394 write_lines(fich, dpl, field[j], np) ;
395
396 }
397
398 }
399
400 }