LORENE
meos.C
1 /*
2  * Methods of class MEos
3  *
4  */
5 
6 /*
7  * Copyright (c) 2002 Michal Bejger, Eric Gourgoulhon & Leszek Zdunik
8  *
9  * This file is part of LORENE.
10  *
11  * LORENE is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2
13  * as published by the Free Software Foundation.
14  *
15  * LORENE is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with LORENE; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */
25 
26 
27 
28 /*
29  * $Id: meos.C,v 1.11 2023/04/21 09:21:45 j_novak Exp $
30  * $Log: meos.C,v $
31  * Revision 1.11 2023/04/21 09:21:45 j_novak
32  * Added some asserts.
33  *
34  * Revision 1.10 2017/12/22 09:21:50 j_novak
35  * Corrected the constructor from a binary file
36  *
37  * Revision 1.9 2017/12/21 16:25:28 j_novak
38  * Building from binary file now possible, too for Meos.
39  *
40  * Revision 1.8 2017/12/15 15:36:38 j_novak
41  * Improvement of the MEos class. Implementation of automatic offset computation accross different EoSs/domains.
42  *
43  * Revision 1.7 2016/12/05 16:17:52 j_novak
44  * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
45  *
46  * Revision 1.6 2014/10/13 08:52:54 j_novak
47  * Lorene classes and functions now belong to the namespace Lorene.
48  *
49  * Revision 1.5 2014/10/06 15:13:07 j_novak
50  * Modified #include directives to use c++ syntax.
51  *
52  * Revision 1.4 2004/04/01 11:09:26 e_gourgoulhon
53  * Copy constructor of MEos: explicit call to the default constructor of
54  * base class Eos.
55  *
56  * Revision 1.3 2002/10/16 14:36:35 j_novak
57  * Reorganization of #include instructions of standard C++, in order to
58  * use experimental version 3 of gcc.
59  *
60  * Revision 1.2 2002/04/09 14:42:29 e_gourgoulhon
61  * Dummy argument in assignment.
62  *
63  * Revision 1.1 2002/04/09 14:40:36 e_gourgoulhon
64  * Methods for new class MEos
65  *
66  *
67  *
68  * $Header: /cvsroot/Lorene/C++/Source/Eos/meos.C,v 1.11 2023/04/21 09:21:45 j_novak Exp $
69  *
70  */
71 
72 // C headers
73 #include <cstdlib>
74 #include <cassert>
75 
76 // Lorene headers
77 #include "headcpp.h"
78 #include "eos.h"
79 #include "utilitaires.h"
80 #include "param.h"
81 
82 
83  //--------------------------------------//
84  // Constructors //
85  //--------------------------------------//
86 
87 namespace Lorene {
88 MEos::MEos(int ndom_i, const Eos** mono_eos_i) : ndom(ndom_i) ,
89  constructed_from_file(false) {
90 
91 
92  mono_eos = new const Eos* [ndom] ;
93 
94  for (int l=0; l<ndom; l++) {
95  mono_eos[l] = mono_eos_i[l] ;
96  }
97 
98 }
99 
100 
101 MEos::MEos(const Eos& eos1, const Eos& eos2) : ndom(2) ,
102  constructed_from_file(false) {
103 
104  mono_eos = new const Eos* [ndom] ;
105 
106  mono_eos[0] = &eos1 ;
107  mono_eos[1] = &eos2 ;
108 
109 }
110 
111 MEos::MEos(const Eos& eos1, const Eos& eos2, const Eos& eos3) : ndom(3) ,
112  constructed_from_file(false) {
113 
114  mono_eos = new const Eos* [ndom] ;
115 
116  mono_eos[0] = &eos1 ;
117  mono_eos[1] = &eos2 ;
118  mono_eos[2] = &eos3 ;
119 
120 }
121 
122 MEos::MEos(const Eos& eos1, const Eos& eos2, const Eos& eos3, const Eos& eos4) : ndom(4) ,
123  constructed_from_file(false) {
124 
125  mono_eos = new const Eos* [ndom] ;
126 
127  mono_eos[0] = &eos1 ;
128  mono_eos[1] = &eos2 ;
129  mono_eos[2] = &eos3 ;
130  mono_eos[3] = &eos4 ;
131 
132 }
133 
134 // Copy constructor
135 MEos::MEos(const MEos& meos) : Eos(),
136  ndom(meos.ndom),
137  constructed_from_file(false) {
138 
139  mono_eos = new const Eos* [ndom] ;
140 
141  for (int l=0; l<ndom; l++) {
142  mono_eos[l] = meos.mono_eos[l] ;
143  }
144 
145 }
146 
147 
148 // Constructor from a binary file
149 MEos::MEos(FILE* fich) : Eos(fich), constructed_from_file(true) {
150 
151  fread_be(&ndom, sizeof(int), 1, fich) ;
152 
153  mono_eos = new const Eos* [ndom] ;
154  ofstream temp("meos_is_being_built.d") ;
155  temp << " " << flush ;
156  temp.close() ;
157 
158  for (int l=ndom-1; l>=0; l--) { // Reverse order, to start at lower densities
159  mono_eos[l] = Eos::eos_from_file(fich) ;
160  }
161 
162  system("rm -f meos_is_being_built.d") ;
163 }
164 
165 // Constructor from a formatted file
166 MEos::MEos(ifstream& fich) : Eos(fich),
167  constructed_from_file(true) {
168 
169  char blabla[80] ;
170 
171  fich >> ndom ; fich.getline(blabla, 80) ;
172 
173  mono_eos = new const Eos* [ndom] ;
174  ofstream temp("meos_is_being_built.d") ;
175  temp << " " << flush ;
176  temp.close() ;
177 
178  for (int l=ndom-1; l>=0; l--) { // Reverse order, to start at lower densities
179  mono_eos[l] = Eos::eos_from_file(fich) ;
180  }
181 
182  system("rm -f meos_is_being_built.d") ;
183 }
184 
185 
186 
187 // Destructor
189 
190  if (constructed_from_file) {
191  for (int l=0; l<ndom; l++) {
192  delete mono_eos[l] ;
193  }
194  }
195 
196  delete [] mono_eos ;
197 
198 }
199 
200  //--------------//
201  // Assignment //
202  //--------------//
203 
204 void MEos::operator=(const MEos& ) {
205 
206  cout << "MEos::operator= : not implemented yet !" << endl ;
207  abort() ;
208 
209 }
210 
211 
212  //---------------------------------------//
213  // Outputs //
214  //---------------------------------------//
215 
216 void MEos::sauve(FILE* fich) const {
217 
218  Eos::sauve(fich) ;
219 
220  fwrite_be(&ndom, sizeof(int), 1, fich) ;
221 
222  for (int l=ndom-1; l>=0; l--) {
223  mono_eos[l]->sauve(fich) ;
224  }
225 
226 }
227 
228 ostream& MEos::operator>>(ostream & ost) const {
229 
230  ost << "EOS of class MEos (multi-domain equation of state) : " << endl ;
231  ost << " Number of domains : " << ndom << endl ;
232 
233  for (int l=0; l<ndom; l++) {
234  ost << "Equation of state in domain " << l << " : " << endl ;
235  ost << "-------------------------------" << endl ;
236  ost << *(mono_eos[l]) ;
237  }
238 
239  return ost ;
240 
241 }
242 
243  //------------------------//
244  // Comparison operators //
245  //------------------------//
246 
247 
248 bool MEos::operator==(const Eos& eos_i) const {
249 
250  bool resu = true ;
251 
252  if ( eos_i.identify() != identify() ) {
253  cout << "The second EOS is not of type MEos !" << endl ;
254  resu = false ;
255  }
256  else{
257 
258  const MEos& eos = dynamic_cast<const MEos&>( eos_i ) ;
259 
260  if (eos.ndom != ndom) {
261  cout << "The two MEos have different number of domains" << endl ;
262  resu = false ;
263 
264  }
265  else {
266  for (int l=0; l<ndom; l++) {
267  resu = resu && ( *(mono_eos[l]) == *(eos.mono_eos[l]) ) ;
268  }
269  }
270  }
271 
272  return resu ;
273 
274 }
275 
276 bool MEos::operator!=(const Eos& eos_i) const {
277 
278  return !(operator==(eos_i)) ;
279 
280 }
281 
282 
283  //------------------------------//
284  // Computational routines //
285  //------------------------------//
286 
287 // Baryon density from enthalpy
288 //------------------------------
289 
290 double MEos::nbar_ent_p(double ent, const Param* par) const {
291 
292  assert(par != 0x0) ;
293 
294  int l0 = par->get_int_mod() ; // index of the domain
295 
296  return mono_eos[l0]->nbar_ent_p(ent) ;
297 
298 }
299 
300 // Energy density from enthalpy
301 //------------------------------
302 
303 double MEos::ener_ent_p(double ent, const Param* par) const {
304 
305  assert(par != 0x0) ;
306 
307  int l0 = par->get_int_mod() ; // index of the domain
308 
309  return mono_eos[l0]->ener_ent_p(ent) ;
310 }
311 
312 // Pressure from enthalpy
313 //------------------------
314 
315 double MEos::press_ent_p(double ent, const Param* par) const {
316 
317  assert(par != 0x0) ;
318  int l0 = par->get_int_mod() ; // index of the domain
319 
320  return mono_eos[l0]->press_ent_p(ent) ;
321 }
322 
323 // dln(n)/ln(H) from enthalpy
324 //---------------------------
325 
326 double MEos::der_nbar_ent_p(double ent, const Param* par) const {
327 
328  assert(par != 0x0) ;
329  int l0 = par->get_int_mod() ; // index of the domain
330 
331  return mono_eos[l0]->der_nbar_ent_p(ent) ;
332 }
333 
334 // dln(e)/ln(H) from enthalpy
335 //---------------------------
336 
337 double MEos::der_ener_ent_p(double ent, const Param* par) const {
338 
339  assert(par != 0x0) ;
340  int l0 = par->get_int_mod() ; // index of the domain
341 
342  return mono_eos[l0]->der_ener_ent_p(ent) ;
343 }
344 
345 // dln(p)/ln(H) from enthalpy
346 //---------------------------
347 
348 double MEos::der_press_ent_p(double ent, const Param* par) const {
349 
350  assert(par != 0x0) ;
351 
352  int l0 = par->get_int_mod() ; // index of the domain
353 
354  return mono_eos[l0]->der_press_ent_p(ent) ;
355 }
356 
357 
358 
359 }
virtual double der_nbar_ent_p(double ent, const Param *par=0x0) const
Computes the logarithmic derivative from the log-enthalpy.
Definition: meos.C:326
virtual double der_press_ent_p(double ent, const Param *par=0x0) const
Computes the logarithmic derivative from the log-enthalpy.
Definition: meos.C:348
virtual double der_press_ent_p(double ent, const Param *par=0x0) const =0
Computes the logarithmic derivative from the log-enthalpy and extra parameters (virtual function imp...
virtual double der_nbar_ent_p(double ent, const Param *par=0x0) const =0
Computes the logarithmic derivative from the log-enthalpy and extra parameters (virtual function imp...
void operator=(const MEos &)
Assignment to another MEos.
Definition: meos.C:204
virtual int identify() const
Returns a number to identify the sub-classe of Eos the object belongs to.
virtual bool operator==(const Eos &) const
Comparison operator (egality)
Definition: meos.C:248
Lorene prototypes.
Definition: app_hor.h:67
MEos(int ndom_i, const Eos **mono_eos_i)
Standard constructor.
Definition: meos.C:88
Equation of state base class.
Definition: eos.h:209
int ndom
Number of domains.
Definition: eos.h:2790
virtual int identify() const =0
Returns a number to identify the sub-classe of Eos the object belongs to.
virtual ostream & operator>>(ostream &) const
Operator >>
Definition: meos.C:228
virtual double press_ent_p(double ent, const Param *par=0x0) const =0
Computes the pressure from the log-enthalpy and extra parameters (virtual function implemented in the...
EOS with domain dependency.
Definition: eos.h:2780
virtual void sauve(FILE *) const
Save in a file.
Definition: eos.C:189
virtual double ener_ent_p(double ent, const Param *par=0x0) const =0
Computes the total energy density from the log-enthalpy and extra parameters (virtual function implem...
virtual double press_ent_p(double ent, const Param *par=0x0) const
Computes the pressure from the log-enthalpy.
Definition: meos.C:315
virtual double nbar_ent_p(double ent, const Param *par=0x0) const =0
Computes the baryon density from the log-enthalpy and extra parameters (virtual function implemented ...
static Eos * eos_from_file(FILE *)
Construction of an EOS from a binary file.
const Eos ** mono_eos
Array (upon the domains) containing the various EOS.
Definition: eos.h:2787
Parameter storage.
Definition: param.h:125
int & get_int_mod(int position=0) const
Returns the reference of a modifiable int stored in the list.
Definition: param.C:433
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:73
virtual double ener_ent_p(double ent, const Param *par=0x0) const
Computes the total energy density from the log-enthalpy.
Definition: meos.C:303
bool constructed_from_file
Indicates wether the EOS has been constructed from a file.
Definition: eos.h:2793
virtual double der_ener_ent_p(double ent, const Param *par=0x0) const
Computes the logarithmic derivative from the log-enthalpy.
Definition: meos.C:337
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:72
virtual ~MEos()
Destructor.
Definition: meos.C:188
virtual bool operator!=(const Eos &) const
Comparison operator (difference)
Definition: meos.C:276
virtual double der_ener_ent_p(double ent, const Param *par=0x0) const =0
Computes the logarithmic derivative from the log-enthalpy with extra parameters (virtual function im...
virtual double nbar_ent_p(double ent, const Param *par=0x0) const
Computes the baryon density from the log-enthalpy.
Definition: meos.C:290
virtual void sauve(FILE *) const
Save in a file.
Definition: meos.C:216