LORENE
hoteos_tabul.C
1 /*
2  * Methods of class Hoteos_tabul
3  *
4  * (see file hoteos.h for documentation).
5  *
6  */
7 
8 /*
9  * Copyright (c) 2015 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 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: hoteos_tabul.C,v 1.7 2022/12/15 14:38:27 j_novak Exp $
34  * $Log: hoteos_tabul.C,v $
35  * Revision 1.7 2022/12/15 14:38:27 j_novak
36  * Change in the call to fread, to avoid compilation warnings
37  *
38  * Revision 1.6 2022/04/06 12:38:05 g_servignat
39  * Added source computation routine and source reading in table for electronic fraction advection equation
40  *
41  * Revision 1.5 2022/03/01 10:03:06 g_servignat
42  * Corrected all pure virtual interference between Hot_eos derived classes ; amended use of interpol_linear_2D
43  *
44  * Revision 1.4 2017/06/06 15:36:42 j_novak
45  * Reads a number as first column of tabulated EoS
46  *
47  * Revision 1.3 2016/12/05 16:17:52 j_novak
48  * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
49  *
50  * Revision 1.2 2015/12/08 15:42:17 j_novak
51  * Low/zero entropy is set to the lowest value in the table in computational functions.
52  *
53  * Revision 1.1 2015/12/08 10:52:18 j_novak
54  * New class Hoteos_tabul for tabulated temperature-dependent EoSs.
55  *
56  *
57  *
58  * $Header: /cvsroot/Lorene/C++/Source/Eos/hoteos_tabul.C,v 1.7 2022/12/15 14:38:27 j_novak Exp $
59  *
60  */
61 
62 // headers C
63 #include <cstdlib>
64 #include <cstring>
65 #include <cmath>
66 
67 // Headers Lorene
68 #include "hoteos.h"
69 #include "eos.h"
70 #include "utilitaires.h"
71 #include "unites.h"
72 
73 
74 namespace Lorene {
75  void interpol_herm_2d(const Tbl&, const Tbl&, const Tbl&, const Tbl&, const Tbl&,
76  const Tbl&, double, double, double&, double&, double&) ;
77 
78 
79  //----------------------------//
80  // Constructors //
81  //----------------------------//
82 
83  // Standard constructor
84  // --------------------
85  Hoteos_tabul::Hoteos_tabul(const string& filename):
86  Hot_eos("Tabulated hot EoS"), tablename(filename), authors("Unknown"),
87  hmin(0.), hmax(1.), sbmin(0.), sbmax(1.)
88  {
89  set_arrays_0x0() ;
90  read_table() ;
91  }
92 
93 
94  // Constructor from binary file
95  // ----------------------------
96  Hoteos_tabul::Hoteos_tabul(FILE* fich) : Hot_eos(fich) {
97 
98  const int nc = 160 ;
99  char tmp_string[nc] ;
100  size_t ret = fread(tmp_string, sizeof(char), nc, fich) ;
101  if (int(ret) == nc)
102  tablename = tmp_string ;
103  else {
104  cerr << "Hoteos_tabul: constructor from a binary file:" << endl ;
105  cerr << "Problems in reading the table name." << endl ;
106  cerr << "Aborting..." << endl ;
107  abort() ;
108  }
109  set_arrays_0x0() ;
110  read_table() ;
111  }
112 
113  // Constructor from a formatted file
114  // ---------------------------------
115  Hoteos_tabul::Hoteos_tabul(ifstream& fich) :
116  Hot_eos(fich) {
117 
118  fich >> tablename ;
119  set_arrays_0x0() ;
120  read_table() ;
121  }
122 
123  //Sets the arrays to the null pointer
125  hhh = 0x0 ;
126  s_B = 0x0 ;
127  ppp = 0x0 ;
128  dpdh = 0x0 ;
129  dpds = 0x0 ;
130  d2p = 0x0 ;
131  }
132 
133  //--------------//
134  // Destructor //
135  //--------------//
136 
138  if (hhh != 0x0) delete hhh ;
139  if (s_B != 0x0) delete s_B ;
140  if (ppp != 0x0) delete ppp ;
141  if (dpdh != 0x0) delete dpdh ;
142  if (dpds != 0x0) delete dpds ;
143  if (d2p != 0x0) delete d2p ;
144  }
145 
146  //------------//
147  // Outputs //
148  //------------//
149 
150  void Hoteos_tabul::sauve(FILE* fich) const {
151 
152  Hot_eos::sauve(fich) ;
153 
154  char tmp_string[160] ;
155  strcpy(tmp_string, tablename.c_str()) ;
156  fwrite(tmp_string, sizeof(char), 160, fich) ;
157  }
158 
159  ostream& Hoteos_tabul::operator>>(ostream & ost) const {
160 
161  ost << "Hot EOS of class Hoteos_tabul (tabulated hot beta-equilibrium EoS) : "
162  << endl ;
163  ost << "Built from file " << tablename << endl ;
164  ost << "Authors : " << authors << endl ;
165  ost << "Number of points in file : " << hhh->get_dim(0)
166  << " in enthalpy, and " << hhh->get_dim(1)
167  << " in entropy." << endl ;
168 
169  return ost ;
170 }
171 
172  //------------------------//
173  // Reading of the table //
174  //------------------------//
175 
177 
178  using namespace Unites ;
179 
180  ifstream fich(tablename.data()) ;
181 
182  if (!fich) {
183  cerr << "Hoteos_tabul::read_table(): " << endl ;
184  cerr << "Problem in opening the EOS file!" << endl ;
185  cerr << "While trying to open " << tablename << endl ;
186  cerr << "Aborting..." << endl ;
187  abort() ;
188  }
189 
190  fich.ignore(1000, '\n') ; // Jump over the first header
191  fich.ignore(1) ;
192  getline(fich, authors, '\n') ;
193  for (int i=0; i<3; i++) { // jump over the file
194  fich.ignore(1000, '\n') ; // header
195  } //
196 
197  int nbp1, nbp2 ;
198  fich >> nbp1 >> nbp2 ; fich.ignore(1000, '\n') ; // number of data
199  if ( (nbp1<=0) || (nbp2<=0) ) {
200  cerr << "Hoteos_tabul::read_table(): " << endl ;
201  cerr << "Wrong value for the number of lines!" << endl ;
202  cerr << "nbp1 = " << nbp1 << ", nbp2 = " << nbp2 << endl ;
203  cerr << "Aborting..." << endl ;
204  abort() ;
205  }
206 
207  for (int i=0; i<3; i++) { // jump over the table
208  fich.ignore(1000, '\n') ; // description
209  }
210 
211  ppp = new Tbl(nbp2, nbp1) ;
212  hhh = new Tbl(nbp2, nbp1) ;
213  s_B = new Tbl(nbp2, nbp1) ;
214  dpdh = new Tbl(nbp2, nbp1) ;
215  dpds = new Tbl(nbp2, nbp1) ;
216  d2p = new Tbl(nbp2, nbp1) ;
217 
218  ppp->set_etat_qcq() ;
219  hhh->set_etat_qcq() ;
220  s_B->set_etat_qcq() ;
221  dpdh->set_etat_qcq() ;
222  dpds->set_etat_qcq() ;
223  d2p->set_etat_qcq() ;
224 
225  double c2 = c_si * c_si ;
226  double dummy, nb_fm3, rho_cgs, p_cgs, mu_MeV, entr, temp, der2 ;
227  double ww = 0. ;
228  int no;
229 
230  for (int j=0; j<nbp2; j++) {
231  for (int i=0; i<nbp1; i++) {
232  fich >> no >> nb_fm3>> rho_cgs >> p_cgs>> mu_MeV >> entr >> temp >> der2
233  >> dummy;
234  fich.ignore(1000,'\n') ;
235  if ( (nb_fm3<0) || (rho_cgs<0) || (p_cgs < 0) ){
236  cerr << "Eos_mag::read_table(): " << endl ;
237  cerr << "Negative value in table!" << endl ;
238  cerr << "nb = " << nb_fm3 << ", rho = " << rho_cgs <<
239  ", p = " << p_cgs << endl ;
240  cerr << "Aborting..." << endl ;
241  abort() ;
242  }
243 
244  double psc2 = 0.1*p_cgs/c2 ; // in kg m^-3
245  double rho_si = rho_cgs*1000. ; // in kg m^-3
246 
247  double h_read = log(mu_MeV) ;
248  if ( (i==0) && (j==0) ) ww = h_read ;
249  double h_new = h_read - ww ;
250 
251  ppp->set(j, i) = psc2/rhonuc_si ;
252  hhh->set(j, i) = h_new ;
253  s_B->set(j, i) = entr ; // in Lorene units (k_B)
254  dpdh->set(j, i) = (rho_si + psc2)/rhonuc_si ;
255  dpds->set(j, i) = -temp*nb_fm3*mevpfm3 ;
256  d2p->set(j, i) = 0.1*der2*mu_MeV/(c2*rhonuc_si) ;
257  }
258  }
259 
260  hmin = (*hhh)(0, 0) ;
261  hmax = (*hhh)(0, nbp1-1) ;
262 
263  sbmin = (*s_B)(0, 0) ;
264  sbmax = (*s_B)(nbp2-1, 0) ;
265 
266  cout << "hmin: " << hmin << ", hmax: " << hmax << endl ;
267  cout << "sbmin: " << sbmin << ", sbmax: " << sbmax << endl ;
268 
269  fich.close();
270 }
271 
272  //-------------------------------//
273  // The corresponding cold Eos //
274  //-------------------------------//
275 
277 
278  if (p_cold_eos == 0x0) {
279  cerr << "Warning: Hoteos_tabul::new_cold_Eos " <<
280  "The corresponding cold EoS is likely not to work" << endl ;
281  cout << "read from file: "<< tablename.c_str() << endl;
282  p_cold_eos = new Eos_CompOSE(tablename.c_str()) ;
283  }
284 
285  return *p_cold_eos ;
286  }
287 
288 
289 
290  //------------------------//
291  // Comparison operators //
292  //------------------------//
293 
294 
295  bool Hoteos_tabul::operator==(const Hot_eos& eos_i) const {
296 
297  bool resu = true ;
298 
299  if ( eos_i.identify() != identify() ) {
300  cout << "The second EOS is not of type Hoteos_tabul !" << endl ;
301  resu = false ;
302  }
303 
304  return resu ;
305  }
306 
307 
308  bool Hoteos_tabul::operator!=(const Hot_eos& eos_i) const {
309  return !(operator==(eos_i)) ;
310  }
311 
312 
313  //------------------------------//
314  // Computational routines //
315  //------------------------------//
316 
317 // Baryon density from enthalpy and entropy
318 //------------------------------------------
319 
320 double Hoteos_tabul::nbar_Hs_p(double ent, double sb) const {
321 
322  if ((ent > hmin - 1.e-12) && (ent < hmin))
323  ent = hmin ;
324 
325  if (sb < sbmin) sb = sbmin ;
326 
327  if ( ent >= hmin ) {
328  if (ent > hmax) {
329  cout << "Hoteos_tabul::nbar_Hs_p : ent > hmax !" << endl ;
330  abort() ;
331  }
332 
333  if (sb > sbmax) {
334  cerr << "Hoteos_tabul::nbar_Hs_p : s_B not in the tabulated interval !"
335  << endl ;
336  cerr << "s_B = " << sb << ", sbmin = " << sbmin << ", sbmax = " << sbmax
337  << endl ;
338  abort() ;
339  }
340 
341  double p_int, dpds_int, dpdh_int ;
342  interpol_herm_2d(*s_B, *hhh, *ppp, *dpds, *dpdh, *d2p, sb, ent, p_int,
343  dpds_int, dpdh_int) ;
344 
345  double nbar_int = dpdh_int * exp(-ent) ;
346  return nbar_int ;
347  }
348  else{
349  return 0 ;
350  }
351 }
352 
353 // Energy density from enthalpy and entropy
354 //-----------------------------------------
355 
356 double Hoteos_tabul::ener_Hs_p(double ent, double sb) const {
357 
358  if ((ent > hmin - 1.e-12) && (ent < hmin))
359  ent = hmin ;
360 
361  if (sb < sbmin) sb = sbmin ;
362 
363  if ( ent >= hmin ) {
364  if (ent > hmax) {
365  cout << "Hoteos_tabul::ener_Hs_p : ent > hmax !" << endl ;
366  abort() ;
367  }
368 
369  if (sb > sbmax) {
370  cerr << "Hoteos_tabul::ener_Hs_p : s_B not in the tabulated interval !"
371  << endl ;
372  cerr << "s_B = " << sb << ", sbmin = " << sbmin << ", sbmax = " << sbmax
373  << endl ;
374  abort() ;
375  }
376 
377  double p_int, dpds_int, dpdh_int ;
378  interpol_herm_2d(*s_B, *hhh, *ppp, *dpds, *dpdh, *d2p, sb, ent, p_int,
379  dpds_int, dpdh_int) ;
380 
381  double nbar_int = dpdh_int * exp(-ent) ;
382 
383  double f_int = - p_int + exp(ent) * nbar_int;
384 
385  return f_int ;
386  }
387  else{
388  return 0 ;
389  }
390 }
391 
392 // Pressure from enthalpy and entropy
393 //-----------------------------------
394 
395 double Hoteos_tabul::press_Hs_p(double ent, double sb) const {
396 
397  if ((ent > hmin - 1.e-12) && (ent < hmin))
398  ent = hmin ;
399 
400  if (sb < sbmin) sb = sbmin ;
401 
402  if ( ent >= hmin ) {
403  if (ent > hmax) {
404  cout << "Hoteos_tabul::press_Hs_p : ent > hmax !" << endl ;
405  abort() ;
406  }
407 
408  if (sb > sbmax) {
409  cerr << "Hoteos_tabul::press_Hs_p : s_B not in the tabulated interval !"
410  << endl ;
411  cerr << "s_B = " << sb << ", sbmin = " << sbmin << ", sbmax = " << sbmax
412  << endl ;
413  abort() ;
414  }
415 
416  double p_int, dpds_int, dpdh_int ;
417  interpol_herm_2d(*s_B, *hhh, *ppp, *dpds, *dpdh, *d2p, sb, ent, p_int,
418  dpds_int, dpdh_int) ;
419 
420  return p_int ;
421  }
422  else{
423  return 0 ;
424  }
425 }
426 
427  // Temperature from enthalpy and entropy
428  //--------------------------------------
429  double Hoteos_tabul::temp_Hs_p(double ent, double sb) const {
430 
431  if ((ent > hmin - 1.e-12) && (ent < hmin))
432  ent = hmin ;
433 
434  if (sb < sbmin) sb = sbmin ;
435 
436  if ( ent >= hmin ) {
437  if (ent > hmax) {
438  cout << "Hoteos_tabul::temp_Hs_p : ent > hmax !" << endl ;
439  abort() ;
440  }
441 
442  if (sb > sbmax) {
443  cerr << "Hoteos_tabul::temp_Hs_p : s_B not in the tabulated interval !"
444  << endl ;
445  cerr << "s_B = " << sb << ", sbmin = " << sbmin << ", sbmax = " << sbmax
446  << endl ;
447  abort() ;
448  }
449 
450  double p_int, dpds_int, dpdh_int ;
451  interpol_herm_2d(*s_B, *hhh, *ppp, *dpds, *dpdh, *d2p, sb, ent, p_int,
452  dpds_int, dpdh_int) ;
453 
454  double nbar_int = dpdh_int * exp(-ent) ;
455 
456  double temp_int = -dpds_int / nbar_int ;
457 
458  return temp_int ;
459  }
460  else {
461  return 0 ;
462  }
463  }
464 
465  double Hoteos_tabul::csound_square_Hs_p(double, double) const {
466  cerr << "Hoteos_tabul::csound_square_Hs_p : function not implemented (yet !) ..." << endl;
467  cerr << "Aborting ..." << endl;
468  abort() ;
469 
470  return 0. ;
471  }
472 
473  double Hoteos_tabul::chi2_Hs_p(double, double) const {
474  cerr << "Hoteos_tabul::chi2_Hs_p : function not implemented (yet !) ..." << endl;
475  cerr << "Aborting ..." << endl;
476  abort() ;
477 
478  return 0. ;
479  }
480 
481  double Hoteos_tabul::mul_Hs_p(double, double) const {
482  cerr << "Hoteos_tabul::mul_Hs_p : function not implemented (yet !) ..." << endl;
483  cerr << "Aborting ..." << endl;
484  abort() ;
485 
486  return 0. ;
487  }
488 
489  double Hoteos_tabul::sigma_Hs_p(double, double) const {
490  cerr << "Hoteos_tabul::sigma_Hs_p : function not implemented (yet !) ..." << endl;
491  cerr << "Aborting ..." << endl;
492  abort() ;
493 
494  return 0. ;
495  }
496 
497 
498 } //End of namespace Lorene
Tbl * dpds
Table of .
Definition: hoteos.h:784
Cmp log(const Cmp &)
Neperian logarithm.
Definition: cmp_math.C:299
Cmp exp(const Cmp &)
Exponential.
Definition: cmp_math.C:273
Eos * p_cold_eos
Corresponding cold Eos.
Definition: hoteos.h:126
virtual double chi2_Hs_p(double ent, const double ye) const
Computes the chi^2 coefficient from the enthapy with electronic fraction (virtual function implemente...
Definition: hoteos_tabul.C:473
virtual double sigma_Hs_p(double ent, const double ye) const
Computes the source terms for electronic fraction advection equation from the enthapy with electronic...
Definition: hoteos_tabul.C:489
Lorene prototypes.
Definition: app_hor.h:67
Standard units of space, time and mass.
Equation of state base class.
Definition: eos.h:209
double & set(int i)
Read/write of a particular element (index i) (1D case)
Definition: tbl.h:301
Hoteos_tabul(const string &filename)
Standard constructor from a filename.
Definition: hoteos_tabul.C:85
virtual double press_Hs_p(double ent, double sb) const
Computes the pressure from the log-enthalpy and entropy per baryon (virtual function implemented in t...
Definition: hoteos_tabul.C:395
virtual int identify() const
Returns a number to identify the sub-classe of Hot_eos the object belongs to.
virtual bool operator==(const Hot_eos &) const
Comparison operator (egality)
Definition: hoteos_tabul.C:295
Tbl * d2p
Table of .
Definition: hoteos.h:787
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: tbl.C:364
Tbl * s_B
Table of , entropy per baryon (in units of Boltzmann constant).
Definition: hoteos.h:775
double hmax
Upper boundary of the enthalpy interval.
Definition: hoteos.h:763
Tbl * dpdh
Table of .
Definition: hoteos.h:781
string authors
Authors - reference for the table.
Definition: hoteos.h:757
double hmin
Lower boundary of the enthalpy interval.
Definition: hoteos.h:760
string tablename
Name of the file containing the tabulated data.
Definition: hoteos.h:755
void set_arrays_0x0()
Sets all the arrays to the null pointer.
Definition: hoteos_tabul.C:124
void read_table()
Reads the file containing the table and initializes in the arrays hhh , s_B, ppp, ...
Definition: hoteos_tabul.C:176
virtual double csound_square_Hs_p(double ent, const double ye) const
Computes the sound speed squared from the enthapy with electronic fraction (virtual function impleme...
Definition: hoteos_tabul.C:465
virtual int identify() const =0
Returns a number to identify the sub-classe of Hot_eos the object belongs to.
int get_dim(int i) const
Gives the i-th dimension (ie dim.dim[i])
Definition: tbl.h:423
virtual const Eos & new_cold_Eos() const
Returns the corresponding cold Eos.
Definition: hoteos_tabul.C:276
virtual void sauve(FILE *) const
Save in a file.
Definition: hoteos_tabul.C:150
Base class for 2-parameters equations of state (abstract class).
Definition: hoteos.h:85
virtual ~Hoteos_tabul()
Destructor.
Definition: hoteos_tabul.C:137
virtual void sauve(FILE *) const
Save in a file.
Definition: hoteos.C:129
virtual double temp_Hs_p(double ent, double sb) const
Computes the temperature from the log-enthalpy and entropy per baryon (virtual function implemented i...
Definition: hoteos_tabul.C:429
Basic array class.
Definition: tbl.h:164
Tbl * ppp
Table of pressure $P$.
Definition: hoteos.h:778
virtual ostream & operator>>(ostream &) const
Operator >>
Definition: hoteos_tabul.C:159
double sbmax
Upper boundary of the entropy interval.
Definition: hoteos.h:769
Equation of state for the CompOSE database.
Definition: eos_compose.h:93
Tbl * hhh
Table of .
Definition: hoteos.h:772
virtual bool operator!=(const Hot_eos &) const
Comparison operator (difference)
Definition: hoteos_tabul.C:308
virtual double mul_Hs_p(double ent, const double ye) const
Computes the electronic chemical potential from the enthapy with electronic fraction (virtual functio...
Definition: hoteos_tabul.C:481
virtual double nbar_Hs_p(double ent, double sb) const
Computes the baryon density from the log-enthalpy and entropy per baryon (virtual function implemente...
Definition: hoteos_tabul.C:320
double sbmin
Lower boundary of the entropy interval.
Definition: hoteos.h:766
virtual double ener_Hs_p(double ent, double sb) const
Computes the total energy density from the log-enthalpy and entropy per baryon (virtual function impl...
Definition: hoteos_tabul.C:356