LORENE
ye_eos_tabul.C
1/*
2 * Methods of class Ye_eos_tabul
3 *
4 * (see file hoteos.h for documentation).
5 *
6 */
7
8/*
9 * Copyright (c) 2022 Jerome Novak, Gael Servignat
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// headers C
30#include <cstdlib>
31#include <cstring>
32#include <cmath>
33
34// Headers Lorene
35#include "hoteos.h"
36#include "eos.h"
37#include "utilitaires.h"
38#include "unites.h"
39
40
41namespace Lorene {
42 void interpol_herm_2d(const Tbl&, const Tbl&, const Tbl&, const Tbl&, const Tbl&,
43 const Tbl&, double, double, double&, double&, double&) ;
44 void interpol_linear_2d(const Tbl&, const Tbl&, const Tbl&, double, double, int&, int&, double&) ;
45 void huntm(const Tbl& xx, double& x, int& i_low) ;
46 Tbl extract_column(const Tbl&, const Tbl&, double) ;
47
48
49 //----------------------------//
50 // Constructors //
51 //----------------------------//
52
53 // Standard constructor
54 // --------------------
55 Ye_eos_tabul::Ye_eos_tabul(const string& filename):
56 Hot_eos("Tabulated Y_e EoS"), tablename(filename), authors("Unknown"),
57 hmin(0.), hmax(1.), yemin(0.), yemax(1.)
58 {
60 read_table() ;
61 }
62
63
64 // Constructor from binary file
65 // ----------------------------
66 Ye_eos_tabul::Ye_eos_tabul(FILE* fich) : Hot_eos(fich) {
67
68 const int nc = 160 ;
69 char tmp_string[nc] ;
70 size_t ret = fread(tmp_string, sizeof(char), nc, fich) ;
71 if (int(ret) == nc)
72 tablename = tmp_string ;
73 else {
74 cerr << "Ye_tabul: constructor from a binary file:" << endl ;
75 cerr << "Problems in reading the table name." << endl ;
76 cerr << "Aborting..." << endl ;
77 abort() ;
78 }
80 read_table() ;
81 }
82
83 // Constructor from a formatted file
84 // ---------------------------------
85 Ye_eos_tabul::Ye_eos_tabul(ifstream& fich) :
86 Hot_eos(fich) {
87
88 fich >> tablename ;
90 read_table() ;
91 }
92
93 //Sets the arrays to the null pointer
95 hhh = 0x0 ;
96 Y_e = 0x0 ;
97 c_sound2 = 0x0 ;
98 mu_l = 0x0 ;
99 ppp = 0x0 ;
100 dpdh = 0x0 ;
101 dpdye = 0x0 ;
102 d2p = 0x0 ;
103 Sourcetbl = 0x0 ;
104 }
105
106 //--------------//
107 // Destructor //
108 //--------------//
109
111 if (hhh != 0x0) delete hhh ;
112 if (Y_e != 0x0) delete Y_e ;
113 if (ppp != 0x0) delete ppp ;
114 if (dpdh != 0x0) delete dpdh ;
115 if (dpdye != 0x0) delete dpdye ;
116 if (d2p != 0x0) delete d2p ;
117 if (Sourcetbl != 0x0) delete Sourcetbl ;
118 }
119
120 //------------//
121 // Outputs //
122 //------------//
123
124 void Ye_eos_tabul::sauve(FILE* fich) const {
125
126 Hot_eos::sauve(fich) ;
127
128 char tmp_string[160] ;
129 strcpy(tmp_string, tablename.c_str()) ;
130 fwrite(tmp_string, sizeof(char), 160, fich) ;
131 }
132
133 ostream& Ye_eos_tabul::operator>>(ostream & ost) const {
134
135 ost << "Non-beta equilibrium EOS of class Ye_eos_tabul (tabulated out of beta-equilibrium EoS) : "
136 << endl ;
137 ost << "Built from file " << tablename << endl ;
138 ost << "Authors : " << authors << endl ;
139 ost << "Number of points in file : " << hhh->get_dim(0)
140 << " in enthalpy, and " << hhh->get_dim(1)
141 << " in electronic fraction." << endl ;
142
143 return ost ;
144}
145
146 //------------------------//
147 // Reading of the table //
148 //------------------------//
149
151
152 using namespace Unites ;
153
154 ifstream fich(tablename.data()) ;
155
156 if (!fich) {
157 cerr << "Ye_eos_tabul::read_table(): " << endl ;
158 cerr << "Problem in opening the EOS file!" << endl ;
159 cerr << "While trying to open " << tablename << endl ;
160 cerr << "Aborting..." << endl ;
161 abort() ;
162 }
163
164 fich.ignore(1000, '\n') ; // Jump over the first header
165 fich.ignore(1) ;
166 getline(fich, authors, '\n') ;
167 for (int i=0; i<3; i++) { // jump over the file
168 fich.ignore(1000, '\n') ; // header
169 } //
170
171 int nbp1, nbp2 ;
172 fich >> nbp1 >> nbp2 ; fich.ignore(1000, '\n') ; // number of data
173 if ( (nbp1<=0) || (nbp2<=0) ) {
174 cerr << "Ye_eos_tabul::read_table(): " << endl ;
175 cerr << "Wrong value for the number of lines!" << endl ;
176 cerr << "nbp1 = " << nbp1 << ", nbp2 = " << nbp2 << endl ;
177 cerr << "Aborting..." << endl ;
178 abort() ;
179 }
180
181 for (int i=0; i<3; i++) { // jump over the table
182 fich.ignore(1000, '\n') ; // description
183 }
184
185 ppp = new Tbl(nbp2, nbp1) ;
186 hhh = new Tbl(nbp2, nbp1) ;
187 Y_e = new Tbl(nbp2, nbp1) ;
188 mu_l = new Tbl(nbp2, nbp1) ;
189 c_sound2 = new Tbl(nbp2, nbp1) ;
190 dpdh = new Tbl(nbp2, nbp1) ;
191 dpdye = new Tbl(nbp2, nbp1) ;
192 d2p = new Tbl(nbp2, nbp1) ;
193 Sourcetbl = new Tbl(nbp2, nbp1) ;
194
195 ppp->set_etat_qcq() ;
196 hhh->set_etat_qcq() ;
197 Y_e->set_etat_qcq() ;
198 mu_l->set_etat_qcq() ;
199 c_sound2->set_etat_qcq() ;
200 dpdh->set_etat_qcq() ;
201 dpdye->set_etat_qcq() ;
202 d2p->set_etat_qcq() ;
203 Sourcetbl->set_etat_qcq() ;
204
205 double c2 = c_si * c_si ;
206 double nb_fm3, rho_cgs, p_cgs, ent, ye, mul, der2, cs2, source_d ;
207 double ww = 0. ;
208 int no;
209
210 for (int j=0; j<nbp2; j++) {
211 for (int i=0; i<nbp1; i++) {
212 fich >> no >> nb_fm3>> rho_cgs >> p_cgs>> ent >> ye >> cs2 >> mul >> der2 >> source_d ;
213
214 fich.ignore(1000,'\n') ;
215 if ( (nb_fm3<0) || (rho_cgs<0) || (p_cgs < 0) ){
216 cerr << "Ye_eos_tabul::read_table(): " << endl ;
217 cerr << "Negative value in table!" << endl ;
218 cerr << "nb = " << nb_fm3 << ", rho = " << rho_cgs <<
219 ", p = " << p_cgs << endl ;
220 cerr << "Aborting..." << endl ;
221 abort() ;
222 }
223
224 double psc2 = 0.1*p_cgs/c2 ; // in kg m^-3
225 double rho_si = rho_cgs*1000. ; // in kg m^-3
226
227 double h_read = ent ;
228 if ( (i==0) ) ww = h_read ;
229 double h_new = h_read - ww + 1e-14;
230
231 ppp->set(j, i) = psc2/rhonuc_si ;
232 hhh->set(j, i) = h_new ;
233 Y_e->set(j, i) = ye ;
234 c_sound2->set(j, i) = cs2 ;
235 mu_l->set(j, i) = mul*mev_si*1e44/(rhonuc_si*c2) ;
236 dpdh->set(j, i) = (rho_si + psc2)/rhonuc_si ;
237 dpdye->set(j, i) = -mul*nb_fm3*mevpfm3 ;
238 d2p->set(j, i) = 0.1*der2/(c2*rhonuc_si) ; // Bien revérifier l'expression !!
239 Sourcetbl->set(j, i) = source_d*t_unit ;
240 }
241 }
242
243 hmin = (*hhh)(0, 0) ;
244 hmax = (*hhh)(0, nbp1-1) ;
245
246 yemin = (*Y_e)(0, 0) ;
247 yemax = (*Y_e)(nbp2-1, 0) ;
248
249 cout << "hmin: " << hmin << ", hmax: " << hmax << endl ;
250 cout << "yemin: " << yemin << ", yemax: " << yemax << endl ;
251
252 fich.close();
253}
254
255 //-------------------------------//
256 // The corresponding cold Eos //
257 //-------------------------------//
258
260
261 if (p_cold_eos == 0x0) {
262 cerr << "Warning: Ye_eos_tabul::new_cold_Eos " <<
263 "The corresponding cold EoS is likely not to work" << endl ;
264 cout << "read from file: "<< tablename.c_str() << endl;
265 p_cold_eos = new Eos_CompOSE(tablename.c_str()) ;
266 }
267
268 return *p_cold_eos ;
269 }
270
271
272
273
274
275 //------------------------//
276 // Comparison operators //
277 //------------------------//
278
279
280 bool Ye_eos_tabul::operator==(const Hot_eos& eos_i) const {
281
282 bool resu = true ;
283
284 if ( eos_i.identify() != identify() ) {
285 cout << "The second EOS is not of type Ye_eos_tabul !" << endl ;
286 resu = false ;
287 }
288
289 return resu ;
290 }
291
292
293 bool Ye_eos_tabul::operator!=(const Hot_eos& eos_i) const {
294 return !(operator==(eos_i)) ;
295 }
296
297
298 //------------------------------//
299 // Computational routines //
300 //------------------------------//
301
302// Baryon density from enthalpy and electronic fraction
303//------------------------------------------
304
305double Ye_eos_tabul::nbar_Hs_p(double ent, double ye) const {
306
307 if ((ent > hmin - 1.e-12) && (ent < hmin))
308 ent = hmin ;
309
310 if (ye < yemin) ye = yemin ;
311
312 if ( ent >= hmin ) {
313 if (ent > hmax) {
314 cout << "Ye_eos_tabul::nbar_Hs_p : ent > hmax !" << endl ;
315 abort() ;
316 }
317
318 if (ye > yemax) {
319 cerr << "Ye_eos_tabul::nbar_Hs_p : Y_e not in the tabulated interval !"
320 << endl ;
321 cerr << "Y_e = " << ye << ", yemin = " << yemin << ", yemax = " << yemax
322 << endl ;
323 abort() ;
324 }
325
326 double p_int, dpdye_int, dpdh_int ;
327
328 interpol_herm_2d(*Y_e, *hhh, *ppp, *dpdye, *dpdh, *d2p, ye, ent, p_int,
329 dpdye_int, dpdh_int) ;
330
331 double nbar_int = dpdh_int * exp(-ent) ;
332 return nbar_int ;
333 }
334 else{
335 return 0 ;
336 }
337}
338
339
340// Energy density from enthalpy and electronic fraction
341//-----------------------------------------
342
343double Ye_eos_tabul::ener_Hs_p(double ent, double ye) const {
344
345 if ((ent > hmin - 1.e-12) && (ent < hmin))
346 ent = hmin ;
347
348 if (ye < yemin) ye = yemin ;
349
350 if ( ent >= hmin ) {
351 if (ent > hmax) {
352 cout << "Ye_eos_tabul::ener_Hs_p : ent > hmax !" << endl ;
353 abort() ;
354 }
355
356 if (ye > yemax) {
357 cerr << "Ye_eos_tabul::ener_Hs_p : Y_e not in the tabulated interval !"
358 << endl ;
359 cerr << "Y_e = " << ye << ", yemin = " << yemin << ", yemax = " << yemax
360 << endl ;
361 abort() ;
362 }
363
364 double p_int, dpdye_int, dpdh_int ;
365 interpol_herm_2d(*Y_e, *hhh, *ppp, *dpdye, *dpdh, *d2p, ye, ent, p_int,
366 dpdye_int, dpdh_int) ;
367
368
369 double f_int = - p_int + dpdh_int;
370
371 return f_int ;
372 }
373 else{
374 return 0 ;
375 }
376}
377
378double Ye_eos_tabul::press_Hs_p(double ent, double ye) const {
379
380 if ((ent > hmin - 1.e-12) && (ent < hmin))
381 ent = hmin ;
382
383 if (ye < yemin) ye = yemin ;
384
385 if ( ent >= hmin ) {
386 if (ent > hmax) {
387 cout << "Ye_eos_tabul::press_Hs_p : ent > hmax !" << endl ;
388 abort() ;
389 }
390
391 if (ye > yemax) {
392 cerr << "Ye_eos_tabul::press_Hs_p : Y_e not in the tabulated interval !"
393 << endl ;
394 cerr << "Y_e = " << ye << ", yemin = " << yemin << ", yemax = " << yemax
395 << endl ;
396 abort() ;
397 }
398
399 double p_int, dpdye_int, dpdh_int ;
400 interpol_herm_2d(*Y_e, *hhh, *ppp, *dpdye, *dpdh, *d2p, ye, ent, p_int,
401 dpdye_int, dpdh_int) ;
402
403 return p_int ;
404 }
405 else{
406 return 0 ;
407 }
408}
409
410double Ye_eos_tabul::csound_square_Hs_p(double ent, double ye) const {
411 static int i_near = hhh->get_dim(0) / 2 ;
412 static int j_near = Y_e->get_dim(1) / 2 ;
413
414 if ((ent > hmin - 1.e-12) && (ent < hmin))
415 ent = hmin ;
416
417 if (ye < yemin) ye = yemin ;
418
419 Tbl ye_1D(Y_e->get_dim(1)) ; ye_1D.set_etat_qcq() ;
420 for (int i=0 ; i<Y_e->get_dim(1) ; i++){
421 ye_1D.set(i) = (*Y_e)(i,0) ;
422 }
423 Tbl enthalpy = extract_column(*hhh,ye_1D,ye) ;
424 if ( ent > hmin ) {
425 if (ent > hmax) {
426 cout << "Eos_tabul::csound_square_Hs_p : ent>hmax !" << endl ;
427 abort() ;
428 }
429 if (ye > yemax) {
430 cerr << "Ye_eos_tabul::csound_square_Hs_p : Y_e not in the tabulated interval !"
431 << endl ;
432 cerr << "Y_e = " << ye << ", yemin = " << yemin << ", yemax = " << yemax
433 << endl ;
434 abort() ;
435 }
436 double csound0 ;
437
438
439 interpol_linear_2d(enthalpy, ye_1D, *c_sound2, ent, ye, i_near, j_near, csound0) ;
440
441
442 return csound0 ;
443 }
444 else
445 {
446 double csound0 ;
447 interpol_linear_2d(enthalpy, ye_1D, *c_sound2, hmin, ye, i_near, j_near, csound0) ;
448 return csound0 ;
449 }
450 }
451
452double Ye_eos_tabul::chi2_Hs_p(double, double) const {
453
454 cerr << "Warning : (H,Y_e) EoS have no contribution from chi^2 ; Ye_eos_tabul::chi2_Hs_p :function not implemented." << endl;
455 cerr << "Aborting ..." << endl;
456 abort() ;
457
458 return 0. ;
459 }
460
461double Ye_eos_tabul::mul_Hs_p(double ent, double ye) const {
462 static int i_near = hhh->get_dim(0) / 2 ;
463 static int j_near = Y_e->get_dim(1) / 2 ;
464
465 if ((ent > hmin - 1.e-12) && (ent < hmin))
466 ent = hmin ;
467
468 if (ye < yemin) ye = yemin ;
469
470 Tbl ye_1D(Y_e->get_dim(1)) ; ye_1D.set_etat_qcq() ;
471 for (int i=0 ; i<Y_e->get_dim(1) ; i++){
472 ye_1D.set(i) = (*Y_e)(i,0) ;
473 }
474 Tbl enthalpy = extract_column(*hhh,ye_1D,ye) ;
475
476 if ( ent > hmin ) {
477 if (ent > hmax) {
478 cout << "Eos_tabul::mul_Hs_p : ent>hmax !" << endl ;
479 abort() ;
480 }
481
482 if (ye > yemax) {
483 cerr << "Ye_eos_tabul::mul_Hs_p : Y_e not in the tabulated interval !"
484 << endl ;
485 cerr << "Y_e = " << ye << ", yemin = " << yemin << ", yemax = " << yemax
486 << endl ;
487 abort() ;
488 }
489
490 double mul0 ;
491
492 interpol_linear_2d(enthalpy, ye_1D, *mu_l, ent, ye, i_near, j_near, mul0) ;
493
494 return mul0 ;
495 }
496 else
497 {
498 double mul0 ;
499 interpol_linear_2d(enthalpy, ye_1D, *mu_l, hmin, ye, i_near, j_near, mul0) ;
500 return mul0 ;
501 }
502 }
503
504 double Ye_eos_tabul::sigma_Hs_p(double ent, double ye) const {
505 static int i_near = hhh->get_dim(0) / 2 ;
506 static int j_near = Y_e->get_dim(1) / 2 ;
507
508 if ((ent > hmin - 1.e-12) && (ent < hmin))
509 ent = hmin ;
510
511 if (ye < yemin) ye = yemin ;
512
513 Tbl ye_1D(Y_e->get_dim(1)) ; ye_1D.set_etat_qcq() ;
514 for (int i=0 ; i<Y_e->get_dim(1) ; i++){
515 ye_1D.set(i) = (*Y_e)(i,0) ;
516 }
517 Tbl enthalpy = extract_column(*hhh,ye_1D,ye) ;
518
519 if ( ent > hmin ) {
520 if (ent > hmax) {
521 cout << "Eos_tabul::sigma_Hs_p : ent>hmax !" << endl ;
522 abort() ;
523 }
524
525 if (ye > yemax) {
526 cerr << "Ye_eos_tabul::sigma_Hs_p : Y_e not in the tabulated interval !"
527 << endl ;
528 cerr << "Y_e = " << ye << ", yemin = " << yemin << ", yemax = " << yemax
529 << endl ;
530 abort() ;
531 }
532
533 double sigma0 ;
534
535 interpol_linear_2d(enthalpy, ye_1D, *Sourcetbl, ent, ye, i_near, j_near, sigma0) ;
536
537 return sigma0 ;
538 }
539 else
540 {
541 double sigma0 ;
542 interpol_linear_2d(enthalpy, ye_1D, *Sourcetbl, hmin, ye, i_near, j_near, sigma0) ;
543 return sigma0 ;
544 }
545 }
546
547 double Ye_eos_tabul::temp_Hs_p(double, double) const {
548
549 cerr << "Warning : (H,Y_e) EoS does not use T as a parameter ; Ye_eos_tabul::temp_Hs_p :function not implemented." << endl;
550 cerr << "Aborting ..." << endl;
551 abort() ;
552
553 return 0. ;
554 }
555} //End of namespace Lorene
556
557
Equation of state for the CompOSE database.
Definition eos_compose.h:93
Equation of state base class.
Definition eos.h:206
virtual int identify() const =0
Returns a number to identify the sub-classe of Hot_eos the object belongs to.
Eos * p_cold_eos
Corresponding cold Eos.
Definition hoteos.h:123
Hot_eos()
Standard constructor.
Definition hoteos.C:56
virtual void sauve(FILE *) const
Save in a file.
Definition hoteos.C:129
Basic array class.
Definition tbl.h:161
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition tbl.C:364
double & set(int i)
Read/write of a particular element (index i) (1D case).
Definition tbl.h:281
void set_arrays_0x0()
Sets all the arrays to the null pointer.
virtual double mul_Hs_p(double ent, const double ye) const
Computes the electronic chemical potential from the enthapy with electronic fraction (virtual functio...
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...
string tablename
Name of the file containing the tabulated data.
Definition hoteos.h:987
Tbl * d2p
Table of .
Definition hoteos.h:1025
Tbl * Y_e
Table of , electronic fraction (dimensionless).
Definition hoteos.h:1007
virtual double press_Hs_p(double ent, double ye) const
Computes the pressure from the log-enthalpy and electronic fraction (virtual function implemented in ...
void read_table()
Reads the file containing the table and initializes in the arrays hhh , s_B, ppp, ....
Tbl * hhh
Table of .
Definition hoteos.h:1004
Tbl * Sourcetbl
Table of electronic fraction source.
Definition hoteos.h:1028
double yemax
Upper boundary of the electronic fraction interval.
Definition hoteos.h:1001
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...
virtual int identify() const
Returns a number to identify the sub-classe of Hot_eos the object belongs to.
Tbl * c_sound2
Table of , sound speed squared (units of c^2).
Definition hoteos.h:1010
Tbl * ppp
Table of pressure $P$.
Definition hoteos.h:1016
virtual ostream & operator>>(ostream &) const
Operator >>.
Tbl * dpdye
Table of .
Definition hoteos.h:1022
double hmin
Lower boundary of the enthalpy interval.
Definition hoteos.h:992
virtual bool operator!=(const Hot_eos &) const
Comparison operator (difference).
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...
virtual double ener_Hs_p(double ent, double ye) const
Computes the total energy density from the log-enthalpy and electronic fraction (virtual function imp...
double yemin
Lower boundary of the electronic fraction interval.
Definition hoteos.h:998
virtual double nbar_Hs_p(double ent, double ye) const
Computes the baryon density from the log-enthalpy and electronic fraction (virtual function implement...
Ye_eos_tabul(const string &filename)
Standard constructor from a filename.
Tbl * mu_l
Table of , the electronic chemical potential (MeV).
Definition hoteos.h:1013
string authors
Authors - reference for the table.
Definition hoteos.h:989
virtual void sauve(FILE *) const
Save in a file.
double hmax
Upper boundary of the enthalpy interval.
Definition hoteos.h:995
virtual bool operator==(const Hot_eos &) const
Comparison operator (egality).
virtual const Eos & new_cold_Eos() const
Returns the corresponding cold Eos.
Tbl * dpdh
Table of .
Definition hoteos.h:1019
virtual ~Ye_eos_tabul()
Destructor.
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...
Cmp exp(const Cmp &)
Exponential.
Definition cmp_math.C:273
Lorene prototypes.
Definition app_hor.h:67
Tbl extract_column(const Tbl &, const Tbl &, double)
Coord x
x coordinate centered on the grid
Definition map.h:738
Standard units of space, time and mass.