Skip to content

Commit

Permalink
Convert Inputs class to json;
Browse files Browse the repository at this point in the history
Library is now header only
  • Loading branch information
streeve committed Aug 14, 2023
1 parent 0495cac commit 49f0565
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 187 deletions.
8 changes: 3 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ configure_file(CabanaPD_config.hpp.cmakein CabanaPD_config.hpp @ONLY)

file(GLOB HEADERS GLOB *.hpp)

file(GLOB SOURCES *.cpp)

install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CabanaPD_config.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

add_library(CabanaPD ${SOURCES})
add_library(CabanaPD INTERFACE)

target_include_directories(CabanaPD PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
target_include_directories(CabanaPD INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)

target_link_libraries(CabanaPD Cabana::cabanacore Cabana::Cajita nlohmann_json::nlohmann_json)
target_link_libraries(CabanaPD INTERFACE Cabana::cabanacore Cabana::Cajita nlohmann_json::nlohmann_json)

install(TARGETS CabanaPD DESTINATION ${CMAKE_INSTALL_LIBDIR})
141 changes: 0 additions & 141 deletions src/CabanaPD_Input.cpp

This file was deleted.

75 changes: 55 additions & 20 deletions src/CabanaPD_Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,69 @@
#ifndef INPUTS_H
#define INPUTS_H

#include <fstream>
#include <iostream>
#include <string>

#include <nlohmann/json.hpp>

namespace CabanaPD
{

class Inputs
{
public:
std::string output_file = "cabanaPD.out";
std::string error_file = "cabanaPD.err";
std::string device_type = "SERIAL";

std::array<int, 3> num_cells;
std::array<double, 3> low_corner;
std::array<double, 3> high_corner;

std::size_t num_steps;
double final_time;
double timestep;
int output_frequency;

bool half_neigh = false;

Inputs( const std::array<int, 3> nc, std::array<double, 3> lc,
std::array<double, 3> hc, const double t_f, const double dt,
const int output_freq );
~Inputs();
void read_args( int argc, char* argv[] );
Inputs( const std::string filename )
{
// Get user inputs.
inputs = parse( filename );

// Add additional derived inputs to json. System size.
std::array<double, 3> size = inputs["system_size"];
inputs["low_corner"] = { -0.5 * size[0], -0.5 * size[1],
-0.5 * size[2] };
inputs["high_corner"] = { 0.5 * size[0], 0.5 * size[1], 0.5 * size[2] };

// Number of steps.
double tf = inputs["final_time"];
double dt = inputs["timestep"];
int num_steps = tf / dt;
inputs["num_steps"] = num_steps;

// Output files.
if ( !inputs.contains( "output_file" ) )
inputs["output_file"] = "cabanaPD.out";
if ( !inputs.contains( "error_file" ) )
inputs["error_file"] = "cabanaPD.err";
inputs["input_file"] = filename;

// Save inputs (including derived) to new file.
std::string input_file = "cabanaPD.in.json";
if ( !inputs.contains( "exported_input_file" ) )
inputs["exported_input_file"] = input_file;
std::ofstream in( input_file );
in << inputs;

// Not yet a user option.
inputs["half_neigh"] = false;
}
~Inputs() {}

// Parse JSON file.
inline nlohmann::json parse( const std::string& filename )
{
std::ifstream stream( filename );
return nlohmann::json::parse( stream );
}

// Get a single input.
auto operator[]( std::string label ) { return inputs[label]; }

// Check a key exists.
bool contains( std::string label ) { return inputs.contains( label ); }

protected:
nlohmann::json inputs;
};

} // namespace CabanaPD
Expand Down
49 changes: 28 additions & 21 deletions src/CabanaPD_Solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class SolverElastic

SolverElastic( Inputs _inputs, std::shared_ptr<particle_type> _particles,
force_model_type force_model )
: particles( _particles )
, inputs( std::make_shared<Inputs>( _inputs ) )
: inputs( _inputs )
, particles( _particles )
{
force_time = 0;
integrate_time = 0;
Expand All @@ -122,12 +122,13 @@ class SolverElastic
total_timer.reset();
init_timer.reset();

num_steps = inputs->num_steps;
output_frequency = inputs->output_frequency;
num_steps = inputs["num_steps"];
output_frequency = inputs["output_frequency"];

// Create integrator.
// FIXME: hardcoded.
integrator = std::make_shared<integrator_type>( inputs->timestep, 1.0 );
dt = inputs["timestep"];
integrator = std::make_shared<integrator_type>( dt, 1.0 );

// Add ghosts from other MPI ranks.
comm = std::make_shared<comm_type>( *particles );
Expand All @@ -146,7 +147,8 @@ class SolverElastic
int max_neighbors =
Cabana::NeighborList<neighbor_type>::maxNeighbor( *neighbors );

force = std::make_shared<force_type>( inputs->half_neigh, force_model );
force =
std::make_shared<force_type>( inputs["half_neigh"], force_model );

print = print_rank();
if ( print )
Expand All @@ -155,8 +157,10 @@ class SolverElastic
", Maximum local neighbors: ", max_neighbors );
log( std::cout, "#Timestep/Total-steps Simulation-time" );

std::ofstream out( inputs->output_file, std::ofstream::app );
std::ofstream err( inputs->error_file, std::ofstream::app );
output_file = inputs["output_file"];
std::ofstream out( output_file, std::ofstream::app );
error_file = inputs["error_file"];
std::ofstream err( error_file, std::ofstream::app );

auto time = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now() );
Expand Down Expand Up @@ -236,8 +240,7 @@ class SolverElastic
neigh_iter_tag() );

