LORENE
read_compose_table.C
1 /*
2  * Function to read a CompOSE table (general purpose or neutron star matter).
3  *
4  */
5 
6 /*
7  * Copyright (c) 2024 Jerome Novak
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 as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
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 // Lorene headers
28 #include "tbl.h"
29 #include "unites.h"
30 
31 
32 namespace Lorene {
33 
34  bool read_compose_table(const string& tablename, Tbl*& p_ental, Tbl*& p_entro,
35  Tbl*& p_press, Tbl*& p_ener, Tbl*& p_nb, Tbl*& p_temp,
36  Tbl*& p_ye) {
37 
38  bool resu = true ;
39  if (p_ental != nullptr) delete p_ental ;
40  if (p_entro != nullptr) delete p_entro ;
41  if (p_press != nullptr) delete p_press ;
42  if (p_ener != nullptr) delete p_ener ;
43  if (p_nb != nullptr) delete p_nb ;
44  if (p_temp != nullptr) delete p_temp ;
45  if (p_ye != nullptr) delete p_ye ;
46 
47  // Files containing data and a test
48  //---------------------------------
49  string file_nb = tablename + ".nb" ;
50  string file_t = tablename + ".t" ;
51  string file_ye = tablename + ".yq" ;
52  string file_thermo = tablename + ".thermo" ;
53 
54  ifstream in_nb(file_nb.data()) ;
55  ifstream in_t(file_t.data()) ;
56  ifstream in_ye(file_ye.data()) ;
57  if ((!in_nb)||(!in_t)||(!in_ye)) {
58  cerr << "Problem in opening the EOS files!" << endl ;
59  cerr << "While trying to open " << tablename << endl ;
60  cerr << "Aborting ... " << endl ;
61  abort() ;
62  }
63 
64  // reading density, temperature & Ye tables
65  //------------------------------------------
66  int index1_n, index2_n ;
67  in_nb >> index1_n >> index2_n ;
68  int nbn = index2_n - index1_n + 1 ;
69  p_nb = new Tbl(nbn) ; p_nb->set_etat_qcq() ;
70  for (int i=0; i<nbn; i++)
71  in_nb >> p_nb->set(i) ;
72 
73  int index1_t, index2_t ;
74  in_t >> index1_t >> index2_t ;
75  int nbt = index2_t - index1_t + 1 ;
76  p_temp = new Tbl(nbt) ; p_temp->set_etat_qcq() ;
77  for (int j=0; j<nbt; j++)
78  in_t >> p_temp->set(j) ;
79 
80  int index1_y, index2_y ;
81  in_ye >> index1_y >> index2_y ;
82  int nby = index2_y - index1_y + 1 ;
83  p_ye = new Tbl(nby) ; p_ye->set_etat_qcq() ;
84  for (int k=0; k<nby; k++)
85  in_ye >> p_ye->set(k) ;
86 
87  // Arrays for other thermo quantities
88  //------------------------------------
89  p_ener = new Tbl(nby, nbn, nbt) ; p_ener->set_etat_qcq() ; (*p_ener) = NAN ;
90  p_press = new Tbl(nby, nbn, nbt) ; p_press->set_etat_qcq() ;
91  p_ental = new Tbl(nby, nbn, nbt) ; p_ental->set_etat_qcq() ; // Enthalpy
92  p_entro = new Tbl(nby, nbn, nbt) ; p_entro->set_etat_qcq() ; // Entropy / baryon
93 
94  // Variables and conversion
95  //-------------------------
96  double e_compose, p_compose, p_over_nb_comp, s_per_bar, mub_shifted, eps_comp ;
97  double dummy_x ;
98  int dummy_n ;
99 
100  // Conversion from MeV/fm^3 to cgs
101  double p_convert = Unites::mev_si * 1.e45 * 10. ;
102  // From meV/fm^3 to g/cm^3
103  double eps_convert = Unites::mev_si * 1.e42 / (Unites::c_si*Unites::c_si) ;
104  double rhonuc_cgs = Unites::rhonuc_si * 1e-3 ;
105  double c2_cgs = Unites::c_si * Unites::c_si * 1e4 ;
106 
107  double m_neutron_MeV, m_proton_MeV ;
108 
109  ifstream in_thermo(file_thermo.data()) ;
110  if (!in_thermo) {
111  cerr << "Problem in opening the EOS file!" << endl ;
112  cerr << "While trying to open " << file_thermo << endl ;
113  cerr << "Aborting ... " << endl ;
114  abort() ;
115  }
116 
117  // neutron and proton masses
118  in_thermo >> m_neutron_MeV >> m_proton_MeV >> dummy_n;
119 
120  long int ntot = nby*nbn*nbt ;
121 
122  // Main loop reading the table
123  //----------------------------
124 #ifndef NDEBUG
125  cout << "Reading " << file_thermo << " ... " << flush ;
126 #endif
127 
128  for (int i=0; i<ntot; i++) {
129 #ifndef NDEBUG
130  if (!in_thermo) {
131  cerr << "Problem in EOS file: seems too small!" << endl ;
132  cerr << "Name: " << file_thermo << endl ;
133  cerr << "Aborting ... " << endl ;
134  abort() ;
135  }
136 #endif
137  int it, inb, iye ;
138  in_thermo >> it >> inb >> iye >> p_over_nb_comp ; // T, nB, Ye
139  in_thermo >> s_per_bar >> mub_shifted >> dummy_x >> dummy_x >> dummy_x
140  >> eps_comp ;
141  in_thermo.ignore(1000, '\n') ;
142  double nb0 = (*p_nb)(inb - index1_n) ;
143  p_compose = p_over_nb_comp * nb0 ;
144  e_compose = ( eps_comp + 1. ) * m_neutron_MeV * nb0 ;
145  //double mub_compose = (mub_shifted + 1.)*m_neutron_MeV ;
146  if ( (e_compose<0) || (p_compose < 0) ){
147  cout << "Negative value in table!" << endl ;
148  cout << "e = " << e_compose << ", p = " << p_compose << endl ;
149  cout << "x = " << it << ", y = " << inb << ", z = " << iye << endl ;
150  resu = false ;
151  }
152  p_ener->set(iye-index1_y, inb-index1_n, it-index1_t)
153  = e_compose*eps_convert / rhonuc_cgs;
154  p_press->set(iye-index1_y, inb-index1_n, it-index1_t)
155  = p_compose * p_convert / (c2_cgs*rhonuc_cgs) ;
156  double ent = ( e_compose + p_compose) / (m_neutron_MeV*nb0) ;
157  p_ental->set(iye-index1_y, inb-index1_n, it-index1_t)
158  = ent ;
159  p_entro->set(iye-index1_y, inb-index1_n, it-index1_t)
160  = s_per_bar ;
161  }
162  cout << "done! " << endl ;
163 #ifndef NDEBUG
164  for (int i=0; i<ntot; i++) {
165  if (isnan(p_ener->t[i])) {
166  cerr << "Missing points in EoS table for " << file_thermo << endl ;
167  resu = false ;
168  }
169  }
170 #endif
171 
172  return resu ;
173  } // end of read_compose_table()
174 
175 } // end of namespace Lorene
Lorene prototypes.
Definition: app_hor.h:67
double & set(int i)
Read/write of a particular element (index i) (1D case)
Definition: tbl.h:301
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition: tbl.C:364
bool read_compose_table(const string &tablename, Tbl *&p_ental, Tbl *&p_entro, Tbl *&p_press, Tbl *&p_ener, Tbl *&p_nb, Tbl *&p_temp, Tbl *&p_ye)
A function to read CompOSE table and return themro quantities in Lorene units.
double * t
The array of double.
Definition: tbl.h:176
Basic array class.
Definition: tbl.h:164