LORENE
dyneos.C
1 /*
2  * Methods of class Dyn_eos.
3  *
4  * (see file dyneos.h for documentation).
5  */
6 
7 /*
8  * Copyright (c) 2019 Jerome Novak
9  * (c) 2000 Eric Gourgoulhon for Eos classes
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 as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * LORENE is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with LORENE; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  */
28 
29 
30 
31 
32 /*
33  * $Id: dyneos.C,v 1.3 2021/05/06 14:33:17 j_novak Exp $
34  * $Log: dyneos.C,v $
35  * Revision 1.3 2021/05/06 14:33:17 j_novak
36  * New conversion function from Eos to Dyn_eos.
37  *
38  * Revision 1.2 2020/12/17 17:00:27 j_novak
39  * Output of sound speed squared, instead of sound speed.
40  *
41  * Revision 1.1 2019/12/06 14:30:50 j_novak
42  * New classes Dyn_eos... for cold Eos's with baryon density as input.
43  *
44  *
45  *
46  * $Header: /cvsroot/Lorene/C++/Source/Eos/dyneos.C,v 1.3 2021/05/06 14:33:17 j_novak Exp $
47  *
48  */
49 
50 // Headers Lorene
51 #include "dyneos.h"
52 #include "eos.h"
53 #include "scalar.h"
54 #include "utilitaires.h"
55 #include "param.h"
56 
57 
58 namespace Lorene {
59 
60  //--------------//
61  // Constructors //
62  //--------------//
63 
64 
65  // Standard constructor without name
66  // ---------------------------------
68 
69  // Standard constructor with name
70  // ---------------------------------
71  Dyn_eos::Dyn_eos(const string& name_i):name(name_i){}
72 
73  // Copy constructor
74  // ----------------
75  Dyn_eos::Dyn_eos(const Dyn_eos& eos_i):name(eos_i.name){}
76 
77  // Constructor from a binary file
78  // ------------------------------
79  Dyn_eos::Dyn_eos(FILE* fich)
80  {
81  const int nchar = 100 ;
82  char tmp_c[nchar] ;
83  size_t ret = fread(tmp_c, sizeof(char), nchar, fich) ;
84  if (int(ret) == nchar)
85  name = tmp_c ;
86  }
87 
88  // Constructor from a formatted file
89  // ---------------------------------
90  Dyn_eos::Dyn_eos(ifstream& fich)
91  {
92  getline(fich, name, '\n') ;
93  }
94 
95  //--------------//
96  // Destructor //
97  //--------------//
98 
99  Dyn_eos::~Dyn_eos(){} // does nothing
100 
101  //-------------------------//
102  // Manipulation of name //
103  //-------------------------//
104 
105  void Dyn_eos::set_name(const string& name_i)
106  {
107  name = name_i ;
108  }
109 
110  const string& Dyn_eos::get_name() const
111  {
112  return name ;
113  }
114 
115 
116  //-------------------------//
117  // Conversion from Eos //
118  //-------------------------//
119 
121  int eos_type = eos_in.identify() ;
122  Dyn_eos* resu ;
123  switch (eos_type) {
124  case 1: {
125  const Eos_poly* p_poly = dynamic_cast<const Eos_poly*>(&eos_in) ;
126  assert(p_poly != nullptr) ;
127  resu = new Dyn_eos_poly(p_poly->get_gam(), p_poly->get_kap(),
128  p_poly->get_m_0(), p_poly->get_mu_0() ) ;
129  break ;
130  }
131  case 10:
132  case 11:
133  case 12:
134  case 13:
135  case 14:
136  case 15:
137  case 16: {
138  const Eos_tabul* p_tabul = dynamic_cast<const Eos_tabul*>(&eos_in) ;
139  assert(p_tabul != nullptr) ;
140  string eos_name = p_tabul->get_name() ;
141  resu = new Dyn_eos_tab(eos_name, p_tabul->get_tablename(), false) ;
142  break ;
143  }
144  case 17: {
145  const Eos_CompOSE* p_compose
146  = dynamic_cast<const Eos_CompOSE*>(&eos_in) ;
147  assert(p_compose != nullptr) ;
148  string eos_name = p_compose->get_name() ;
149  resu = new Dyn_eos_tab(eos_name, p_compose->get_tablename(),
150  p_compose->get_format()) ;
151  break ;
152  }
153  case 20: {
154  const Eos_consistent* p_consistent
155  = dynamic_cast<const Eos_consistent*>(&eos_in) ;
156  assert(p_consistent != nullptr) ;
157  string eos_name = p_consistent->get_name() ;
158  resu = new Dyn_eos_cons(eos_name, p_consistent->get_tablename(),
159  p_consistent->get_format()) ;
160  break ;
161  }
162  default: {
163  cerr << "Dyn_eos::convert_from_Eos : " ;
164  cerr << "Unknown input Eos!" << endl ;
165  abort() ;
166  break ;
167  }
168  }
169 
170  return resu ;
171 
172  }
173 
174  //------------//
175  // Outputs //
176  //------------//
177 
178  void Dyn_eos::sauve(FILE* fich) const
179  {
180  int ident = identify() ;
181  fwrite_be(&ident, sizeof(int), 1, fich) ;
182  fwrite(name.c_str(), sizeof(char), 100, fich) ;
183  }
184 
185  ostream& operator<<(ostream& ost, const Dyn_eos& eqetat)
186  {
187  ost << eqetat.get_name() << endl ;
188  eqetat >> ost ;
189  return ost ;
190  }
191 
192  //-------------------------------//
193  // Generic computational routine //
194  //-------------------------------//
195 
196  void Dyn_eos::calcule(const Scalar& nbar, int nzet, int l_min,
197  double (Dyn_eos::*fait)(double, const Param*) const,
198  Param* par, Scalar& resu) const
199  {
200  assert(nbar.get_etat() != ETATNONDEF) ;
201  const Map& mp = nbar.get_mp() ; // Mapping
202 
203  if (nbar.get_etat() == ETATZERO) {
204  resu.set_etat_zero() ;
205  return ;
206  }
207 
208  assert(nbar.get_etat() == ETATQCQ) ;
209  const Valeur& vnbar = nbar.get_spectral_va() ;
210  vnbar.coef_i() ; // the values in the configuration space are required
211 
212  const Mg3d* mg = mp.get_mg() ; // Multi-grid
213  int nz = mg->get_nzone() ; // total number of domains
214 
215  // Preparations for a point by point computation:
216  resu.set_etat_qcq() ;
217  Valeur& vresu = resu.set_spectral_va() ;
218  vresu.set_etat_c_qcq() ;
219  vresu.c->set_etat_qcq() ;
220 
221  // Loop on domains where the computation has to be done :
222  for (int l = l_min; l< l_min + nzet; l++)
223  {
224  assert(l>=0) ;
225  assert(l<nz) ;
226 
227  Tbl* tnbar = vnbar.c->t[l] ;
228  Tbl* tresu = vresu.c->t[l] ;
229 
230  if (tnbar->get_etat() == ETATZERO)
231  {
232  tresu->set_etat_zero() ;
233  }
234  else
235  {
236  assert( tnbar->get_etat() == ETATQCQ ) ;
237  tresu->set_etat_qcq() ;
238  for (int i=0; i<tnbar->get_taille(); i++)
239  {
240  tresu->t[i] = (this->*fait)( tnbar->t[i], par ) ;
241  }
242  } // End of the case where nbar != 0 in the considered domain
243  } // End of the loop on domains where the computation had to be done
244 
245  // resu is set to zero in the other domains :
246  if (l_min > 0)
247  {
248  resu.annule(0, l_min-1) ;
249  }
250  if (l_min + nzet < nz)
251  {
252  resu.annule(l_min + nzet, nz - 1) ;
253  }
254  }
255  //---------------------------------//
256  // Public functions //
257  //---------------------------------//
258 
259 
260  // Enthalpy from baryon density
261  //------------------------------
262 
263  Scalar Dyn_eos::ent_nbar(const Scalar& nbar, int nzet, int l_min, Param* par) const
264  {
265  Scalar resu(nbar.get_mp()) ;
266 
267  calcule(nbar, nzet, l_min, &Dyn_eos::ent_nbar_p, par, resu) ;
268 
269  return resu ;
270  }
271 
272 
273  // Energy density from baryon density
274  //------------------------------------
275 
276  Scalar Dyn_eos::ener_nbar(const Scalar& nbar, int nzet, int l_min, Param* par) const
277  {
278  Scalar resu(nbar.get_mp()) ;
279 
280  calcule(nbar, nzet, l_min, &Dyn_eos::ener_nbar_p, par, resu) ;
281 
282  return resu ;
283  }
284 
285  // Pressure from baryon density
286  //------------------------------
287 
288  Scalar Dyn_eos::press_nbar(const Scalar& nbar, int nzet, int l_min, Param* par) const
289  {
290  Scalar resu(nbar.get_mp()) ;
291 
292  calcule(nbar, nzet, l_min, &Dyn_eos::press_nbar_p, par, resu) ;
293 
294  return resu ;
295  }
296 
297  // Sound speed from baryon density
298  //---------------------------------
299 
301  int l_min, Param* par) const {
302  Scalar resu(nbar.get_mp()) ;
303 
304  calcule(nbar, nzet, l_min, &Dyn_eos::csound_square_nbar_p, par, resu) ;
305 
306  return resu ;
307  }
308 
309  //--------------------------------------//
310  // Identification virtual functions //
311  //--------------------------------------//
312 
313  int Dyn_eos_poly::identify() const { return 1; }
314 
315  int Dyn_eos_tab::identify() const { return 17; }
316 
317  int Dyn_eos_cons::identify() const { return 20; }
318 
319  //---------------------------------------------//
320  // EOS construction from a binary file //
321  //---------------------------------------------//
322 
324 
325  Dyn_eos* p_eos ;
326 
327  // Type (class) of EOS :
328  int identificator ;
329  fread_be(&identificator, sizeof(int), 1, fich) ;
330 
331  switch(identificator) {
332 
333  case 1 : {
334  p_eos = new Dyn_eos_poly(fich) ;
335  break ;
336  }
337 
338  case 17 : {
339  p_eos = new Dyn_eos_tab(fich) ;
340  break ;
341  }
342 
343  case 20 : {
344  p_eos = new Dyn_eos_cons(fich) ;
345  break ;
346  }
347 
348  default : {
349  cout << "Dyn_eos::eos_from_file : unknown type of EOS !" << endl ;
350  cout << " identificator = " << identificator << endl ;
351  abort() ;
352  break ;
353  }
354  }
355  return p_eos ;
356  }
357 
358  //----------------------------------------------//
359  // EOS construction from a formatted file //
360  //----------------------------------------------//
361 
362 Dyn_eos* Dyn_eos::eos_from_file(ifstream& fich) {
363 
364  int identificator ;
365 
366  // EOS identificator :
367  fich >> identificator ; fich.ignore(1000, '\n') ;
368 
369  Dyn_eos* p_eos ;
370 
371  switch(identificator) {
372 
373  case 1 : {
374  p_eos = new Dyn_eos_poly(fich) ;
375  break ;
376  }
377 
378  case 17 : {
379  p_eos = new Dyn_eos_tab(fich) ;
380  break ;
381  }
382 
383  case 20 : {
384  p_eos = new Dyn_eos_cons(fich) ;
385  break ;
386  }
387  default : {
388  cout << "Dyn_eos::eos_from_file : unknown type of EOS !" << endl ;
389  cout << " identificator = " << identificator << endl ;
390  abort() ;
391  break ;
392  }
393  }
394  return p_eos ;
395 }
396 
397 
398 }
void calcule(const Scalar &thermo, int nzet, int l_min, double(Dyn_eos::*fait)(double, const Param *) const, Param *par, Scalar &resu) const
General computational method for Scalar &#39;s.
Definition: dyneos.C:196
void set_name(const string &)
Sets the EOS name.
Definition: dyneos.C:105
Scalar csound_square_nbar(const Scalar &nbar, int nzet, int l_min=0, Param *par=0x0) const
Computes the sound speed squared from the baryon density with extra parameters.
Definition: dyneos.C:300
virtual int identify() const
Returns a number to identify the sub-classe of Dyn_eos the object belongs to.
Definition: dyneos.C:313
virtual void sauve(FILE *) const
Save in a file.
Definition: dyneos.C:178
virtual int identify() const =0
Returns a number to identify the sub-classe of Dyn_eos the object belongs to.
virtual void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition: scalar.C:330
const char * get_name() const
Returns the EOS name.
Definition: eos.C:179
virtual void annule(int l_min, int l_max)
Sets the Scalar to zero in several domains.
Definition: scalar.C:397
virtual double ent_nbar_p(double nbar, const Param *par=0x0) const =0
Computes the log-enthalpy from the baryon density and extra parameters (virtual function implemented ...
virtual double csound_square_nbar_p(double nbar, const Param *par=0x0) const =0
Computes the sound speed squared from the baryon density with extra parameters (virtual function imp...
Lorene prototypes.
Definition: app_hor.h:67
Equation of state base class.
Definition: eos.h:209
virtual int identify() const =0
Returns a number to identify the sub-classe of Eos the object belongs to.
Tensor field of valence 0 (or component of a tensorial field).
Definition: scalar.h:393
void coef_i() const
Computes the physical value of *this.
Base class for coordinate mappings.
Definition: map.h:682
const string & get_name() const
Returns the EOS name.
Definition: dyneos.C:110
virtual ~Dyn_eos()
Destructor.
Definition: dyneos.C:99
Polytropic equation of state (relativistic case) for use in dynamical code.
Definition: dyneos.h:378
Values and coefficients of a (real-value) function.
Definition: valeur.h:297
int get_etat() const
Gives the logical state.
Definition: tbl.h:414
int get_etat() const
Returns the logical state ETATNONDEF (undefined), ETATZERO (null) or ETATQCQ (ordinary).
Definition: scalar.h:560
Scalar press_nbar(const Scalar &nbar, int nzet, int l_min=0, Param *par=0x0) const
Computes the pressure from the baryon density and extra parameters.
Definition: dyneos.C:288
virtual void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: scalar.C:359
virtual int identify() const
Returns a number to identify the sub-classe of Dyn_eos the object belongs to.
Definition: dyneos.C:315
double get_gam() const
Returns the adiabatic index (cf. Eq. (3))
Definition: eos_poly.C:271
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: tbl.C:364
Equation of state for use in dynamical code base class.
Definition: dyneos.h:75
virtual double press_nbar_p(double nbar, const Param *par=0x0) const =0
Computes the pressure from the baryon density and extra parameters (virtual function implemented in t...
Class for tabulated equations of state for use in dynamical code.
Definition: dyneos.h:648
double get_kap() const
Returns the pressure coefficient (cf.
Definition: eos_poly.C:275
double get_m_0() const
Return the individual particule mass (cf.
Definition: eos_poly.C:279
virtual double ener_nbar_p(double nbar, const Param *par=0x0) const =0
Computes the total energy density from the baryon density and extra parameters (virtual function impl...
Equation of state for the CompOSE database with a consistent computation of the log-enthalpy (deriv...
Definition: eos_compose.h:227
double * t
The array of double.
Definition: tbl.h:176
static Dyn_eos * convert_from_Eos(const Eos &)
Conversion operator from Eos to Dyn_eos.
Definition: dyneos.C:120
Mtbl * c
Values of the function at the points of the multi-grid.
Definition: valeur.h:309
Parameter storage.
Definition: param.h:125
Polytropic equation of state (relativistic case).
Definition: eos.h:812
int get_nzone() const
Returns the number of domains.
Definition: grilles.h:465
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
Base class for tabulated equations of state.
Definition: eos_tabul.h:185
double get_mu_0() const
Return the relativistic chemical potential at zero pressure [unit: , with ].
Definition: eos_poly.C:283
Scalar ener_nbar(const Scalar &nbar, int nzet, int l_min=0, Param *par=0x0) const
Computes the total energy density from the baryon density and extra parameters.
Definition: dyneos.C:276
Dyn_eos()
Standard constructor.
Definition: dyneos.C:67
string name
EOS name.
Definition: dyneos.h:81
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: mtbl.C:302
void set_etat_zero()
Sets the logical state to ETATZERO (zero).
Definition: tbl.C:350
static Dyn_eos * eos_from_file(FILE *)
Construction of an EOS from a binary file.
Definition: dyneos.C:323
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
Multi-domain grid.
Definition: grilles.h:279
virtual int identify() const
Returns a number to identify the sub-classe of Dyn_eos the object belongs to.
Definition: dyneos.C:317
int get_taille() const
Gives the total size (ie dim.taille)
Definition: tbl.h:417
void set_etat_c_qcq()
Sets the logical state to ETATQCQ (ordinary state) for values in the configuration space (Mtbl c )...
Definition: valeur.C:704
Basic array class.
Definition: tbl.h:164
Valeur & set_spectral_va()
Returns va (read/write version)
Definition: scalar.h:610
Equation of state for the CompOSE database with a consistent computation of the baryon density...
Definition: dyneos.h:839
Scalar ent_nbar(const Scalar &nbar, int nzet, int l_min=0, Param *par=0x0) const
Computes the log-enthalpy field from the baryon density field and extra parameters.
Definition: dyneos.C:263
Tbl ** t
Array (size nzone ) of pointers on the Tbl &#39;s.
Definition: mtbl.h:132
const Map & get_mp() const
Returns the mapping.
Definition: tensor.h:874
Equation of state for the CompOSE database.
Definition: eos_compose.h:93
const Valeur & get_spectral_va() const
Returns va (read only version)
Definition: scalar.h:607