Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 2ffa262

Browse files
authored
Add corenrn_parameters::reset() method. (#720)
1 parent 966533d commit 2ffa262

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

coreneuron/apps/corenrn_parameters.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,12 @@ corenrn_parameters::corenrn_parameters() {
160160
app.add_flag("-v, --version", this->show_version, "Show version information and quit.");
161161

162162
CLI::retire_option(app, "--show");
163-
};
163+
}
164+
165+
void corenrn_parameters::reset() {
166+
static_cast<corenrn_parameters_data&>(*this) = corenrn_parameters_data{};
167+
app.clear();
168+
}
164169

165170
void corenrn_parameters::parse(int argc, char** argv) {
166171
try {

coreneuron/apps/corenrn_parameters.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535

3636
namespace coreneuron {
3737

38-
struct corenrn_parameters {
38+
struct corenrn_parameters_data {
3939
enum verbose_level : std::uint32_t { NONE = 0, ERROR = 1, INFO = 2, DEBUG = 3, DEFAULT = INFO };
4040

41-
const int report_buff_size_default = 4;
41+
static constexpr int report_buff_size_default = 4;
4242

4343
unsigned spikebuf = 100'000; /// Internal buffer used on every rank for spikes
4444
int prcellgid = -1; /// Gid of cell for prcellstate
@@ -86,14 +86,23 @@ struct corenrn_parameters {
8686
std::string checkpointpath; /// Enable checkpoint and specify directory to store related files.
8787
std::string writeParametersFilepath; /// Write parameters to this file
8888
std::string mpi_lib; /// Name of CoreNEURON MPI library to load dynamically.
89+
};
8990

91+
struct corenrn_parameters: corenrn_parameters_data {
9092
CLI::App app{"CoreNeuron - Optimised Simulator Engine for NEURON."}; /// CLI app that performs
9193
/// CLI parsing
9294

9395
corenrn_parameters(); /// Constructor that initializes the CLI11 app.
9496

9597
void parse(int argc, char* argv[]); /// Runs the CLI11_PARSE macro.
9698

99+
/** @brief Reset all parameters to their default values.
100+
*
101+
* Unfortunately it is awkward to support `x = corenrn_parameters{}`
102+
* because `app` holds pointers to members of `corenrn_parameters`.
103+
*/
104+
void reset();
105+
97106
inline bool is_quiet() {
98107
return verbose == verbose_level::NONE;
99108
}

coreneuron/apps/main1.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ static void* load_dynamic_mpi(const std::string& libname) {
467467
#endif
468468

469469
extern "C" void mk_mech_init(int argc, char** argv) {
470+
// reset all parameters to their default values
471+
corenrn_param.reset();
470472
// read command line parameters and parameter config files
471473
corenrn_param.parse(argc, argv);
472474

tests/unit/cmdline_interface/test_cmdline_interface.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
# See top-level LICENSE file for details.
66
# =============================================================================.
77
*/
8+
#include "coreneuron/apps/corenrn_parameters.hpp"
89

910
#define BOOST_TEST_MODULE cmdline_interface
1011
#define BOOST_TEST_MAIN
1112

1213
#include <boost/test/unit_test.hpp>
14+
1315
#include <cfloat>
14-
#include "coreneuron/apps/corenrn_parameters.hpp"
1516

1617
using namespace coreneuron;
1718

@@ -74,13 +75,7 @@ BOOST_AUTO_TEST_CASE(cmdline_interface) {
7475

7576
"--dt_io",
7677
"0.2"};
77-
78-
int argc = 0;
79-
80-
for (; strcmp(argv[argc], "0.2"); argc++)
81-
;
82-
83-
argc++;
78+
constexpr int argc = sizeof argv / sizeof argv[0];
8479

8580
corenrn_parameters corenrn_param_test;
8681

@@ -126,4 +121,14 @@ BOOST_AUTO_TEST_CASE(cmdline_interface) {
126121
BOOST_CHECK(corenrn_param_test.spkcompress == 32);
127122

128123
BOOST_CHECK(corenrn_param_test.multisend == true);
124+
125+
// Reset all parameters to their default values.
126+
corenrn_param_test.reset();
127+
128+
// Should match a default-constructed set of parameters.
129+
BOOST_CHECK_EQUAL(corenrn_param_test.voltage, corenrn_parameters{}.voltage);
130+
131+
// Everything has its default value, and the first `false` says not to
132+
// include default values in the output, so this should be empty
133+
BOOST_CHECK(corenrn_param_test.app.config_to_str(false, false).empty());
129134
}

0 commit comments

Comments
 (0)