Skip to content

Commit 7b93662

Browse files
committed
formalize calibration parameters
1 parent ce47de2 commit 7b93662

12 files changed

Lines changed: 50 additions & 46 deletions

src/interface/interface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <cmath>
77

8-
#include "SimulationParameters.h"
8+
#include "Parameters.h"
99

1010
// Static member data.
1111
int SolverInterface::problem_id_count_ = 0;

src/optimize/LevenbergMarquardtOptimizer.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
#include <iomanip>
66

77
LevenbergMarquardtOptimizer::LevenbergMarquardtOptimizer(
8-
Model* model, int num_obs, int num_params, double lambda0, double tol_grad,
9-
double tol_inc, int max_iter) {
8+
Model* model, int num_obs, int num_params, CalibrationParameters cali_params) {
109
this->model = model;
1110
this->num_obs = num_obs;
1211
this->num_params = num_params;
1312
this->num_eqns = model->dofhandler.get_num_equations();
1413
this->num_vars = model->dofhandler.get_num_variables();
1514
this->num_dpoints = this->num_obs * this->num_eqns;
16-
this->lambda = lambda0;
17-
this->tol_grad = tol_grad;
18-
this->tol_inc = tol_inc;
19-
this->max_iter = max_iter;
15+
this->lambda = cali_params.lambda0;
16+
this->tol_grad = cali_params.tolerance_gradient;
17+
this->tol_inc = cali_params.tolerance_increment;
18+
this->max_iter = cali_params.maximum_iterations;
2019

2120
jacobian = Eigen::SparseMatrix<double>(num_dpoints, num_params);
2221
residual = Eigen::Matrix<double, Eigen::Dynamic, 1>::Zero(num_dpoints);

src/optimize/LevenbergMarquardtOptimizer.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <Eigen/Sparse>
1212

1313
#include "Model.h"
14+
#include "Parameters.h"
1415

1516
/**
1617
* @brief Levenberg-Marquardt optimization class
@@ -81,14 +82,10 @@ class LevenbergMarquardtOptimizer {
8182
* @param model The 0D model
8283
* @param num_obs Number of observations in optimization
8384
* @param num_params Number of parameters in optimization
84-
* @param lambda0 Initial damping factor
85-
* @param tol_grad Gradient tolerance
86-
* @param tol_inc Parameter increment tolerance
87-
* @param max_iter Maximum iterations
85+
* @param cali_params Calibration parameters
8886
*/
8987
LevenbergMarquardtOptimizer(Model* model, int num_obs, int num_params,
90-
double lambda0, double tol_grad, double tol_inc,
91-
int max_iter);
88+
CalibrationParameters cali_params);
9289

