TransFlow  0.1.0
A transient pipeline flow simulation library
sampler.hpp
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <fstream>
6 #include <filesystem>
7 #include <armadillo>
8 
9 class Pipeline;
10 
29 typedef const arma::vec& (Pipeline::*PropertyGetter)() const;
30 
31 namespace details
32 {
33 
42 {
48  PropertyToSample(const std::string& label, PropertyGetter function):
49  label(label),
51  {}
52 
53  std::string label;
54  PropertyGetter function;
55 };
56 
57 }; // end namespace details
58 
69 class Sampler
70 {
71 public:
78  Sampler(
79  const std::filesystem::path& path,
80  const arma::uword interval = 60,
81  const bool append = false,
82  const arma::uvec indicesToSample = {});
83 
89  void addPropertyToPrint(PropertyGetter samplingFunction);
90 
96  void addPropertyToPrint(PropertyGetter samplingFunction, const std::string& label);
97 
105  bool sample(const Pipeline& pipeline, const bool forceSample = false);
106 
108  const std::filesystem::path& outputDir() const { return m_outputDir; }
109 
112  static std::string getSampleLabel(PropertyGetter samplingFunction);
113 
117  static std::filesystem::path makeOutputDir(const std::filesystem::path& path);
118 
121  Sampler& setIndicesToSample(const arma::uvec& indices);
122 
123 private:
126  std::vector<std::ofstream> m_outputFiles;
129  std::vector<details::PropertyToSample> m_samplers;
131  const std::filesystem::path m_outputDir;
132 
134  arma::uword m_printInterval = 60;
136  arma::uword m_timeOfLastPrint = 0;
139  arma::uvec m_indicesToSample {};
140 
142  bool m_append;
143 };
Sampler::m_outputDir
const std::filesystem::path m_outputDir
Output directory.
Definition: sampler.hpp:131
Sampler::m_outputFiles
std::vector< std::ofstream > m_outputFiles
Definition: sampler.hpp:126
Sampler::m_indicesToSample
arma::uvec m_indicesToSample
Definition: sampler.hpp:139
Sampler::m_samplers
std::vector< details::PropertyToSample > m_samplers
Definition: sampler.hpp:129
details::PropertyToSample::PropertyToSample
PropertyToSample(const std::string &label, PropertyGetter function)
Construct from label and a Pipeline member function pointer.
Definition: sampler.hpp:48
Sampler::m_printInterval
arma::uword m_printInterval
How often to print (max) [s].
Definition: sampler.hpp:134
PropertyGetter
const typedef arma::vec &(Pipeline::* PropertyGetter)() const
Definition: sampler.hpp:29
Pipeline
Definition: pipeline.hpp:16
Sampler::m_append
bool m_append
If we append to (true) or overwrite (false) existing files.
Definition: sampler.hpp:142
Sampler::sample
bool sample(const Pipeline &pipeline, const bool forceSample=false)
Sample current state. This is usually only called by Simulator, but can also be called to force a sam...
Sampler::Sampler
Sampler(const std::filesystem::path &path, const arma::uword interval=60, const bool append=false, const arma::uvec indicesToSample={})
Construct given output directory and (optional) print interval.
details::PropertyToSample::function
PropertyGetter function
Pipeline member function (getter) pointer.
Definition: sampler.hpp:54
details::PropertyToSample::label
std::string label
Label.
Definition: sampler.hpp:53
Sampler::outputDir
const std::filesystem::path & outputDir() const
Get (const ref) output directory.
Definition: sampler.hpp:108
Sampler::m_timeOfLastPrint
arma::uword m_timeOfLastPrint
Time of last print [s].
Definition: sampler.hpp:136
Sampler::addPropertyToPrint
void addPropertyToPrint(PropertyGetter samplingFunction)
Add property to save to file. This automatically determines the label and filename of the property.
details::PropertyToSample
The PropertyToSample struct contains information about a single property from Pipeline that we are go...
Definition: sampler.hpp:41
Sampler::setIndicesToSample
Sampler & setIndicesToSample(const arma::uvec &indices)
Sampler
The Sampler class is used to sample selected Pipeline properties during simulations.
Definition: sampler.hpp:69
Sampler::getSampleLabel
static std::string getSampleLabel(PropertyGetter samplingFunction)
Sampler::makeOutputDir
static std::filesystem::path makeOutputDir(const std::filesystem::path &path)
Pipeline::Pipeline
Pipeline(const arma::uword nGridPoints=100, const double length=100e3)
Construct from number of grid points and pipeline length.