SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
helm.h
Go to the documentation of this file.
1/* ***********************************************************************
2//
3// Copyright (C) 2025 -- The 4D-STAR Collaboration
4// File Authors: Aaron Dotter, Emily Boudreaux
5// Last Modified: March 20, 2025
6//
7// 4DSSE is free software; you can use it and/or modify
8// it under the terms and restrictions the GNU General Library Public
9// License version 3 (GPLv3) as published by the Free Software Foundation.
10//
11// 4DSSE is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Library General Public License for more details.
15//
16// You should have received a copy of the GNU Library General Public License
17// along with this software; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19//
20// *********************************************************************** */
21// this file contains a C++ conversion of Frank Timmes' fortran code
22// helmholtz.f90, which implements the Helmholtz Free Energy EOS described
23// by Timmes & Swesty (2000) doi:10.1086/313304 -- Aaron Dotter 2025
24
25#pragma once
26
27
28#include <array>
29#include <iostream>
30#include <iomanip>
31#include <sstream>
32#include <string>
33#include <format>
34
35#include "debug.h"
36
37namespace serif::eos::helmholtz {
38 static constexpr int IMAX = 541;
39 static constexpr int JMAX = 201;
46 template <typename T, std::size_t J, std::size_t I>
47 using array2D = std::array<std::array<T, J>, I>;
48
49 double** heap_allocate_contiguous_2D_memory(int rows, int cols);
50
51 void heap_deallocate_contiguous_2D_memory(double **array);
52
53 const double tlo = 3.0, thi = 13.0;
54 const double dlo = -12.0, dhi = 15.0;
55 const double tstp = (thi - tlo) / (JMAX - 1), tstpi = 1 / tstp;
56 const double dstp = (dhi - dlo) / (IMAX - 1), dstpi = 1 / dstp;
57
62 struct HELMTable
63 {
64 bool loaded = false;
65 const int imax = IMAX, jmax = JMAX;
66 double t[JMAX], d[IMAX];
67 double** f;
68 double** fd;
69 double** ft;
70 double** fdd;
71 double** ftt;
72 double** fdt;
73 double** fddt;
74 double** fdtt;
75 double** fddtt;
76 double** dpdf;
77 double** dpdfd;
78 double** dpdft;
79 double** dpdfdt;
80 double** ef;
81 double** efd;
82 double** eft;
83 double** efdt;
84 double** xf;
85 double** xfd;
86 double** xft;
87 double** xfdt;
88
91
92 // Constructor
116
117 // Destructor
141
142 friend std::ostream& operator<<(std::ostream& os, const helmholtz::HELMTable& table) {
143 if (!table.loaded) {
144 os << "HELMTable not loaded\n";
145 return os;
146 }
147 double tMin = std::numeric_limits<double>::max(), tMax = std::numeric_limits<double>::lowest();
148 double dMin = std::numeric_limits<double>::max(), dMax = std::numeric_limits<double>::lowest();
149
150 for (const auto& temp : table.t) {
151 if (temp < tMin) tMin = temp;
152 if (temp > tMax) tMax = temp;
153 }
154
155 for (const auto& dens : table.d) {
156 if (dens < dMin) dMin = dens;
157 if (dens > dMax) dMax = dens;
158 }
159
160 os << "HELMTable Data:\n";
161 os << " imax: " << table.imax << ", jmax: " << table.jmax << "\n";
162 os << " Temperature Range: [" << tMin << ", " << tMax << "]\n";
163 os << " Density Range: [" << dMin << ", " << dMax << "]\n";
164
165 return os;
166 }
167
168
169 };
170
176 {
177 double T;
178 double rho;
179 double abar;
180 double zbar;
181
182 friend std::ostream& operator<<(std::ostream& os, const helmholtz::HELMEOSInput& eosInput) {
183 os << "HELMEOSInput Data:\n";
184 os << " Temperature: " << eosInput.T << "\n";
185 os << " Density: " << eosInput.rho << "\n";
186 os << " Mean Atomic Mass: " << eosInput.abar << "\n";
187 os << " Mean Atomic Number: " << eosInput.zbar << "\n";
188
189 return os;
190 }
191 };
192
198 {
199 // output
200 double ye, etaele, xnefer; //
201 double ptot, pgas, prad; // pressure
202 double etot, egas, erad; // energy
203 double stot, sgas, srad; // entropy
204
205 // derivatives
209
210 // these could become functions:
211 double chiT, chiRho, csound, grad_ad; // derived quantities
212 double gamma1, gamma2, gamma3, cV, cP; // derived quantities
213 double dse, dpe, dsp; // Maxwell relations
214
215 friend std::ostream& operator<<(std::ostream& os, const helmholtz::HELMEOSOutput& eos) {
216 os << "EOS Data:\n" << std::setw(20) << std::left;
217 os << " Electron Fraction: " << std::format("{0:24.16e}",eos.ye) << "\n";
218 os << " Electron Chemical Potential: " << std::format("{0:24.16e}",eos.etaele) << "\n";
219 os << " Electron Number Density: " << std::format("{0:24.16e}",eos.xnefer) << "\n";
220 os << " Total Pressure: " << std::format("{0:24.16e}",eos.ptot) << "\n";
221 os << " dPres/dRho: " << std::format("{0:24.16e}",eos.dpresdd) << "\n";
222 os << " dPres/dT: " << std::format("{0:24.16e}",eos.dpresdt) << "\n";
223 os << " Gas Pressure: " << std::format("{0:24.16e}",eos.pgas) << "\n";
224 os << " Radiation Pressure: " << std::format("{0:24.16e}",eos.prad) << "\n";
225 os << " Total Energy: " << std::format("{0:24.16e}",eos.etot) << "\n";
226 os << " dEner/dRho: " << std::format("{0:24.16e}",eos.denerdd) << "\n";
227 os << " dEner/dT: " << std::format("{0:24.16e}",eos.denerdt) << "\n";
228 os << " Gas Energy: " << std::format("{0:24.16e}",eos.egas) << "\n";
229 os << " Radiation Energy: " << std::format("{0:24.16e}",eos.erad) << "\n";
230 os << " Total Entropy: " << std::format("{0:24.16e}",eos.stot) << "\n";
231 os << " dEntr/dRho: " << std::format("{0:24.16e}",eos.dentrdd) << "\n";
232 os << " dEntr/dT: " << std::format("{0:24.16e}",eos.dentrdt) << "\n";
233 os << " Gas Entropy: " << std::format("{0:24.16e}",eos.sgas) << "\n";
234 os << " Radiation Entropy: " << std::format("{0:24.16e}",eos.srad);
235 return os;
236 }
237 };
238
239 // interpolating polynomial function definitions
240
246 double psi0(double z);
247
253 double dpsi0(double z);
254
260 double ddpsi0(double z);
261
267 double psi1(double z);
268
274 double dpsi1(double z);
275
281 double ddpsi1(double z);
282
288 double psi2(double z);
289
295 double dpsi2(double z);
296
302 double ddpsi2(double z);
303
309 double xpsi0(double z);
310
316 double xdpsi0(double z);
317
323 double xpsi1(double z);
324
330 double xdpsi1(double z);
331
345 double h3(const std::array<double, 36> &fi, const double w0t, const double w1t, const double w0mt, const double w1mt,
346 const double w0d, const double w1d, const double w0md, const double w1md);
347
365 double h5(const std::array<double, 36> &fi, const double w0t, const double w1t, const double w2t, const double w0mt,
366 const double w1mt, const double w2mt, const double w0d, const double w1d, const double w2d,
367 const double w0md, const double w1md, const double w2md);
368
374 std::unique_ptr<HELMTable> read_helm_table(const std::string& filename);
375
382 HELMEOSOutput get_helm_EOS(HELMEOSInput &q, const HELMTable &table);
383
384}
385
Defines a macro for triggering a breakpoint in different compilers and platforms.
double ddpsi1(const double z)
Second derivative of the interpolating polynomial function psi1.
Definition helm.cpp:80
const double dlo
Definition helm.h:54
double psi0(const double z)
Interpolating polynomial function psi0.
Definition helm.cpp:70
double dpsi0(const double z)
Derivative of the interpolating polynomial function psi0.
Definition helm.cpp:72
double psi1(const double z)
Interpolating polynomial function psi1.
Definition helm.cpp:76
static constexpr int IMAX
Definition helm.h:38
double xdpsi1(const double z)
Derivative of the interpolating polynomial function xpsi1.
Definition helm.cpp:94
const double tstpi
Definition helm.h:55
double h3(const std::array< double, 36 > &fi, const double w0t, const double w1t, const double w0mt, const double w1mt, const double w0d, const double w1d, const double w0md, const double w1md)
Interpolating polynomial function h3.
Definition helm.cpp:96
const double dstp
Definition helm.h:56
double ddpsi2(const double z)
Second derivative of the interpolating polynomial function psi2.
Definition helm.cpp:86
const double thi
Definition helm.h:53
double h5(const std::array< double, 36 > &fi, const double w0t, const double w1t, const double w2t, const double w0mt, const double w1mt, const double w2mt, const double w0d, const double w1d, const double w2d, const double w0md, const double w1md, const double w2md)
Interpolating polynomial function h5.
Definition helm.cpp:108
const double dhi
Definition helm.h:54
double xdpsi0(const double z)
Derivative of the interpolating polynomial function xpsi0.
Definition helm.cpp:90
static constexpr int JMAX
Definition helm.h:39
double dpsi2(const double z)
Derivative of the interpolating polynomial function psi2.
Definition helm.cpp:84
double dpsi1(const double z)
Derivative of the interpolating polynomial function psi1.
Definition helm.cpp:78
double xpsi1(const double z)
Interpolating polynomial function xpsi1.
Definition helm.cpp:92
double ddpsi0(const double z)
Second derivative of the interpolating polynomial function psi0.
Definition helm.cpp:74
const double tlo
Definition helm.h:53
const double dstpi
Definition helm.h:56
void heap_deallocate_contiguous_2D_memory(double **array)
Definition helm.cpp:66
double xpsi0(const double z)
Interpolating polynomial function xpsi0.
Definition helm.cpp:88
double ** heap_allocate_contiguous_2D_memory(const int rows, const int cols)
Definition helm.cpp:52
std::array< std::array< T, J >, I > array2D
2D array template alias.
Definition helm.h:47
serif::eos::helmholtz::HELMEOSOutput get_helm_EOS(serif::eos::helmholtz::HELMEOSInput &q, const serif::eos::helmholtz::HELMTable &table)
Calculate the Helmholtz EOS components.
Definition helm.cpp:243
const double tstp
Definition helm.h:55
double psi2(const double z)
Interpolating polynomial function psi2.
Definition helm.cpp:82
std::unique_ptr< HELMTable > read_helm_table(const std::string &filename)
Read the Helmholtz EOS table from a file.
Definition helm.cpp:132
Structure to hold the input parameters for the EOS calculation.
Definition helm.h:176
friend std::ostream & operator<<(std::ostream &os, const helmholtz::HELMEOSInput &eosInput)
Definition helm.h:182
double abar
Mean atomic mass.
Definition helm.h:179
double zbar
Mean atomic number.
Definition helm.h:180
friend std::ostream & operator<<(std::ostream &os, const helmholtz::HELMEOSOutput &eos)
Definition helm.h:215
Structure to hold the Helmholtz EOS table data.
Definition helm.h:63
friend std::ostream & operator<<(std::ostream &os, const helmholtz::HELMTable &table)
Definition helm.h:142