Skip to content

Commit

Permalink
Unit conversions (#539)
Browse files Browse the repository at this point in the history
* Utilities and unit conversions

* Formatting

* 2 structs and renamed functions to avoid macros issue

* Templating and namespaces

* Added Utilities to Docs

* Overload instead of templating
  • Loading branch information
dmontgomeryNREL authored Nov 5, 2024
1 parent 0674560 commit e265383
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Docs/sphinx/Utility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ In addition to routines for evaluating chemical reactions, transport properties,
* Turbulent inflows (``TurbInflow``) on domain boundaries from saved turbulence data
* Plt file management (``PltFileManager``)
* Output of runtime ``Diagnostics``
* Basic ``Utilities``, including unit conversions

This section provides relevant notes on using these utilities across the Pele family of codes. There may be additional subtleties based on the code-specific implementation of these capabilities, in which case it will be necessary to refer to the documentation of the code of interest.

Expand Down Expand Up @@ -146,3 +147,26 @@ discussed in PeleC milestone reports. An example call to the filtering operation
les_filter.apply_filter(bxtmp, flux[i], filtered_flux[i], Density, NUM_STATE);

The user must ensure that the correct number of grow cells is present in the Fab or MultiFab.

Utilities
=============================
The utilities namespace contains unit conversions, which are particularly useful for PeleLM(eX) users working with mixed unit systems. The following aliases, defined in a file header, enable straightforward conversions between MKS and CGS units for use in equation of state (``eos``) function calls:

::

namespace m2c = pele::physics::utilities::mks2cgs;
namespace c2m = pele::physics::utilities::cgs2mks;

For example, users can call ``eos.PYT2R`` as follows:

::

auto eos = pele::physics::PhysicsType::eos();
amrex::Real P_mean = 101325.0_rt; // Pressure in Pa (MKS)
amrex::Real massfrac[NUM_SPECIES]; // Mass fractions tracked by PeleLM(eX)
amrex::Real Temp = 300.0_rt; // Temp in K
amrex::Real rho_cgs = 0.0_rt; // Density in g/cm^3 (CGS)
// Calculate density in CGS units, then convert to MKS
eos.PYT2R(m2c::P(P_mean), massfrac, Temp, rho_cgs);
amrex::Real rho = c2m::Rho(rho_cgs); // Convert eos density to MKS
1 change: 1 addition & 0 deletions Source/PelePhysics.H
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "PelePhysicsConstraints.H"
#include "EOS.H"
#include "Transport.H"
#include "Utilities.H"

namespace pele::physics {

Expand Down
4 changes: 4 additions & 0 deletions Source/Utility/Utilities/Make.package
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CEXE_headers += Utilities.H UnitConversions.H

VPATH_LOCATIONS += $(PELE_PHYSICS_HOME)/Source/Utility/Utilities
INCLUDE_LOCATIONS += $(PELE_PHYSICS_HOME)/Source/Utility/Utilities
271 changes: 271 additions & 0 deletions Source/Utility/Utilities/UnitConversions.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
#ifndef UNITCONVERSIONS_H
#define UNITCONVERSIONS_H

#include <AMReX.H>
#include <AMReX_REAL.H>
#include <AMReX_Utility.H>

namespace pele::physics::utilities {
namespace cgs2mks {
// CGS to MKS conversions
AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Length(const amrex::Real L_cgs)
{
return L_cgs * 1.0e-2; // cm to m
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Length(const amrex::Real L_cgs, const amrex::Real n)
{
return L_cgs * std::pow(1.0e-2, n); // cm^n to m^n
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Mass(const amrex::Real M_cgs)
{
return M_cgs * 1.0e-3; // g to kg
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Mass(const amrex::Real M_cgs, const amrex::Real n)
{
return M_cgs * std::pow(1.0e-3, n); // g^n to kg^n
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Energy(const amrex::Real E_cgs)
{
return E_cgs * 1.0e-7; // erg to J
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Rho(const amrex::Real rho_cgs)
{
return rho_cgs * 1.0e3; // g/cm^3 to kg/m^3
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
U(const amrex::Real u_cgs)
{
return u_cgs * 1.0e-2; // cm/s to m/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
RhoU(const amrex::Real rhou_cgs)
{
return rhou_cgs * 1.0e1; // g/cm^2/s to kg/m^2/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
P(const amrex::Real p_cgs)
{
return p_cgs * 1.0e-1; // dyne/cm^2 to Pa
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
H(const amrex::Real h_cgs)
{
return h_cgs * 1.0e-4; // erg/g to J/kg
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
RhoH(const amrex::Real rhoh_cgs)
{
return rhoh_cgs * 1.0e1; // erg/cm^3 to J/m^3
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Nu(const amrex::Real nu_cgs)
{
return nu_cgs * 1.0e-4; // cm^2/s to m^2/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Mu(const amrex::Real mu_cgs)
{
return mu_cgs * 1.0e-1; // g/cm/s to kg/m/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Cp(const amrex::Real cp_cgs)
{
return cp_cgs * 1.0e-4; // erg/g/K to J/kg/K
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Alpha(const amrex::Real a_cgs)
{
return a_cgs * 1.0e-4; // cm^2/s to m^2/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Lambda(const amrex::Real l_cgs)
{
return l_cgs * 1.0e-5; // g/cm/s^3/K to W/m/K
}
} // namespace cgs2mks

namespace mks2cgs {
// MKS to CGS conversions
AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Length(const amrex::Real L_mks)
{
return L_mks * 1.0e2; // m to cm
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Length(const amrex::Real L_mks, const amrex::Real n)
{
return L_mks * std::pow(1.0e2, n); // m^n to cm^n
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Mass(const amrex::Real M_mks)
{
return M_mks * 1.0e3; // kg to g
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Mass(const amrex::Real M_mks, const amrex::Real n)
{
return M_mks * std::pow(1.0e3, n); // kg^n to g^n
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Energy(const amrex::Real E_mks)
{
return E_mks * 1.0e7; // J to erg
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Rho(const amrex::Real Rho_mks)
{
return Rho_mks * 1.0e-3; // kg/m^3 to g/cm^3
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
U(const amrex::Real u_mks)
{
return u_mks * 1.0e2; // m/s to cm/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
RhoU(const amrex::Real Rhou_mks)
{
return Rhou_mks * 1.0e-1; // kg/m^2/s to g/cm^2/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
P(const amrex::Real p_mks)
{
return p_mks * 1.0e1; // Pa to dyne/cm^2
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
H(const amrex::Real h_mks)
{
return h_mks * 1.0e4; // J/kg to erg/g
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
RhoH(const amrex::Real Rhoh_mks)
{
return Rhoh_mks * 1.0e1; // J/m^3 to erg/cm^3
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Nu(const amrex::Real Nu_mks)
{
return Nu_mks * 1.0e4; // m^2/s to cm^2/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Mu(const amrex::Real Mu_mks)
{
return Mu_mks * 1.0e1; // kg/m/s to g/cm/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Cp(const amrex::Real cp_mks)
{
return cp_mks * 1.0e4; // J/kg/K to erg/g/K
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Alpha(const amrex::Real a_mks)
{
return a_mks * 1.0e4; // m^2/s to cm^2/s
}

AMREX_GPU_HOST_DEVICE
AMREX_FORCE_INLINE
amrex::Real
Lambda(const amrex::Real l_mks)
{
return l_mks * 1.0e5; // W/m/K to g/cm/s^3/K
}
} // namespace mks2cgs
} // namespace pele::physics::utilities
#endif
11 changes: 11 additions & 0 deletions Source/Utility/Utilities/Utilities.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef UTILITIES_H
#define UTILITIES_H

#include "UnitConversions.H"
namespace pele::physics {
namespace utilities {

} // namespace utilities
} // namespace pele::physics

#endif
1 change: 1 addition & 0 deletions Testing/Exec/Make.PelePhysics
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ include $(UTILITY_HOME)/PMF/Make.package
include $(UTILITY_HOME)/PltFileManager/Make.package
include $(UTILITY_HOME)/TurbInflow/Make.package
include $(UTILITY_HOME)/BlackBoxFunction/Make.package
include $(UTILITY_HOME)/Utilities/Make.package

Pdirs := Base Boundary AmrCore

Expand Down

0 comments on commit e265383

Please sign in to comment.