Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ Studio.zip
/unit_tests
/test_custom_DCs
/test_voxel_values

.vscode
344 changes: 271 additions & 73 deletions BioFVM/BioFVM_microenvironment.cpp

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion BioFVM/BioFVM_microenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ class Microenvironment
void set_substrate_dirichlet_activation( int index, std::vector<bool>& new_value );
bool get_substrate_dirichlet_activation( int substrate_index, int index );

// only change dirichlet_activation_vector, not the activation_vectors for each voxel
void set_only_substrate_dirichlet_activation( int substrate_index , bool new_value );

double get_substrate_dirichlet_value( int substrate_index, int index );

bool& is_dirichlet_node( int voxel_index );
Expand Down Expand Up @@ -341,9 +344,13 @@ class Microenvironment_Options

std::vector<double> initial_condition_vector;

bool initial_condition_from_file_enabled;
bool initial_condition_from_file_enabled = false;
std::string initial_condition_file_type;
std::string initial_condition_file;

bool dirichlet_condition_from_file_enabled = false;
std::string dirichlet_condition_file_type;
std::string dirichlet_condition_file;

bool simulate_2D;
std::vector<double> X_range;
Expand All @@ -368,6 +375,13 @@ void set_microenvironment_initial_condition( void );
void load_initial_conditions_from_matlab( std::string filename );
void load_initial_conditions_from_csv( std::string filename );
void get_row_from_substrate_initial_condition_csv(std::vector<int> &voxel_set, const std::string line, const std::vector<int> substrate_indices, const bool header_provided);

void set_dirichlet_initial_condition( void );
void set_dirichlet_boundaries_from_XML( void );
void set_dirichlet_boundaries_from_file( void );
void load_dirichlet_conditions_from_matlab( std::string filename );
void load_dirichlet_conditions_from_csv(std::string filename);
void get_row_from_dirichlet_condition_csv(std::vector<int> &voxel_set, const std::string line, const std::vector<int> substrate_indices, const bool header_provided, int n_cols);
};

#endif
84 changes: 84 additions & 0 deletions BioFVM/BioFVM_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
*/

#include "BioFVM_vector.h"
#include <stdexcept>
#include <string>

/* some global BioFVM strings */

Expand Down Expand Up @@ -376,6 +378,88 @@ void csv_to_vector( const char* buffer , std::vector<double>& vect )
return;
}

// store one field of a dirichlet-condition csv row. The bounds check matters: a row carrying more
// fields than the header established would otherwise write past the end of both vectors.
static void store_dirichlet_csv_entry( const char* buffer , const std::string& entry , std::vector<bool>& is_missing , std::vector<double>& data , const size_t ind )
{
if (ind >= is_missing.size())
{
std::cerr << "Error: Too many data supplied in a row of the .csv file specifying BioFVM dirichlet conditions." << std::endl;
std::cerr << "\tExpected: " << is_missing.size() << ". Found: " << ind + 1 << " or more." << std::endl;
std::cerr << "\tRow: " << buffer << std::endl;
exit(-1);
}

// a field holding nothing but whitespace is omitted, so that " , " reads the same as ","
size_t first = entry.find_first_not_of(" \t\r");
if (first == std::string::npos)
{
is_missing[ind] = true;
return;
}
std::string trimmed = entry.substr(first, entry.find_last_not_of(" \t\r") - first + 1);

double value = 0.0;
size_t parsed = 0;
try
{
value = std::stod(trimmed, &parsed);
}
catch (const std::logic_error&) // std::stod throws invalid_argument on no conversion, out_of_range on overflow
{
parsed = 0;
}
if (parsed != trimmed.size()) // no conversion, or trailing garbage as in "1.5abc"
{
std::cerr << "Error: Column " << ind + 1 << " of a row of the .csv file specifying BioFVM dirichlet conditions is not a number." << std::endl;
std::cerr << "\tOffending value: " << trimmed << std::endl;
std::cerr << "\tRow: " << buffer << std::endl;
exit(-1);
}

data[ind] = value;
is_missing[ind] = false;
}

