Skip to content

Commit

Permalink
Merge pull request #348 from PrincetonUniversity/issue-343
Browse files Browse the repository at this point in the history
Property reader / writer
  • Loading branch information
icui authored Jan 14, 2025
2 parents cafebb2 + 02e4b58 commit d10bdf3
Show file tree
Hide file tree
Showing 70 changed files with 1,718 additions and 597 deletions.
28 changes: 16 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ option(BUILD_TESTS "Tests included" OFF)
option(BUILD_EXAMPLES "Examples included" ON)
option(ENABLE_SIMD "Enable SIMD" OFF)
option(ENABLE_PROFILING "Enable profiling" OFF)
option(SPECFEMPP_ENABLE_PYTHON "Enable Python binding" OFF)
option(SPECFEMPP_BINDING_PYTHON "Enable Python binding" OFF)
# set(CMAKE_BUILD_TYPE Release)
set(CHUNK_SIZE 32)
set(NUM_CHUNKS 1)
Expand All @@ -25,7 +25,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# For external modules that need to be built suppress build output
set(FETCHCONTENT_QUIET TRUE)

if (SPECFEMPP_ENABLE_PYTHON)
if (SPECFEMPP_BINDING_PYTHON)
message("-- Adding -fPIC flag for Python binding.")
if(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
set(CMAKE_CXX_FLAGS "-fp-model=precise -fPIC")
Expand All @@ -36,15 +36,15 @@ if (SPECFEMPP_ENABLE_PYTHON)
else()
set(CMAKE_CXX_FLAGS "-fPIC")
endif()
else (SPECFEMPP_ENABLE_PYTHON)
else (SPECFEMPP_BINDING_PYTHON)
if(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
set(CMAKE_CXX_FLAGS "-fp-model=precise")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
message("-- Detected Intel classic compiler which will be deprecated soon.")
message("-- It is recommended you use IntelLLVM compiler.")
set(CMAKE_CXX_FLAGS "-diag-disable=10441 -fp-model=precise")
endif()
endif (SPECFEMPP_ENABLE_PYTHON)
endif (SPECFEMPP_BINDING_PYTHON)

# Check if MacOS
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Expand Down Expand Up @@ -341,7 +341,7 @@ target_link_libraries(

add_library(
read_seismogram
src/reader/seismogram.cpp
src/IO/seismogram/reader.cpp
)

target_link_libraries(
Expand All @@ -351,7 +351,8 @@ target_link_libraries(

add_library(
reader
src/reader/wavefield.cpp
src/IO/wavefield/reader.cpp
src/IO/property/reader.cpp
)

target_link_libraries(
Expand Down Expand Up @@ -535,9 +536,10 @@ target_link_libraries(

add_library(
writer
src/writer/seismogram.cpp
src/writer/wavefield.cpp
src/writer/kernel.cpp
src/IO/seismogram/writer.cpp
src/IO/wavefield/writer.cpp
src/IO/kernel/writer.cpp
src/IO/property/writer.cpp
)

target_link_libraries(
Expand Down Expand Up @@ -589,6 +591,7 @@ add_library(
src/parameter_parser/writer/wavefield.cpp
src/parameter_parser/writer/plot_wavefield.cpp
src/parameter_parser/writer/kernel.cpp
src/parameter_parser/writer/property.cpp
)

target_link_libraries(
Expand Down Expand Up @@ -674,7 +677,7 @@ if (SPECFEMPP_USE_SKBUILD AND EXISTS ${SKBUILD_SCRIPTS_DIR})
install(FILES ${CMAKE_BINARY_DIR}/xmeshfem2D DESTINATION ${SKBUILD_SCRIPTS_DIR} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE)
endif ()

if (SPECFEMPP_ENABLE_PYTHON)
if (SPECFEMPP_BINDING_PYTHON)
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)
python_add_library(_core MODULE src/specfem2d.cpp WITH_SOABI)
Expand All @@ -689,6 +692,7 @@ if (SPECFEMPP_ENABLE_PYTHON)
parameter_reader
receiver_class
writer
plotter
reader
domain
medium
Expand All @@ -698,7 +702,7 @@ if (SPECFEMPP_ENABLE_PYTHON)
${BOOST_LIBS}
pybind11::headers
)
target_compile_definitions(_core PRIVATE SPECFEMPP_ENABLE_PYTHON)
target_compile_definitions(_core PRIVATE SPECFEMPP_BINDING_PYTHON)
target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})
install(TARGETS _core DESTINATION specfempp_core)
endif (SPECFEMPP_ENABLE_PYTHON)
endif (SPECFEMPP_BINDING_PYTHON)
6 changes: 3 additions & 3 deletions docs/api/IO/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ formats for the reading of the mesh is binary, and for sources and receivers it
is yaml.

In addition to these basic read functions, there are also the two
reader and writer classes, :cpp:class:`specfem::reader::wavefield` and
:cpp:class:`specfem::writer::wavefield`, which support both HDF5 and ASCII I/O.
And, to write seismograms, we can use :cpp:class:`specfem::writer::seismogram`.
reader and writer classes, :cpp:class:`specfem::IO::wavefield_reader` and
:cpp:class:`specfem::IO::wavefield_writer`, which support both HDF5 and ASCII I/O.
And, to write seismograms, we can use :cpp:class:`specfem::IO::seismogram_writer`.
Seismogram I/O is only supported in ASCII format thus far.

The slightly-lower level functionality to read and write data to and from disk
Expand Down
2 changes: 1 addition & 1 deletion docs/api/IO/reader/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Reader

The ``reader`` class provides interfaces to read simulation data as output files.

.. doxygenclass:: specfem::writer::writer
.. doxygenclass:: specfem::IO::writer

Types of readers
----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/api/IO/reader/wavefield.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
Wavefield Reader
================

.. doxygenclass:: specfem::reader::wavefield
.. doxygenclass:: specfem::IO::wavefield_reader
:members:
2 changes: 1 addition & 1 deletion docs/api/IO/writer/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Writer

The ``writer`` class provides interfaces to write simulation data as output files.

.. doxygenclass:: specfem::writer::writer
.. doxygenclass:: specfem::IO::writer
:members:

Types of writers
Expand Down
2 changes: 1 addition & 1 deletion docs/api/IO/writer/seismogram_writer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Seismogram Writer

The ``seismogram`` provides methods to write seismograms to a file.

.. doxygenclass:: specfem::writer::seismogram
.. doxygenclass:: specfem::IO::seismogram_writer
:members:
2 changes: 1 addition & 1 deletion docs/api/IO/writer/wavefield.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
Wavefield Writer
================

.. doxygenclass:: specfem::writer::wavefield
.. doxygenclass:: specfem::IO::wavefield_writer
:members:
6 changes: 3 additions & 3 deletions fortran/meshfem3d/setup/precision.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ either version 3 of the License,
!
! You should have received a copy of the GNU General Public License along
! with this program;
if
not, write to the Free Software Foundation, Inc., !51 Franklin Street,
Fifth Floor, Boston,
if not
, write to the Free Software Foundation, Inc., !51 Franklin Street, Fifth Floor,
Boston,
MA 02110 - 1301 USA.! != == == == == == == == == == == == == == == == == ==
== == == == == == == == == == == == == == == == ==

Expand Down
22 changes: 8 additions & 14 deletions include/writer/kernel.hpp → include/IO/kernel/writer.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#ifndef _SPECFEM_WRITER_KERNEL_HPP
#define _SPECFEM_WRITER_KERNEL_HPP
#pragma once

#include "compute/interface.hpp"
#include "enumerations/interface.hpp"
#include "writer/writer.hpp"
#include "IO/writer.hpp"

namespace specfem {
namespace writer {
namespace IO {
/**
* @brief Writer to output misfit kernel data to disk
*
* @tparam OutputLibrary Library to use for output (HDF5, ASCII, etc.)
*/
template <typename OutputLibrary> class kernel : public writer {
template <typename OutputLibrary> class kernel_writer : public writer {
public:
/**
* @name Constructors
Expand All @@ -26,21 +25,16 @@ template <typename OutputLibrary> class kernel : public writer {
* @param output_folder Path to output location (will be an .h5 file if using
* HDF5, and a folder if using ASCII)
*/
kernel(const specfem::compute::assembly &assembly,
const std::string output_folder);
kernel_writer(const std::string output_folder);

/**
* @brief write the kernel data to disk
*
*/
void write() override;
void write(specfem::compute::assembly &assembly) override;

private:
std::string output_folder; ///< Path to output folder
specfem::compute::mesh mesh; ///< Mesh object
specfem::compute::kernels kernels; ///< Kernels object
std::string output_folder; ///< Path to output folder
};
} // namespace writer
} // namespace IO
} // namespace specfem

#endif /* _SPECFEM_WRITER_KERNEL_HPP */
19 changes: 8 additions & 11 deletions include/writer/kernel.tpp → include/IO/kernel/writer.tpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#ifndef _SPECFEM_WRITER_KERNEL_TPP
#define _SPECFEM_WRITER_KERNEL_TPP
#pragma once

#include "compute/assembly/assembly.hpp"
#include "enumerations/dimension.hpp"
#include "enumerations/medium.hpp"
#include "kokkos_abstractions.h"
#include "point/kernels.hpp"
#include "writer/kernel.hpp"
#include "IO/kernel/writer.hpp"
#include <Kokkos_Core.hpp>

template <typename OutputLibrary>
specfem::writer::kernel<OutputLibrary>::kernel(
const specfem::compute::assembly &assembly, const std::string output_folder)
: output_folder(output_folder), mesh(assembly.mesh),
kernels(assembly.kernels) {}
specfem::IO::kernel_writer<OutputLibrary>::kernel_writer(const std::string output_folder)
: output_folder(output_folder) {}

template <typename OutputLibrary>
void specfem::writer::kernel<OutputLibrary>::write() {
void specfem::IO::kernel_writer<OutputLibrary>::write(specfem::compute::assembly &assembly) {
const auto &mesh = assembly.mesh;
auto &kernels = assembly.kernels;

using DomainView =
Kokkos::View<type_real ***, Kokkos::LayoutLeft, Kokkos::HostSpace>;
Expand Down Expand Up @@ -167,7 +166,7 @@ void specfem::writer::kernel<OutputLibrary>::write() {
const specfem::point::index<specfem::dimension::type::dim2> index(
ispec, iz, ix);
specfem::point::kernels<specfem::dimension::type::dim2,
specfem::element::medium_tag::elastic,
specfem::element::medium_tag::acoustic,
specfem::element::property_tag::isotropic,
false>
point_kernels;
Expand Down Expand Up @@ -195,5 +194,3 @@ void specfem::writer::kernel<OutputLibrary>::write() {
std::cout << "Kernels written to " << output_folder << "/Kernels"
<< std::endl;
}

#endif /* _SPECFEM_WRITER_KERNEL_TPP */
42 changes: 42 additions & 0 deletions include/IO/property/reader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "compute/interface.hpp"
#include "enumerations/interface.hpp"
#include "IO/reader.hpp"

namespace specfem {
namespace IO {
/**
* @brief Read model property
*
* @tparam InputLibrary Library to use for output (HDF5, ASCII, etc.)
*/
template <typename InputLibrary> class property_reader : public reader {
public:
/**
* @name Constructors
*
*/
///@{
/**
* @brief Construct a reader object
*
* @param output_folder Path to input location (will be an .h5 file if using
* HDF5, and a folder if using ASCII)
*/
property_reader(const std::string input_folder);

/**
* @brief read the property from disk
*
* @param assembly SPECFEM++ assembly
*
*/
void read(specfem::compute::assembly &assembly) override;

private:
std::string input_folder; ///< Path to output folder
specfem::compute::properties properties; ///< Properties object
};
} // namespace IO
} // namespace specfem
57 changes: 57 additions & 0 deletions include/IO/property/reader.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "compute/assembly/assembly.hpp"
#include "enumerations/dimension.hpp"
#include "enumerations/medium.hpp"
#include "kokkos_abstractions.h"
#include "point/properties.hpp"
#include "IO/property/reader.hpp"
#include <Kokkos_Core.hpp>

template <typename InputLibrary>
specfem::IO::property_reader<InputLibrary>::property_reader(const std::string input_folder): input_folder(input_folder) {}

template <typename InputLibrary>
void specfem::IO::property_reader<InputLibrary>::read(specfem::compute::assembly &assembly) {
auto &properties = assembly.properties;

using DomainView =
Kokkos::View<type_real ***, Kokkos::LayoutLeft, Kokkos::HostSpace>;

typename InputLibrary::File file(input_folder + "/Properties");

{
typename InputLibrary::Group elastic = file.openGroup("/ElasticIsotropic");

elastic.openDataset("rho", properties.elastic_isotropic.h_rho).read();
elastic.openDataset("mu", properties.elastic_isotropic.h_mu).read();
elastic.openDataset("lambdaplus2mu", properties.elastic_isotropic.h_lambdaplus2mu).read();
}

{
typename InputLibrary::Group elastic = file.openGroup("/ElasticAnisotropic");

elastic.openDataset("rho", properties.elastic_anisotropic.h_rho).read();
elastic.openDataset("c11", properties.elastic_anisotropic.h_c11).read();
elastic.openDataset("c13", properties.elastic_anisotropic.h_c13).read();
elastic.openDataset("c15", properties.elastic_anisotropic.h_c15).read();
elastic.openDataset("c33", properties.elastic_anisotropic.h_c33).read();
elastic.openDataset("c35", properties.elastic_anisotropic.h_c35).read();
elastic.openDataset("c55", properties.elastic_anisotropic.h_c55).read();
elastic.openDataset("c12", properties.elastic_anisotropic.h_c12).read();
elastic.openDataset("c23", properties.elastic_anisotropic.h_c23).read();
elastic.openDataset("c25", properties.elastic_anisotropic.h_c25).read();
}

{
typename InputLibrary::Group acoustic = file.openGroup("/Acoustic");

acoustic.openDataset("rho_inverse", properties.acoustic_isotropic.h_rho_inverse).read();
acoustic.openDataset("kappa", properties.acoustic_isotropic.h_kappa).read();
}

std::cout << "Properties read from " << input_folder << "/Properties"
<< std::endl;

properties.copy_to_device();
}
Loading

0 comments on commit d10bdf3

Please sign in to comment.