TDMS
Time Domain Maxwell Solver
Toggle main menu visibility
Loading...
Searching...
No Matches
input_flags.h
Go to the documentation of this file.
1
/**
2
* @file input_flags.h
3
* @author William Graham (ccaegra@ucl.ac.uk)
4
* @brief Organises enumerated constants, names, and classes for handling
5
* flag-variables that are passed to TDMS in the input file. Such variables do
6
* not contain numerical data for use in the simulation, but affect the manner
7
* in which the simulation is performed.
8
*/
9
#pragma once
10
11
#include <algorithm>
12
#include <stdexcept>
13
#include <string>
14
#include <vector>
15
16
#include <spdlog/spdlog.h>
17
18
#include "
mat_io.h
"
19
20
//! The maximum number of flags that could be present in the input file
21
#define NFLAGS 2
22
23
//! Namespace encompassing variables and enums related to flag values to be read
24
//! from the input file. Enums classes are set so that the value corresponding
25
//! to "true" is the corresponding option that is used when that flag is passed
26
//! in the input file.
27
namespace
tdms_flags
{
28
//! Lists the flag-variables that can be present in the input file, but are not
29
//! required to be present
30
const
std::vector<std::string>
flag_variables
= {
"use_pstd"
,
"use_bli"
};
31
32
//! The timestepping method to be used to forward-propagate the simulation.
33
//! use_pstd is the corresponding flag.
34
enum
SolverMethod
:
bool
{ PseudoSpectral =
true
, FiniteDifference =
false
};
35
//! The interpolation method to use when extracting field values at Yee
36
//! cell centres. use_bli is the corresponding flag.
37
enum
InterpolationMethod
:
bool
{ BandLimited =
true
, Cubic =
false
};
38
}
// namespace tdms_flags
39
40
class
InputFlags {
41
private
:
42
//! Name of the input file to read flags from
43
std::string
input_filename
=
"input_file.mat"
;
44
//! Flag values present in the input file
45
bool
flag_values
[
NFLAGS
] = {
false
};
46
47
/**
48
* @brief Returns the internal index in flag_values of the flag with the name
49
* provided.
50
*
51
* @param flag_name Name of the flag
52
* @return const int Index of this flag's value in flag_values
53
*/
54
const
int
position_from_name
(
const
std::string &flag_name)
const
{
55
auto
position = std::find(
tdms_flags::flag_variables
.begin(),
56
tdms_flags::flag_variables
.end(), flag_name);
57
if
(position ==
tdms_flags::flag_variables
.end()) {
58
// Could not find the flag in the list of expected flags, return an error
59
throw
std::runtime_error(flag_name +
" is not an expected flag"
);
60
}
61
return
std::distance(
tdms_flags::flag_variables
.begin(), position);
62
}
63
64
/**
65
* @brief Fetch the value of the flag variable from the input file.
66
*
67
* @param flag_name The name of the flag to fetch
68
* @param fail_on_not_found If true, throw an error if the flag cannot be
69
* located. Otherwise, return the default flag value.
70
* @return true This flag was present and set to true
71
* @return false This flag was present and set to false, OR was not present
72
*/
73
bool
fetch_flag_value
(
const
std::string flag_name,
74
bool
fail_on_not_found =
false
)
const
;
75
76
public
:
77
InputFlags
() =
default
;
78
/**
79
* @brief Construct a new InputFlags object by reading flags from the input
80
* file provided.
81
*
82
* The user may optionally specify that an error should be thrown if flags
83
* cannot be found through the fail_on_not_found argument.
84
*
85
* @param _input_filename The input file to read from
86
* @param fail_on_not_found If true, any flags that cannot be located in the
87
* input file will cause an error to be thrown.
88
*/
89
InputFlags
(
const
std::string &_input_filename,
bool
fail_on_not_found =
false
)
90
:
input_filename
(_input_filename) {
91
for
(
const
std::string &flag_name :
tdms_flags::flag_variables
) {
92
flag_values
[
position_from_name
(flag_name)] =
93
fetch_flag_value
(flag_name, fail_on_not_found);
94
}
95
};
96
97
/**
98
* @brief Return the value of the flag corresponding to the name provided.
99
*/
100
bool
operator[]
(
const
std::string &flag_name)
const
{
101
return
flag_values
[
position_from_name
(flag_name)];
102
}
103
104
/** @brief Prompts spdlog to report the value of every expected flag in the
105
* input file */
106
void
report_flag_state
() {
107
for
(
const
std::string &flag :
tdms_flags::flag_variables
) {
108
spdlog::info(
"Read "
+ flag +
": {}"
,
109
flag_values
[
position_from_name
(flag)]);
110
}
111
}
112
};
InputFlags
Definition
input_flags.h:40
InputFlags::InputFlags
InputFlags(const std::string &_input_filename, bool fail_on_not_found=false)
Construct a new InputFlags object by reading flags from the input file provided.
Definition
input_flags.h:89
InputFlags::position_from_name
const int position_from_name(const std::string &flag_name) const
Returns the internal index in flag_values of the flag with the name provided.
Definition
input_flags.h:54
InputFlags::fetch_flag_value
bool fetch_flag_value(const std::string flag_name, bool fail_on_not_found=false) const
Fetch the value of the flag variable from the input file.
Definition
input_flags.cpp:3
InputFlags::flag_values
bool flag_values[NFLAGS]
Flag values present in the input file.
Definition
input_flags.h:45
InputFlags::report_flag_state
void report_flag_state()
Prompts spdlog to report the value of every expected flag in the input file.
Definition
input_flags.h:106
InputFlags::operator[]
bool operator[](const std::string &flag_name) const
Return the value of the flag corresponding to the name provided.
Definition
input_flags.h:100
InputFlags::input_filename
std::string input_filename
Name of the input file to read flags from.
Definition
input_flags.h:43
NFLAGS
#define NFLAGS
The maximum number of flags that could be present in the input file.
Definition
input_flags.h:21
mat_io.h
Includes MATLAB headers for I/O.
tdms_flags
Definition
input_flags.h:27
tdms_flags::flag_variables
const std::vector< std::string > flag_variables
Definition
input_flags.h:30
tdms_flags::InterpolationMethod
InterpolationMethod
Definition
input_flags.h:37
tdms_flags::SolverMethod
SolverMethod
Definition
input_flags.h:34
tdms
include
input_flags.h
Generated by
1.17.0