Debian Codegraph @

Export/MagNS/read_mag_ns.C

raw
1 /*
2 * Reads magnetized neutron star initial data, exporting Lorene structures
3 * onto standard C arrays (double[]) on a Cartesian grid.
4 */
5
6 /*
7 * Copyright (c) 2002 Eric Gourgoulhon
8 * Copyright (c) 2009 Jerome Novak
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 read_mag_ns_C[] = "$Header: /cvsroot/Lorene/Export/MagNS/read_mag_ns.C,v 1.3 2014/10/13 08:54:06 j_novak Exp $" ;
28
29 /*
30 * $Id: read_mag_ns.C,v 1.3 2014/10/13 08:54:06 j_novak Exp $
31 * $Log: read_mag_ns.C,v $
32 * Revision 1.3 2014/10/13 08:54:06 j_novak
33 * Lorene classes and functions now belong to the namespace Lorene.
34 *
35 * Revision 1.2 2014/05/13 10:06:45 j_novak
36 * Update to take unto account the change in Lorene magnetic units.
37 *
38 * Revision 1.1 2009/11/19 16:15:21 j_novak
39 * Export class for magnetized neutron stars.
40 *
41 *
42 *
43 * $Header: /cvsroot/Lorene/Export/MagNS/read_mag_ns.C,v 1.3 2014/10/13 08:54:06 j_novak Exp $
44 *
45 */
46
47 // C headers
48 #include <cstdlib>
49
50 // Definition of Mag_NS class
51 #include "mag_ns.h"
52
53 using namespace Lorene ;
54
55 int main() {
56
57 // Reads Cartesian grid parameters
58 // -------------------------------
59 ifstream fparam("read_mag_ns.par") ;
60 char comment[120] ;
61 char datafile[120] ;
62 int nx, ny, nz ;
63 double x_min, x_max, y_min, y_max, z_min, z_max ;
64 fparam.getline(comment, 120) ;
65 fparam.getline(comment, 120) ;
66 fparam.getline(datafile, 120) ;
67 fparam.getline(comment, 120) ;
68 fparam.getline(comment, 120) ;
69 fparam.getline(comment, 120) ;
70 fparam >> nx ; fparam.getline(comment, 120) ;
71 fparam >> ny ; fparam.getline(comment, 120) ;
72 fparam >> nz ; fparam.getline(comment, 120) ;
73 fparam >> x_min ; fparam >> x_max ; fparam.getline(comment, 120) ;
74 fparam >> y_min ; fparam >> y_max ; fparam.getline(comment, 120) ;
75 fparam >> z_min ; fparam >> z_max ; fparam.getline(comment, 120) ;
76 fparam.close() ;
77
78 cout << "File containing the initial data on the spectral grid:"
79 << endl ;
80 cout << datafile << endl << endl ;
81 cout << "Cartesian grid : " << endl ;
82 cout << "---------------- " << endl ;
83 cout << " Number of points in the x direction : " << nx << endl ;
84 cout << " Number of points in the y direction : " << ny << endl ;
85 cout << " Number of points in the z direction : " << nz << endl ;
86 cout << " x_min, x_max : " << x_min << " , " << x_max << endl ;
87 cout << " y_min, y_max : " << y_min << " , " << y_max << endl ;
88 cout << " z_min, z_max : " << z_min << " , " << z_max << endl ;
89
90 // Construction of the Cartesian grid
91 // ----------------------------------
92
93 int nbp = nx*ny*nz ;
94 double* const xi = new double[nbp] ;
95 double* const yi = new double[nbp] ;
96 double* const zi = new double[nbp] ;
97
98 double dx = (x_max - x_min) / double(nx - 1) ;
99 double dy = (y_max - y_min) / double(ny - 1) ;
100 double dz = (z_max - z_min) / double(nz - 1) ;
101
102 double* pxi = xi ;
103 double* pyi = yi ;
104 double* pzi = zi ;
105
106 for (int k=0; k<nz; k++) {
107
108 double z = z_min + dz * k ;
109
110 for (int j=0; j<ny; j++) {
111
112 double y = y_min + dy * j ;
113
114 for (int i=0; i<nx; i++) {
115
116 *pxi = x_min + dx * i ;
117 *pyi = y ;
118 *pzi = z ;
119
120 pxi++ ;
121 pyi++ ;
122 pzi++ ;
123 }
124 }
125 }
126
127 // Read of the initial data file and computation of the
128 // fields at the Cartesian grid points
129 // ----------------------------------------------------
130
131 Mag_NS magstar(nbp, xi, yi, zi, datafile) ;
132
133 cout << endl << magstar << endl ;
134
135
136 // Save in a binary file
137 // ---------------------
138 FILE* finib = fopen("inib.d", "w") ;
139 magstar.save_bin(finib) ;
140 fclose( finib ) ;
141
142 // Save in a formatted file
143 // ------------------------
144 ofstream finif("inif.d") ;
145 magstar.save_form(finif) ;
146 finif.close() ;
147
148 // Create a new object from a binary file
149 //---------------------------------------
150 // finib = fopen("inib.d", "r") ;
151 // Mag_NS magstar2(finib) ;
152 // fclose( finib ) ;
153
154 // cout << endl << "Magstar read in the binary file : " << magstar2 << endl ;
155
156 // finib = fopen("inib2.d", "w") ;
157 // magstar2.save_bin(finib) ;
158 // fclose( finib ) ;
159
160 // Create a new object from a formatted file
161 //---------------------------------------
162 // ifstream finif3("inif.d") ;
163 // Mag_NS magstar3(finif3) ;
164 // finif3.close() ;
165
166 // cout << endl << "Binary read in the formatted file : " << magstar3 << endl ;
167
168 // finif.open("inif3.d") ;
169 // magstar3.save_form(finif) ;
170 // finif.close() ;
171
172 // Clean exit
173 // ----------
174
175 delete [] xi ;
176 delete [] yi ;
177 delete [] zi ;
178
179 return EXIT_SUCCESS ;
180 }