1
0
Fork 0
mirror of https://github.com/cosmo-sims/monofonIC.git synced 2024-09-19 17:03:45 +02:00
monofonIC/include/output_plugin.hh

103 lines
2.8 KiB
C++
Raw Normal View History

2019-05-09 21:41:54 +02:00
/*
output_plugin.hh - This file is part of MUSIC2 -
2019-05-09 21:41:54 +02:00
a code to generate multi-scale initial conditions
for cosmological simulations
Copyright (C) 2019 Oliver Hahn
2019-05-09 21:41:54 +02:00
*/
#pragma once
2019-05-09 21:41:54 +02:00
#include <string>
2019-07-31 11:57:40 +02:00
#include <cstring>
2019-05-09 21:41:54 +02:00
#include <map>
#include <particle_container.hh>
#include <general.hh>
#include <grid_fft.hh>
#include <config_file.hh>
2019-05-09 21:41:54 +02:00
class output_plugin
2019-07-31 11:57:40 +02:00
{
protected:
//! reference to the ConfigFile object that holds all configuration options
ConfigFile &cf_;
//! output file or directory name
std::string fname_;
//! name of the output interface
2019-07-31 11:57:40 +02:00
std::string interface_name_;
public:
//! constructor
output_plugin(ConfigFile &cf, std::string interface_name )
2019-07-31 11:57:40 +02:00
: cf_(cf), interface_name_(interface_name)
{
fname_ = cf_.GetValue<std::string>("output", "filename");
}
//! virtual destructor
virtual ~output_plugin(){}
//! routine to write particle data for a species
virtual void write_particle_data(const particle_container &pc, const cosmo_species &s ) {};
2019-07-31 11:57:40 +02:00
//! routine to write gridded fluid component data for a species
virtual void write_grid_data(const Grid_FFT<real_t> &g, const cosmo_species &s, const fluid_component &c ) {};
//! routine to query whether species is written as grid data
virtual bool write_species_as_grid( const cosmo_species &s ) = 0;
//! routine to query whether species is written as particle data
virtual bool write_species_as_particles( const cosmo_species &s ){ return !write_species_as_grid(s); }
//! routine to return a multiplicative factor that contains the desired position units for the output
2019-07-31 11:57:40 +02:00
virtual real_t position_unit() const = 0;
//! routine to return a multiplicative factor that contains the desired velocity units for the output
2019-07-31 11:57:40 +02:00
virtual real_t velocity_unit() const = 0;
2019-05-09 21:41:54 +02:00
};
/*!
* @brief implements abstract factory design pattern for output plug-ins
*/
struct output_plugin_creator
{
//! create an instance of a plug-in
2019-07-31 11:57:40 +02:00
virtual std::unique_ptr<output_plugin> create(ConfigFile &cf) const = 0;
2019-05-09 21:41:54 +02:00
//! destroy an instance of a plug-in
2019-07-31 11:57:40 +02:00
virtual ~output_plugin_creator() {}
2019-05-09 21:41:54 +02:00
};
2019-07-31 11:57:40 +02:00
//! maps the name of a plug-in to a pointer of the factory pattern
std::map<std::string, output_plugin_creator *> &get_output_plugin_map();
2019-05-09 21:41:54 +02:00
//! print a list of all registered output plug-ins
void print_output_plugins();
/*!
* @brief concrete factory pattern for output plug-ins
*/
2019-07-31 11:57:40 +02:00
template <class Derived>
2019-05-09 21:41:54 +02:00
struct output_plugin_creator_concrete : public output_plugin_creator
{
//! register the plug-in by its name
2019-07-31 11:57:40 +02:00
output_plugin_creator_concrete(const std::string &plugin_name)
2019-05-09 21:41:54 +02:00
{
2019-07-31 11:57:40 +02:00
get_output_plugin_map()[plugin_name] = this;
2019-05-09 21:41:54 +02:00
}
2019-07-31 11:57:40 +02:00
2019-05-09 21:41:54 +02:00
//! create an instance of the plug-in
2019-07-31 11:57:40 +02:00
std::unique_ptr<output_plugin> create(ConfigFile &cf) const
2019-05-09 21:41:54 +02:00
{
2019-07-31 11:57:40 +02:00
return std::make_unique<Derived>(cf); // Derived( cf );
2019-05-09 21:41:54 +02:00
}
};
//! failsafe version to select the output plug-in
2019-07-31 11:57:40 +02:00
std::unique_ptr<output_plugin> select_output_plugin(ConfigFile &cf);
2019-05-09 21:41:54 +02:00