Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function input feature for time-dependent boundary conditions #108

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
4df6072
expression_string (SEGFAULT)
kharold23 Apr 3, 2024
7fad1f0
Time dependent flow (fixed)
kharold23 Apr 13, 2024
fe12497
Time-dependent pressure
kharold23 Apr 21, 2024
6460bf8
Add test cases and clean/format code
kharold23 May 9, 2024
8258e4e
Documentation
kharold23 May 16, 2024
ad3b585
Clang Format Fix?
kharold23 May 16, 2024
7acb97c
Clang-format fix?
kharold23 May 16, 2024
a3411ba
Update Parameter.cpp
kharold23 May 17, 2024
46e733c
Merge branch 'SimVascular:master' into time-dependent
kharold23 Jun 11, 2024
8b73387
Create "ThirdParty" directory and add expression compiling check
kharold23 Jun 11, 2024
b32058a
exprtk improvements suggested by A. Partow
kharold23 Jun 17, 2024
a328460
Update handling for when function parameter is not given
kharold23 Jun 17, 2024
fa4c9fe
Fix symbol_table bug
kharold23 Jun 18, 2024
cace21c
Expression string length bug
kharold23 Jun 18, 2024
baa42d2
clang formatting
kharold23 Jun 18, 2024
e6760c6
Clang formatting (again)
kharold23 Jun 18, 2024
8a4c875
Requested changes
kharold23 Jul 12, 2024
927d5cd
Add results of the time dependent test cases
kharold23 Jul 12, 2024
2741f35
expression_string (SEGFAULT)
kharold23 Apr 3, 2024
772548b
Time dependent flow (fixed)
kharold23 Apr 13, 2024
10e293f
Time-dependent pressure
kharold23 Apr 21, 2024
25070a1
Add test cases and clean/format code
kharold23 May 9, 2024
356674c
Documentation
kharold23 May 16, 2024
aec0dd2
Clang Format Fix?
kharold23 May 16, 2024
29e4a4a
Clang-format fix?
kharold23 May 16, 2024
24911c8
Update Parameter.cpp
kharold23 May 17, 2024
40d2fb7
Create "ThirdParty" directory and add expression compiling check
kharold23 Jun 11, 2024
62cfac4
exprtk improvements suggested by A. Partow
kharold23 Jun 17, 2024
57dc47d
Update handling for when function parameter is not given
kharold23 Jun 17, 2024
3f85d4a
Fix symbol_table bug
kharold23 Jun 18, 2024
2de2ace
Expression string length bug
kharold23 Jun 18, 2024
006d604
clang formatting
kharold23 Jun 18, 2024
02dd225
Clang formatting (again)
kharold23 Jun 18, 2024
7313593
Requested changes
kharold23 Jul 12, 2024
6ca5d97
Add results of the time dependent test cases
kharold23 Jul 12, 2024
ac20185
Merge branch 'SimVascular-master' into time-dependent
kharold23 Jul 13, 2024
62d8fd5
Update exprtk time variable handling
kharold23 Jul 22, 2024
37a8021
Update test_solver.py
kharold23 Jul 25, 2024
12ebb96
Add constants pi, e, and inf
kharold23 Jul 30, 2024
267e419
Clang Formatting
kharold23 Jul 24, 2024
4a65796
Update block generation
kharold23 Jul 31, 2024
7aa16de
Enable Sanitizers for Debugging
kharold23 Jul 31, 2024
c1f900b
Add macos-latest test
kharold23 Jul 31, 2024
11f8692
Update test_macos.yml
kharold23 Jul 31, 2024
808bd97
Update test_macos.yml
kharold23 Jul 31, 2024
9bd73b3
Merge pull request #5 from SimVascular/master
kharold23 Oct 30, 2024
9cee0b6
Clang Formatting and Graphs for New Tests
kharold23 Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/model/FlowReferenceBC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ void FlowReferenceBC::setup_dofs(DOFHandler &dofhandler) {

void FlowReferenceBC::update_constant(SparseSystem &system,
std::vector<double> &parameters) {
//std::cout << system << std::endl;
for (double i: parameters){
std::cout << i << std::endl;
}
std::cout << global_eqn_ids[0] << std::endl;
std::cout << global_var_ids[1] << std::endl;
system.F.coeffRef(global_eqn_ids[0], global_var_ids[1]) = 1.0;
}

Expand Down
5 changes: 3 additions & 2 deletions src/model/FlowReferenceBC.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ class FlowReferenceBC : public Block {
*/
FlowReferenceBC(int id, Model *model)
: Block(id, model, BlockType::flow_bc, BlockClass::boundary_condition,
{{"t", InputParameter(false, true)},
{"Q", InputParameter(false, true)}}) {}
{{"t", InputParameter(true, true)},
{"Q", InputParameter(true, true)},
{"fn", InputParameter(true, false, false, true)}}) {}

/**
* @brief Set up the degrees of freedom (DOF) of the block
Expand Down
7 changes: 7 additions & 0 deletions src/model/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ int Model::add_parameter(const std::vector<double> &times,
return parameter_count++;
}

int Model::add_parameter(const std::string expression_string) {
auto param = Parameter(parameter_count, expression_string);
parameter_values.push_back(param.get(0.0));
parameters.push_back(std::move(param));
return parameter_count++;
}

Parameter *Model::get_parameter(int param_id) { return &parameters[param_id]; }

double Model::get_parameter_value(int param_id) const {
Expand Down
8 changes: 8 additions & 0 deletions src/model/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ class Model {
int add_parameter(const std::vector<double> &times,
const std::vector<double> &values, bool periodic = true);

/**
* @brief Add a time-dependent model parameter defined by a function
*
* @param expression_string String with function to determine value
* @return int Global ID of the parameter
*/
int add_parameter(const std::string expression_string);

/**
* @brief Get a parameter by its global ID
*
Expand Down
39 changes: 39 additions & 0 deletions src/model/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Parameter.h"
#include <cstdio>
#include <string>

#include "exprtk.hpp"

Parameter::Parameter(int id, double value) {
this->id = id;
Expand All @@ -42,6 +46,12 @@ Parameter::Parameter(int id, const std::vector<double> &times,
update(times, values);
}

Parameter::Parameter(int id, const std::string expression_string) {
this->id = id;
this->expression_string = expression_string;
update(expression_string);
}

void Parameter::update(double update_value) {
is_constant = true;
is_periodic = true;
Expand All @@ -63,6 +73,12 @@ void Parameter::update(const std::vector<double> &update_times,
}
}


void Parameter::update(const std::string update_string) {
is_function = true;
expression_string = update_string;
}

double Parameter::get(double time) {
// Return the constant value if parameter is constant
if (is_constant) {
Expand All @@ -79,6 +95,29 @@ double Parameter::get(double time) {
rtime = time;
}

if (is_function == true) {
// Adapted from example from Basic Design example at http://www.partow.net/programming/exprtk/index.html
typedef double T;

typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;

T t = T(time);

symbol_table_t symbol_table;
symbol_table.add_variable("t",t);

expression_t expression;
expression.register_symbol_table(symbol_table);

parser_t parser;

parser.compile(expression_string, expression);
kharold23 marked this conversation as resolved.
Show resolved Hide resolved
T value = expression.value();
return value;
}

// Determine the lower and upper element for interpolation
auto i = lower_bound(times.begin(), times.end(), rtime);
int k = i - times.begin();
Expand Down
26 changes: 25 additions & 1 deletion src/model/Parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <iostream>
#include <numeric>
#include <vector>
#include <cstdio>
#include <string>

#include "exprtk.hpp"

#include "DOFHandler.h"

Expand Down Expand Up @@ -70,6 +74,13 @@ class Parameter {
Parameter(int id, const std::vector<double>& times,
const std::vector<double>& values, bool periodic = true);

/**
* @brief Construct a new Parameter object
*
* @param id Global ID of the parameter
* @param expression_string string with time-dependent function to get values
*/
Parameter(int id, const std::string expression_string);
int id; ///< Global ID of the parameter
std::vector<double> times; ///< Time steps if parameter is time-dependent
std::vector<double> values; ///< Values if parameter is time-dependent
Expand All @@ -80,6 +91,9 @@ class Parameter {
bool is_constant; ///< Bool value indicating if the parameter is constant
bool is_periodic; ///< Bool value indicating if the parameter is periodic
///< with the cardiac cycle
bool is_function; ///< Bool value indicating if the parameter is a function
std::string expression_string; ///< String with value function


/**
* @brief Update the parameter
Expand All @@ -97,6 +111,14 @@ class Parameter {
void update(const std::vector<double>& times,
const std::vector<double>& values);

/**
* @brief Update the parameter
*
* @param expression_string String with function
* @param update_value Value at time step
*/
void update(const std::string update_string);

/**
* @brief Get the parameter value at the specified time.
*
Expand Down Expand Up @@ -128,6 +150,7 @@ struct InputParameter {
bool is_optional; ///< Is this parameter optional?
bool is_array; ///< Is this parameter an array?
bool is_number; ///< Is this parameter a number?
bool is_function; ///< Is this parameter a time-dependent function?
double default_val; ///< Default value (if parameter is optional)

/**
Expand All @@ -139,10 +162,11 @@ struct InputParameter {
* @param default_val Default value (if parameter is optional)
*/
InputParameter(bool is_optional = false, bool is_array = false,
bool is_number = true, double default_val = 0.0)
bool is_number = true, bool is_function = false, double default_val = 0.0)
: is_optional(is_optional),
is_array(is_array),
is_number(is_number),
is_function(is_function),
default_val(default_val) {}
};

Expand Down
Loading