step_output( step, W );
particles->output( step / output_frequency,
step * inputs->timestep );
particles->output( step / output_frequency, step * dt );
}
other_time += other_timer.seconds();
}
Expand All @@ -249,7 +252,7 @@ class SolverElastic
void init_output()
{
// Output after construction and initial forces.
std::ofstream out( inputs->output_file, std::ofstream::app );
std::ofstream out( output_file, std::ofstream::app );
log( out, "Init-Time(s): ", init_time, "\n" );
log( out, "#Timestep/Total-steps Simulation-time Total-strain-energy "
"Run-Time(s) Force-Time(s) Comm-Time(s) Int-Time(s) "
Expand All @@ -260,18 +263,18 @@ class SolverElastic
{
if ( print )
{
std::ofstream out( inputs->output_file, std::ofstream::app );
std::ofstream out( output_file, std::ofstream::app );
log( std::cout, step, "/", num_steps, " ", std::scientific,
std::setprecision( 2 ), step * inputs->timestep );
std::setprecision( 2 ), step * dt );

total_time = total_timer.seconds();
double rate = 1.0 * particles->n_global * output_frequency /
( total_time - last_time );
log( out, std::fixed, std::setprecision( 6 ), step, "/", num_steps,
" ", std::scientific, std::setprecision( 2 ),
step * inputs->timestep, " ", W, " ", std::fixed, total_time,
" ", force_time, " ", comm_time, " ", integrate_time, " ",
other_time, " ", std::scientific, rate );
" ", std::scientific, std::setprecision( 2 ), step * dt, " ",
W, " ", std::fixed, total_time, " ", force_time, " ",
comm_time, " ", integrate_time, " ", other_time, " ",
std::scientific, rate );
last_time = total_time;
out.close();
}
Expand All @@ -281,7 +284,7 @@ class SolverElastic
{
if ( print )
{
std::ofstream out( inputs->output_file, std::ofstream::app );
std::ofstream out( output_file, std::ofstream::app );
total_time = total_timer.seconds();
double steps_per_sec = 1.0 * num_steps / total_time;
double p_steps_per_sec = particles->n_global * steps_per_sec;
Expand All @@ -304,15 +307,19 @@ class SolverElastic

int num_steps;
int output_frequency;
double dt;

protected:
Inputs inputs;
std::shared_ptr<particle_type> particles;
std::shared_ptr<Inputs> inputs;
std::shared_ptr<comm_type> comm;
std::shared_ptr<integrator_type> integrator;
std::shared_ptr<force_type> force;
std::shared_ptr<neighbor_type> neighbors;

std::string output_file;
std::string error_file;

double total_time;
double force_time;
double integrate_time;
Expand Down Expand Up @@ -445,8 +452,7 @@ class SolverFracture
neigh_iter_tag() );

this->step_output( step, W );
particles->output( step / output_frequency,
step * inputs->timestep );
particles->output( step / output_frequency, step * dt );
}
other_time += other_timer.seconds();
}
Expand All @@ -455,6 +461,7 @@ class SolverFracture
this->final_output();
}

using base_type::dt;
using base_type::num_steps;
using base_type::output_frequency;

Expand Down

0 comments on commit 49f0565

Please sign in to comment.