189 lines
3.9 KiB
C++
189 lines
3.9 KiB
C++
#ifndef SPHLITE_HPP
|
|
#define SPHLITE_HPP
|
|
|
|
#include "sphlite_c_api.h"
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace sphlite {
|
|
|
|
enum Phase {
|
|
PhaseWater = SPHLITE_PHASE_WATER,
|
|
PhaseGas = SPHLITE_PHASE_GAS,
|
|
PhaseExplosive = SPHLITE_PHASE_EXPLOSIVE,
|
|
PhaseSolid = SPHLITE_PHASE_SOLID,
|
|
PhaseShell = SPHLITE_PHASE_SHELL,
|
|
PhaseBoundary = SPHLITE_PHASE_BOUNDARY,
|
|
PhaseCoupling = SPHLITE_PHASE_COUPLING,
|
|
PhaseSolidFailed = SPHLITE_PHASE_SOLID_FAILED,
|
|
PhaseShellFailed = SPHLITE_PHASE_SHELL_FAILED
|
|
};
|
|
|
|
enum Status {
|
|
StatusActive = SPHLITE_STATUS_ACTIVE,
|
|
StatusInactive = SPHLITE_STATUS_INACTIVE,
|
|
StatusFailed = SPHLITE_STATUS_FAILED,
|
|
StatusReserved = SPHLITE_STATUS_RESERVED
|
|
};
|
|
|
|
struct Version {
|
|
int major;
|
|
int minor;
|
|
int patch;
|
|
};
|
|
|
|
struct ParticleState {
|
|
std::vector<int> particle_id_ext;
|
|
std::vector<int> phase;
|
|
std::vector<int> status;
|
|
std::vector<int> material_id;
|
|
std::vector<int> section_id;
|
|
std::vector<double> x;
|
|
std::vector<double> v;
|
|
std::vector<double> a;
|
|
std::vector<double> rho;
|
|
std::vector<double> pressure;
|
|
std::vector<double> internal_energy;
|
|
std::vector<double> temperature;
|
|
std::vector<double> h;
|
|
};
|
|
|
|
class Error : public std::runtime_error {
|
|
public:
|
|
explicit Error(const std::string &message) : std::runtime_error(message) {}
|
|
};
|
|
|
|
inline std::string last_error_message()
|
|
{
|
|
int code = 0;
|
|
char message[256];
|
|
if (sphlite_get_last_error(&code, message, static_cast<int>(sizeof(message))) != 0) {
|
|
return "SPHLite C API parameter error";
|
|
}
|
|
return std::string(message);
|
|
}
|
|
|
|
inline void check(int ierr)
|
|
{
|
|
if (ierr != 0) {
|
|
throw Error(last_error_message());
|
|
}
|
|
}
|
|
|
|
inline void initialize()
|
|
{
|
|
check(sphlite_initialize());
|
|
}
|
|
|
|
inline void finalize()
|
|
{
|
|
check(sphlite_finalize());
|
|
}
|
|
|
|
inline Version version()
|
|
{
|
|
Version result = {0, 0, 0};
|
|
check(sphlite_get_version(&result.major, &result.minor, &result.patch));
|
|
return result;
|
|
}
|
|
|
|
inline void read_inp(const std::string &filename)
|
|
{
|
|
check(sphlite_read_inp(filename.c_str()));
|
|
}
|
|
|
|
inline void build_model()
|
|
{
|
|
check(sphlite_build_model());
|
|
}
|
|
|
|
inline int particle_count()
|
|
{
|
|
int count = 0;
|
|
check(sphlite_get_particle_count(&count));
|
|
return count;
|
|
}
|
|
|
|
inline void run_explicit()
|
|
{
|
|
check(sphlite_run_explicit());
|
|
}
|
|
|
|
inline ParticleState particle_state()
|
|
{
|
|
int count = particle_count();
|
|
ParticleState state;
|
|
state.particle_id_ext.resize(count);
|
|
state.phase.resize(count);
|
|
state.status.resize(count);
|
|
state.material_id.resize(count);
|
|
state.section_id.resize(count);
|
|
state.x.resize(3 * count);
|
|
state.v.resize(3 * count);
|
|
state.a.resize(3 * count);
|
|
state.rho.resize(count);
|
|
state.pressure.resize(count);
|
|
state.internal_energy.resize(count);
|
|
state.temperature.resize(count);
|
|
state.h.resize(count);
|
|
int actual = 0;
|
|
check(sphlite_get_particle_state(count, state.particle_id_ext.data(),
|
|
state.phase.data(), state.status.data(), state.material_id.data(),
|
|
state.section_id.data(), state.x.data(), state.v.data(), state.a.data(),
|
|
state.rho.data(), state.pressure.data(), state.internal_energy.data(),
|
|
state.temperature.data(), state.h.data(), &actual));
|
|
return state;
|
|
}
|
|
|
|
inline void set_output_directory(const std::string &path)
|
|
{
|
|
check(sphlite_set_output_directory(path.c_str()));
|
|
}
|
|
|
|
inline void write_output(int step, double time)
|
|
{
|
|
check(sphlite_write_output(step, time));
|
|
}
|
|
|
|
inline void write_restart(const std::string &filename)
|
|
{
|
|
check(sphlite_write_restart(filename.c_str()));
|
|
}
|
|
|
|
inline void read_restart(const std::string &filename)
|
|
{
|
|
check(sphlite_read_restart(filename.c_str()));
|
|
}
|
|
|
|
class Session {
|
|
public:
|
|
Session() : active_(true) { initialize(); }
|
|
|
|
~Session()
|
|
{
|
|
if (active_) {
|
|
sphlite_finalize();
|
|
}
|
|
}
|
|
|
|
Session(const Session &) = delete;
|
|
Session &operator=(const Session &) = delete;
|
|
|
|
void close()
|
|
{
|
|
if (active_) {
|
|
finalize();
|
|
active_ = false;
|
|
}
|
|
}
|
|
|
|
private:
|
|
bool active_;
|
|
};
|
|
|
|
} // namespace sphlite
|
|
|
|
#endif
|