void dirichlet_csv_to_vector( const char* buffer , std::vector<bool>& is_missing , std::vector<double>& data )
{
size_t ind = 0;
unsigned int i=0;
std::string entry;
while( i < strlen( buffer ) )
{
if(buffer[i] == ',')
{
Comment thread
drbergman marked this conversation as resolved.
store_dirichlet_csv_entry(buffer, entry, is_missing, data, ind);
entry.clear();
ind++;
}
else
{
entry += buffer[i];
}
i++;
}
// Handle the last entry
store_dirichlet_csv_entry(buffer, entry, is_missing, data, ind);

if (is_missing[0] || is_missing[1] || is_missing[2])
{
std::cerr << "Error: x, y, and z data must be provided for each row in the .csv file specifying BioFVM dirichlet conditions." << std::endl;
exit(-1);
}

if (ind < is_missing.size() - 1)
{
std::cerr << "Error: Wrong number of data supplied in a row of the .csv file specifying BioFVM dirichlet conditions." << std::endl;
std::cerr << "\tExpected: " << is_missing.size() << ". Found: " << ind + 1 << std::endl;
std::cerr << "\tRow: " << buffer << std::endl;
exit(-1);
}
return;
}


char* vector_to_csv( const std::vector<double>& vect )
{
static int datum_size = 16; // format = %.7e, 1 (sign) + 1 (lead) + 1 (decimal) + 7 (figs) + 2 (e, sign) + 3 (exponent) + 1 (delimiter) = 16
Expand Down
1 change: 1 addition & 0 deletions BioFVM/BioFVM_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void double_axpy_div( std::vector<double>* y, std::vector<double>& a1 , std::vec
// turn a delimited character array (e.g., csv) into a vector of doubles

void csv_to_vector( const char* buffer , std::vector<double>& vect );
void dirichlet_csv_to_vector( const char* buffer , std::vector<bool>& missings , std::vector<double>& values );
char* vector_to_csv( const std::vector<double>& vect );
void vector_to_csv_safe( const std::vector<double>& vect , char*& buffer );
void vector_to_csv( const std::vector<double>& vect , char*& buffer );
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ template:
cp Makefile Makefile-backup
cp ./sample_projects/template/Makefile .
cp -r ./sample_projects/template/config/* ./config

# sample projects

# ---- non-intracellular projects
Expand Down Expand Up @@ -238,6 +238,14 @@ episode-sample:
cp ./sample_projects/episode/Makefile .
cp -r ./sample_projects/episode/config/* ./config

dirichlet-from-file-sample:
cp -r ./sample_projects/dirichlet_from_file/custom_modules/* ./custom_modules/
touch main.cpp && cp main.cpp main-backup.cpp
cp ./sample_projects/dirichlet_from_file/main.cpp ./main.cpp
cp Makefile Makefile-backup
cp ./sample_projects/dirichlet_from_file/Makefile .
cp -r ./sample_projects/dirichlet_from_file/config/* ./config

# ---- intracellular projects
ode-energy-sample:
cp ./sample_projects_intracellular/ode/ode_energy/custom_modules/* ./custom_modules/
Expand Down
15 changes: 15 additions & 0 deletions modules/PhysiCell_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,21 @@ bool setup_microenvironment_from_XML( pugi::xml_node root_node )
}
}

node = xml_find_node( root_node , "microenvironment_setup" );
node = xml_find_node( node , "options" );
node = xml_find_node(node, "dirichlet_nodes");
if (node)
{
default_microenvironment_options.dirichlet_condition_from_file_enabled = node.attribute("enabled").as_bool();
if (default_microenvironment_options.dirichlet_condition_from_file_enabled)
{
default_microenvironment_options.dirichlet_condition_file_type = node.attribute("type").as_string();
default_microenvironment_options.dirichlet_condition_file = xml_get_string_value(node, "filename");

copy_file_to_output(default_microenvironment_options.dirichlet_condition_file);
}
}

// not yet supported : read initial conditions
/*
// read in initial conditions from an external file
Expand Down
10 changes: 9 additions & 1 deletion sample_projects/Makefile-default
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ template:
cp Makefile Makefile-backup
cp ./sample_projects/template/Makefile .
cp -r ./sample_projects/template/config/* ./config

# sample projects

# ---- non-intracellular projects
Expand Down Expand Up @@ -238,6 +238,14 @@ episode-sample:
cp ./sample_projects/episode/Makefile .
cp -r ./sample_projects/episode/config/* ./config

dirichlet-from-file-sample:
cp -r ./sample_projects/dirichlet_from_file/custom_modules/* ./custom_modules/
touch main.cpp && cp main.cpp main-backup.cpp
cp ./sample_projects/dirichlet_from_file/main.cpp ./main.cpp
cp Makefile Makefile-backup
cp ./sample_projects/dirichlet_from_file/Makefile .
cp -r ./sample_projects/dirichlet_from_file/config/* ./config

# ---- intracellular projects
ode-energy-sample:
cp ./sample_projects_intracellular/ode/ode_energy/custom_modules/* ./custom_modules/
Expand Down
Loading
Loading