Skip to content

Commit

Permalink
Merge pull request #308 from lanl/dholladay00/fpp
Browse files Browse the repository at this point in the history
Enable fortran preprocessor for fortran interface and then add same l…
  • Loading branch information
jhp-lanl authored Oct 31, 2023
2 parents 7985c18 + cc23a92 commit 4e214d1
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 131 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [[PR228]](https://github.com/lanl/singularity-eos/pull/228) added untracked header files in cmake
- [[PR215]](https://github.com/lanl/singularity-eos/pull/215) and [[PR216]](https://github.com/lanl/singularity-eos/pull/216) fix duplicate definition of EPS and fix CI
- [[PR232]](https://github.com/lanl/singularity-eos/pull/228) Fixed uninitialized cmake path variables
- [[PR308]](https://github.com/lanl/singularity-eos/pull/308) spack builds +fortran now compile via correct blocking out of interfaces via preprocessor ifdef

### Added (new features/APIs/variables/...)
- [[PR306]](https://github.com/lanl/singularity-eos/pull/306) Added generic Evaluate method
Expand Down Expand Up @@ -48,10 +49,12 @@
- [[PR234]](https://github.com/lanl/singularity-eos/pull/234) update ports-of-call to correct for undefined behavior in error handling
- [[PR219]](https://github.com/lanl/singularity-eos/pull/219) Removed static analysis from re-git pipeline
- [[PR233]](https://github.com/lanl/singularity-eos/pull/233) Exposed entropy for the EOS type (now required for future EOS)
- [[PR308]](https://github.com/lanl/singularity-eos/pull/308) Fortran initialization interface functions no longer require modifier arrays, they are optional parameters.

### Infrastructure (changes irrelevant to downstream codes)
- [[PR190]](https://github.com/lanl/singularity-eos/pull/190) update CI on re-git
- [[PR245]](https://github.com/lanl/singularity-eos/pull/245) Separating get_sg_eos to other files. Build/compilation improvements, warning fixes/suppression.
- [[PR308]](https://github.com/lanl/singularity-eos/pull/308) Added a fortran test.

### Removed (removing behavior/API/varaibles/...)
- [[PR293]](https://github.com/lanl/singularity-eos/pull/293) Removing PTofRE function. This will no longer be callable downstream.
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ if(SINGULARITY_USE_FORTRAN)
target_include_directories(
singularity-eos INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include/singularity-eos/eos>)
set_target_properties(singularity-eos PROPERTIES Fortran_PREPROCESS ON)
endif() # SINGULARITY_USE_FORTRAN

target_include_directories(
Expand Down
6 changes: 3 additions & 3 deletions singularity-eos/eos/modifiers/ramps_eos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ void pAlpha2BilinearRampParams(const T &eos, const Real alpha0, const Real Pe,
template <typename T>
class BilinearRampEOS : public EosBase<BilinearRampEOS<T>> {
public:
// Vector functions that overload the scalar versions declared here.
SG_ADD_BASE_CLASS_USINGS(BilinearRampEOS<T>)

// move semantics ensures dynamic memory comes along for the ride
BilinearRampEOS(T &&t, const Real r0, const Real a, const Real b, const Real c)
: t_(std::forward<T>(t)), r0_(r0), a_(a), b_(b), c_(c),
Expand Down Expand Up @@ -425,9 +428,6 @@ class BilinearRampEOS : public EosBase<BilinearRampEOS<T>> {
t_.ValuesAtReferenceState(rho, temp, sie, press, cv, bmod, dpde, dvdt, lambda);
}

// Vector functions that overload the scalar versions declared here.
SG_ADD_BASE_CLASS_USINGS(BilinearRampEOS<T>)

inline constexpr bool IsModified() const { return true; }

inline constexpr T UnmodifyOnce() { return t_; }
Expand Down
224 changes: 174 additions & 50 deletions singularity-eos/eos/singularity_eos.f90

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ if(SINGULARITY_USE_SPINER)
target_link_libraries(profile_eos singularity-eos::singularity-eos)
endif()

if(SINGULARITY_USE_FORTRAN)
add_executable(ftn_interface test_f_iface.f90)
target_link_libraries(ftn_interface singularity-eos::singularity-eos)
set_target_properties(ftn_interface PROPERTIES LINKER_LANGUAGE Fortran)
add_test(test_fortran_interface ftn_interface)
endif()

if(SINGULARITY_USE_EOSPAC
AND SINGULARITY_TEST_SESAME
AND NOT SINGULARITY_USE_CUDA)
Expand Down
14 changes: 12 additions & 2 deletions test/eos_unit_test_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include <cstdio>
#include <cstdlib>

#include <iomanip>
#include <limits>
#include <sstream>

// typename demangler
#ifdef __GNUG__
#include <cstdlib>
Expand Down Expand Up @@ -64,9 +68,15 @@ PORTABLE_INLINE_FUNCTION Real myAtan(Real x, Real shift, Real scale, Real offset
template <typename X, typename Y, typename Z, typename ZT, typename XN, typename YN>
inline void array_compare(int num, X &&x, Y &&y, Z &&z, ZT &&ztrue, XN xname, YN yname,
Real tol = 1e-12) {
assert(num > 0);
using underlying_t =
typename std::remove_cv<typename std::remove_reference<decltype(x[0])>::type>::type;
for (int i = 0; i < num; i++) {
INFO("i: " << i << ", " << xname << ": " << x[i] << ", " << yname << ": " << y[i]
<< ", Value: " << z[i] << ", True Value: " << ztrue[i]);
auto s = std::ostringstream{};
s << std::setprecision(std::numeric_limits<underlying_t>::max_digits10)
<< std::scientific << "i: " << i << ", " << xname << ": " << x[i] << ", " << yname
<< ": " << y[i] << ", Value: " << z[i] << ", True Value: " << ztrue[i];
INFO(s.str());
CHECK(isClose(z[i], ztrue[i], 1e-12));
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_eos_gruneisen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ SCENARIO("Gruneisen EOS density limit") {
INFO("FillEos bmod: " << bmod << ", Lookup bmod: " << bmod_true);
CHECK(bmod == bmod_true);
INFO("FillEos pressure: " << P << ", Lookup pressure: " << pres_true);
CHECK(P == pres_true);
CHECK(isClose(P, pres_true, 1.e-14));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_eos_stellar_collapse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ SCENARIO("Test 3D reinterpolation to fast log grid", "[StellarCollapse]") {
constexpr int N2 = 100;
constexpr int N1 = 101;
constexpr int N0 = 102;
StellarCollapse::Grid_t g2(0, 1, N2);
StellarCollapse::Grid_t g2(1.0 / N2, 1, N2);
StellarCollapse::Grid_t g1(1, 3, N1);
StellarCollapse::Grid_t g0(2, 4, N0);
StellarCollapse::DataBox db(N2, N1, N0);
Expand Down
12 changes: 6 additions & 6 deletions test/test_eos_stiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ SCENARIO("StiffGas1", "[StiffGas][StiffGas1]") {

// Gold standard values for a subset of lookups
constexpr std::array<Real, num> pressure_true{
1.0132500000019073e+06, 3.4450500000000000e+07, 6.7887750000001907e+07,
1.0132500000000000e+08};
1.01324999999964016e+06, 3.44504999999983162e+07, 6.78877500000010729e+07,
1.01324999999999583e+08};
constexpr std::array<Real, num> bulkmodulus_true{
2.3502381137500000e+10, 2.3580958675000000e+10, 2.3659536212500004e+10,
2.3738113750000004e+10};
2.35023811375000000e+10, 2.35809586749999962e+10, 2.36595362125000038e+10,
2.37381137500000000e+10};
constexpr std::array<Real, num> temperature_true{
2.9814999999999998e+02, 1.5320999999999999e+03, 2.7660500000000002e+03,
4.0000000000000000e+03};
2.98149999999999920e+02, 1.53209999999999968e+03, 2.76605000000000064e+03,
3.99999999999999955e+03};
constexpr std::array<Real, num> gruneisen_true{1.35, 1.35, 1.35, 1.35};

#ifdef PORTABILITY_STRATEGY_KOKKOS
Expand Down
85 changes: 17 additions & 68 deletions test/test_f_iface.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,81 +12,30 @@
! publicly and display publicly, and to permit others to do so.
!------------------------------------------------------------------------------

program test
use bedroom_door_eos
integer :: res, nmat, ncell, i, j, matid
type(bd_eos_ary_t) :: eos
integer, allocatable, dimension(:) :: offsets
real(kind=8), allocatable, dimension(:) :: spvol, sie_tot, vsum, press, pmax
real(kind=8), allocatable, dimension(:) :: temp, bmod, dpde, cv
real(kind=8), allocatable, dimension(:,:) :: frac_mass, frac_vol, frac_sie
real(kind=8) :: vsumint
character(len=64) :: filename
program test_sg_fortran_interface
! modules
use singularity_eos_types
use singularity_eos
! no implicit vars
implicit none
! variable declaration
integer :: nmat, res
type(sg_eos_ary_t) :: eos

ncell = 1
! set test parameters
nmat = 3
filename = "../data/materials.sp5"

! allocate arrays
allocate(spvol(ncell), sie_tot(ncell), temp(ncell), press(ncell), vsum(ncell))
allocate(bmod(ncell), dpde(ncell), cv(ncell), pmax(ncell))
allocate(offsets(ncell))
allocate(frac_mass(ncell, nmat))
allocate(frac_vol(ncell, nmat), frac_sie(ncell, nmat))
! initialize eos's
res = init_bd_eos_f(nmat, eos)
! allocate and initialize eos's
res = init_sg_eos_f(nmat, eos)

res = init_Gruneisen_f(1, eos, 394000.d0, 1.489d0, 0.d0, 0.d0, 2.02d0, 0.47d0,&
res = init_sg_Gruneisen_f(1, eos, 394000.d0, 1.489d0, 0.d0, 0.d0, 2.02d0, 0.47d0,&
8.93d0, 297.0d0, 1.0d6, 0.383d7)
res = init_DavisReactants_f(2, eos, 1.890d0, 4.115d10, 1.0d6, 297.0d0, 1.8d0,&
res = init_sg_DavisReactants_f(2, eos, 1.890d0, 4.115d10, 1.0d6, 297.0d0, 1.8d0,&
4.6d0, 0.34d0, 0.56d0,0.d0, 0.4265d0, 0.001074d10)
res = init_DavisProducts_f(3, eos, 0.798311d0, 0.58d0, 1.35d0, 2.66182d0,&
res = init_sg_DavisProducts_f(3, eos, 0.798311d0, 0.58d0, 1.35d0, 2.66182d0,&
0.75419d0, 3.2d10, 0.001072d10, 0.d0)
!matid = 4272
!res = init_SpinerDependsRhoT_f(2, eos, filename, matid)
!matid = 5030
!res = init_SpinerDependsRhoT_f(3, eos, filename, matid)
! initialize state variables
call srand(10)
press = 0.d0
do i=1,ncell
vsum(i) = 1.0d-2
frac_mass(i,1) = 8.93d0*vsum(i)*0.33
frac_mass(i,2) = 1.89d0*vsum(i)*.033
frac_mass(i,3) = 2.5d0*vsum(i)*0.34
sie_tot(i) = 0.d0
spvol(i) = 0.d0
!vsum(i) = 0.d0
offsets(i) = i
frac_sie(i,1) = 1.16049000000000000d+09
frac_sie(i,2) = 4.50111706015744858d+10
frac_sie(i,3) = 3.29477034927098885d+10
temp(i) = 0.d0
! frac_vols will be normalized in eos call
do j=1,nmat
frac_vol(i,j) = 0.d0
enddo
do j=1,nmat
spvol(i) = spvol(i) + frac_mass(i,j)
sie_tot(i) = sie_tot(i) + frac_mass(i,j)*frac_sie(i,j)
frac_sie(i,j) = frac_sie(i,j)*frac_mass(i,j) ! needs to be energy
enddo
sie_tot(i) = sie_tot(i)/spvol(i)
spvol(i) = vsum(i)/spvol(i)
enddo

!write(*,*) 'spvol', spvol
write(*,*) frac_mass, frac_vol, frac_sie
! calculate PTE eos
res = get_bd_eos_f(nmat, ncell, ncell, 0, eos, offsets, press, pmax, vsum,&
spvol, sie_tot, temp, bmod, dpde, cv, frac_mass, frac_vol,&
frac_sie)

write(*,*) press, cv, bmod, temp
write(*,*) frac_mass, frac_vol, frac_sie
! cleanup
res = finalize_bd_eos_f(nmat, eos)
res = finalize_sg_eos_f(nmat, eos)

deallocate(vsum, sie_tot, spvol, frac_mass, temp, press, frac_vol, frac_sie)
deallocate(bmod, dpde, cv, offsets, pmax)
end program test
end program test_sg_fortran_interface

0 comments on commit 4e214d1

Please sign in to comment.