9390
/**
9491
* @brief Run the optimization algorithm

src/optimize/calibrate.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33
#include "calibrate.h"
44

55
#include "LevenbergMarquardtOptimizer.h"
6-
#include "SimulationParameters.h"
6+
#include "Parameters.h"
77

88
nlohmann::json calibrate(const nlohmann::json& config) {
99
auto output_config = nlohmann::json(config);
1010

1111
// Read calibration parameters
12-
DEBUG_MSG("Parse calibration parameters");
13-
auto const& calibration_parameters = config["calibration_parameters"];
14-
double gradient_tol =
15-
calibration_parameters.value("tolerance_gradient", 1e-5);
16-
double increment_tol =
17-
calibration_parameters.value("tolerance_increment", 1e-10);
18-
int max_iter = calibration_parameters.value("maximum_iterations", 100);
19-
bool zero_capacitance =
20-
calibration_parameters.value("set_capacitance_to_zero", false);
21-
double lambda0 = calibration_parameters.value("initial_damping_factor", 1.0);
12+
CalibrationParameters cali_params = load_calibration_params(config);
2213

2314
// Setup model
2415
auto model = Model();
@@ -138,8 +129,7 @@ nlohmann::json calibrate(const nlohmann::json& config) {
138129
// Run optimization
139130
DEBUG_MSG("Start optimization");
140131
auto lm_alg =
141-
LevenbergMarquardtOptimizer(&model, num_obs, param_counter, lambda0,
142-
gradient_tol, increment_tol, max_iter);
132+
LevenbergMarquardtOptimizer(&model, num_obs, param_counter, cali_params);
143133

144134
alpha = lm_alg.run(alpha, y_all, dy_all);
145135

@@ -153,12 +143,9 @@ nlohmann::json calibrate(const nlohmann::json& config) {
153143
for (auto& vessel_config : output_config["vessels"]) {
154144
std::string vessel_name = vessel_config["vessel_name"];
155145
auto block = model.get_block(vessel_name);
156-
double stenosis_coeff = 0.0;
157-
stenosis_coeff = alpha[block->global_param_ids[3]];
158-
double c_value = 0.0;
159-
if (!zero_capacitance) {
160-
c_value = alpha[block->global_param_ids[1]];
161-
}
146+
double stenosis_coeff = alpha[block->global_param_ids[3]];
147+
double c_value = alpha[block->global_param_ids[1]];
148+
162149
vessel_config["zero_d_element_values"] = {
163150
{"R_poiseuille", alpha[block->global_param_ids[0]]},
164151
{"C", std::max(c_value, 0.0)},

src/solve/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ set(lib svzero_solve_library)
77

88
set(CXXSRCS
99
csv_writer.cpp
10-
SimulationParameters.cpp
10+
Parameters.cpp
1111
Solver.cpp
1212
)
1313

1414
set(HDRS
1515
csv_writer.h
1616
debug.h
17-
SimulationParameters.h
17+
Parameters.h
1818
Solver.h
1919
)
2020

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
22
// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
3-
#include "SimulationParameters.h"
3+
#include "Parameters.h"
44

55
bool get_param_scalar(const nlohmann::json& data, const std::string& name,
66
const InputParameter& param, double& val) {
@@ -260,6 +260,16 @@ void load_simulation_model(const nlohmann::json& config, Model& model) {
260260
model.finalize();
261261
}
262262

263+
CalibrationParameters load_calibration_params(const nlohmann::json& config) {
264+
DEBUG_MSG("Loading calibration parameters");
265+
CalibrationParameters calib_params;
266+
calib_params.tolerance_gradient = config.value("tolerance_gradient", 1e-5);
267+
calib_params.tolerance_increment = config.value("tolerance_increment", 1e-10);
268+
calib_params.maximum_iterations = config.value("maximum_iterations", 100);
269+
calib_params.lambda0 = config.value("lambda0", 1.0);
270+
return calib_params;
271+
}
272+
263273
void load_calibration_model(const nlohmann::json& config, Model& model) {
264274
std::vector<std::tuple<std::string, std::string>> connections;
265275

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
22
// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
33
/**
4-
* @file SimulationParameters.h
4+
* @file Parameters.h
55
* @brief Source file to read simulation configuration
66
*/
77
#ifndef SVZERODSOLVER_SIMULATIONPARAMETERS_HPP_
@@ -54,6 +54,13 @@ struct SimulationParameters {
5454
///< running coupled
5555
};
5656

57+
struct CalibrationParameters {
58+
double tolerance_gradient{0.0}; ///< Tolerance for gradient
59+
double tolerance_increment{0.0}; ///< Tolerance for increment
60+
int maximum_iterations{0}; ///< Maximum number of iterations
61+
double lambda0{0.0}; ///< Initial damping factor
62+
};
63+
5764
/// @brief Wrapper class for nlohmann:json with error checking
5865
class JsonWrapper : public nlohmann::json {
5966
public:
@@ -152,9 +159,17 @@ SimulationParameters load_simulation_params(const nlohmann::json& config);
152159
*/
153160
void load_simulation_model(const nlohmann::json& config, Model& model);
154161

162+
/**
163+
* @brief Load the calibration parameters from a JSON configuration
164+
*
165+
* @param config The JSON configuration
166+
* @return SimulationParameters Simulation parameters read from configuration
167+
*/
168+
CalibrationParameters load_calibration_params(const nlohmann::json& config);
169+
155170
/**
156171
* @brief Load the 0D block in the model from a configuration for an inverse
157-
* problen
172+
* problem
158173
*
159174
* @param config The json configuration
160175
* @param model The 0D model
@@ -238,7 +253,7 @@ void create_junctions(
238253
*
239254
* @param model The model the block is associated with
240255
* @param connections Vector storing the connections between blocks
241-
*/
256+
*/
242257
void create_connections(
243258
Model& model,
244259
std::vector<std::tuple<std::string, std::string>>& connections);

src/solve/Solver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "Integrator.h"
99
#include "Model.h"
10-
#include "SimulationParameters.h"
10+
#include "Parameters.h"
1111
#include "State.h"
1212
#include "debug.h"
1313

tests/cases/steadyFlow_calibration.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,6 @@
873873
"tolerance_gradient": 1e-5,
874874
"tolerance_increment": 1e-10,
875875
"maximum_iterations": 100,
876-
"set_capacitance_to_zero": false,
877876
"initial_damping_factor": 1.0
878877
}
879878
}

tests/cases/vmr/input/0080_0001_calibrate_from_0d.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5146,8 +5146,7 @@
51465146
"calibration_parameters": {
51475147
"tolerance_gradient": 1e-05,
51485148
"tolerance_increment": 1e-09,
5149-
"maximum_iterations": 20,
5150-
"set_capacitance_to_zero": false
5149+
"maximum_iterations": 20
51515150
},
51525151
"y": {
51535152
"flow:branch0_seg0:J0": [

0 commit comments

Comments
 (0)