From 5ac68be879e89da59208849c73a17c6dc5f5e8f8 Mon Sep 17 00:00:00 2001 From: bue Date: Sat, 21 Sep 2024 00:49:49 -0400 Subject: [PATCH 1/7] add_parameter functions throw now an error if the parameter already exists. additionally, update_parameter functions were implemented and an update_variables argument with default setting false was added to the load_PhysiCell_config_file, parameters.read_from_pugixml, and setup_microenvironment_from_XML functions. --- BioFVM/BioFVM_microenvironment.cpp | 144 ++++++++++++++++++----------- BioFVM/BioFVM_microenvironment.h | 18 ++-- modules/PhysiCell_settings.cpp | 114 +++++++++++++++++++---- modules/PhysiCell_settings.h | 63 +++++++------ 4 files changed, 228 insertions(+), 111 deletions(-) diff --git a/BioFVM/BioFVM_microenvironment.cpp b/BioFVM/BioFVM_microenvironment.cpp index 47e3d5621..3ff10180b 100644 --- a/BioFVM/BioFVM_microenvironment.cpp +++ b/BioFVM/BioFVM_microenvironment.cpp @@ -253,7 +253,7 @@ bool Microenvironment::get_substrate_dirichlet_activation( int substrate_index ) // TODO? fix confusing swapped usage of args double Microenvironment::get_substrate_dirichlet_value( int substrate_index, int index ) { - return dirichlet_value_vectors[index][substrate_index]; + return dirichlet_value_vectors[index][substrate_index]; } // new functions for finer-grained control of Dirichlet conditions -- 1.7.0 @@ -487,76 +487,112 @@ void Microenvironment::add_density( std::string name , std::string units ) void Microenvironment::add_density( std::string name , std::string units, double diffusion_constant, double decay_rate ) { - // fix in PhysiCell preview November 2017 - if ( find_density_index( name ) != -1 ) + // fix in PhysiCell preview November 2017 + // default_microenvironment_options.use_oxygen_as_first_field = false; + + // check if density exist + if (find_density_index( name ) != -1) { - std::cout << "ERROR: density named " << name << " already exists. You probably want your substrates all have unique names!" << std::endl; - exit(-1); + std::cout << "Error: density named " << name << " already exists!" << std::endl; + exit(-1); } - - // update 1, 0 - zero.push_back( 0.0 ); + + // update 1, 0 + zero.push_back( 0.0 ); one.push_back( 1.0 ); - + // update units - density_names.push_back( name ); - density_units.push_back( units ); + density_names.push_back( name ); + density_units.push_back( units ); - // update coefficients - diffusion_coefficients.push_back( diffusion_constant ); - decay_rates.push_back( decay_rate ); - - // update sources and such + // update coefficients + diffusion_coefficients.push_back( diffusion_constant ); + decay_rates.push_back( decay_rate ); + + // update sources and such for( unsigned int i=0; i < temporary_density_vectors1.size() ; i++ ) { - temporary_density_vectors1[i].push_back( 0.0 ); - temporary_density_vectors2[i].push_back( 0.0 ); + temporary_density_vectors1[i].push_back( 0.0 ); + temporary_density_vectors2[i].push_back( 0.0 ); } - // resize the gradient data structures + // resize the gradient data structures for( unsigned int k=0 ; k < mesh.voxels.size() ; k++ ) { - gradient_vectors[k].resize( number_of_densities() ); + gradient_vectors[k].resize( number_of_densities() ); for( unsigned int i=0 ; i < number_of_densities() ; i++ ) { (gradient_vectors[k])[i].resize( 3, 0.0 ); } } - gradient_vector_computed.resize( mesh.voxels.size() , false ); + gradient_vector_computed.resize( mesh.voxels.size() , false ); - one_half = one; - one_half *= 0.5; - - one_third = one; - one_third /= 3.0; - - dirichlet_value_vectors.assign( mesh.voxels.size(), one ); - dirichlet_activation_vector.push_back( false ); - dirichlet_activation_vectors.assign( mesh.voxels.size(), dirichlet_activation_vector ); - - // fix in PhysiCell preview November 2017 - default_microenvironment_options.Dirichlet_condition_vector.push_back( 1.0 ); // = one; - default_microenvironment_options.Dirichlet_activation_vector.push_back( false ); // assign( number_of_densities(), false ); - - default_microenvironment_options.initial_condition_vector.push_back( 1.0 ); - - default_microenvironment_options.Dirichlet_all.push_back( true ); -// default_microenvironment_options.Dirichlet_interior.push_back( true ); - default_microenvironment_options.Dirichlet_xmin.push_back( false ); - default_microenvironment_options.Dirichlet_xmax.push_back( false ); - default_microenvironment_options.Dirichlet_ymin.push_back( false ); - default_microenvironment_options.Dirichlet_ymax.push_back( false ); - default_microenvironment_options.Dirichlet_zmin.push_back( false ); - default_microenvironment_options.Dirichlet_zmax.push_back( false ); - - default_microenvironment_options.Dirichlet_xmin_values.push_back( 1.0 ); - default_microenvironment_options.Dirichlet_xmax_values.push_back( 1.0 ); - default_microenvironment_options.Dirichlet_ymin_values.push_back( 1.0 ); - default_microenvironment_options.Dirichlet_ymax_values.push_back( 1.0 ); - default_microenvironment_options.Dirichlet_zmin_values.push_back( 1.0 ); - default_microenvironment_options.Dirichlet_zmax_values.push_back( 1.0 ); + one_half = one; + one_half *= 0.5; - return; + one_third = one; + one_third /= 3.0; + + dirichlet_value_vectors.assign( mesh.voxels.size(), one ); + dirichlet_activation_vector.push_back( false ); + dirichlet_activation_vectors.assign( mesh.voxels.size(), dirichlet_activation_vector ); + + // fix in PhysiCell preview November 2017 + default_microenvironment_options.Dirichlet_condition_vector.push_back( 1.0 ); // = one; + default_microenvironment_options.Dirichlet_activation_vector.push_back( false ); // assign( number_of_densities(), false ); + + default_microenvironment_options.initial_condition_vector.push_back( 1.0 ); + + default_microenvironment_options.Dirichlet_all.push_back( true ); +// default_microenvironment_options.Dirichlet_interior.push_back( true ); + default_microenvironment_options.Dirichlet_xmin.push_back( false ); + default_microenvironment_options.Dirichlet_xmax.push_back( false ); + default_microenvironment_options.Dirichlet_ymin.push_back( false ); + default_microenvironment_options.Dirichlet_ymax.push_back( false ); + default_microenvironment_options.Dirichlet_zmin.push_back( false ); + default_microenvironment_options.Dirichlet_zmax.push_back( false ); + + default_microenvironment_options.Dirichlet_xmin_values.push_back( 1.0 ); + default_microenvironment_options.Dirichlet_xmax_values.push_back( 1.0 ); + default_microenvironment_options.Dirichlet_ymin_values.push_back( 1.0 ); + default_microenvironment_options.Dirichlet_ymax_values.push_back( 1.0 ); + default_microenvironment_options.Dirichlet_zmin_values.push_back( 1.0 ); + default_microenvironment_options.Dirichlet_zmax_values.push_back( 1.0 ); + + return; +} + +void Microenvironment::update_density( std::string name , std::string units ) +{ + // density exist? + int density_index = find_density_index( name ); + if ( density_index == -1 ) + { + // add density + return Microenvironment::add_density( name , units ); + } + + // update units + density_units[density_index] = units; + return; +} + +void Microenvironment::update_density( std::string name , std::string units, double diffusion_constant, double decay_rate ) +{ + // density exist? + int density_index = find_density_index( name ); + if ( density_index == -1 ) + { + // add density + return Microenvironment::add_density( name , units, diffusion_constant, decay_rate ); + } + + // update units + density_units[density_index] = units; + // update coefficients + diffusion_coefficients[density_index] = diffusion_constant; + decay_rates[density_index] = decay_rate; + return; } int Microenvironment::find_density_index( std::string name ) @@ -1427,7 +1463,7 @@ void initialize_microenvironment( void ) } */ - // April 2023: no longer necessary after flipping our approach and doing an "additive" instead of "subtractive" DCs handling. I.e., we assume DC activation is false by default; make true on-demand. + // April 2023: no longer necessary after flipping our approach and doing an "additive" instead of "subtractive" DCs handling. I.e., we assume DC activation is false by default; make true on-demand. // // set the Dirichlet condition activation vector to match the microenvironment options // for( int i=0 ; i < default_microenvironment_options.Dirichlet_activation_vector.size(); i++ ) diff --git a/BioFVM/BioFVM_microenvironment.h b/BioFVM/BioFVM_microenvironment.h index 8b894e880..5d7fa886b 100644 --- a/BioFVM/BioFVM_microenvironment.h +++ b/BioFVM/BioFVM_microenvironment.h @@ -181,16 +181,20 @@ class Microenvironment void resize_space( double x_start, double x_end, double y_start, double y_end, double z_start, double z_end , double dx_new , double dy_new , double dz_new ); void resize_space_uniform( double x_start, double x_end, double y_start, double y_end, double z_start, double z_end , double dx_new ); - void resize_densities( int new_size ); - void add_density( void ); + void resize_densities( int new_size ); + + void add_density( void ); void add_density( std::string name , std::string units ); - void add_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); + void add_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); - void set_density( int index , std::string name , std::string units ); - void set_density( int index , std::string name , std::string units , double diffusion_constant , double decay_rate ); + void update_density( std::string name , std::string units ); + void update_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); + + void set_density( int index , std::string name , std::string units ); + void set_density( int index , std::string name , std::string units , double diffusion_constant , double decay_rate ); + + int find_density_index( std::string name ); - int find_density_index( std::string name ); - int voxel_index( int i, int j, int k ); std::vector cartesian_indices( int n ); diff --git a/modules/PhysiCell_settings.cpp b/modules/PhysiCell_settings.cpp index 8b5786140..9209939bb 100644 --- a/modules/PhysiCell_settings.cpp +++ b/modules/PhysiCell_settings.cpp @@ -78,7 +78,7 @@ bool physicell_config_dom_initialized = false; pugi::xml_document physicell_config_doc; pugi::xml_node physicell_config_root; -bool load_PhysiCell_config_file( std::string filename ) +bool load_PhysiCell_config_file( std::string filename , bool update_variables ) { std::cout << "Using config file " << filename << " ... " << std::endl ; pugi::xml_parse_result result = physicell_config_doc.load_file( filename.c_str() ); @@ -96,7 +96,7 @@ bool load_PhysiCell_config_file( std::string filename ) // now read the microenvironment (optional) - if( !setup_microenvironment_from_XML( physicell_config_root ) ) + if( !setup_microenvironment_from_XML( physicell_config_root, update_variables ) ) { std::cout << std::endl << "Warning: microenvironment_setup not found in " << filename << std::endl @@ -106,7 +106,7 @@ bool load_PhysiCell_config_file( std::string filename ) // now read user parameters - parameters.read_from_pugixml( physicell_config_root ); + parameters.read_from_pugixml( physicell_config_root, update_variables ); return true; } @@ -523,6 +523,63 @@ void Parameters::assert_not_exists( std::string search_name ) exit(-1); } +void Parameters::update_parameter( std::string my_name , T my_value ) +{ + // check if variable already exist + int parameter_index = -1; + auto it = name_to_index_map.find(my_name); + + if (it == name_to_index_map.end()) { + // generate new variable + return Parameters::add_parameter(my_name, my_value); + } + + // change value + parameter_index = it->second; + parameters[parameter_index].value = my_value; + + return; +} + +void Parameters::update_parameter( std::string my_name , T my_value , std::string my_units ) +{ + // check if variable already exist + int parameter_index = -1; + auto it = name_to_index_map.find(my_name); + + if (it == name_to_index_map.end()) { + // generate new variable + return Parameters::add_parameter(my_name, my_value, my_units); + } + + // change value and unit + parameter_index = it->second; + parameters[parameter_index].value = my_value; + parameters[parameter_index].units = my_units; + + return; +} + +template +void Parameters::update_parameter( Parameter param ) +{ + // check if variable already exist + int parameter_index = -1; + auto it = name_to_index_map.find(param.name); + + if (it == name_to_index_map.end()) { + // generate new variable + return Parameters::add_parameter(param); + } + + // change value and unit + parameter_index = it->second; + parameters[parameter_index].value = param.value; + parameters[parameter_index].units = param.units; + + return; +} + std::ostream& operator<<( std::ostream& os , const User_Parameters up ) { os << "Bool parameters:: " << std::endl << up.bools << std::endl; @@ -532,45 +589,60 @@ std::ostream& operator<<( std::ostream& os , const User_Parameters up ) return os; } -void User_Parameters::read_from_pugixml( pugi::xml_node parent_node ) +void User_Parameters::read_from_pugixml( pugi::xml_node parent_node , bool update_parameter ) { - pugi::xml_node node = xml_find_node( parent_node , "user_parameters" ); - - pugi::xml_node node1 = node.first_child(); - int i = 0; + pugi::xml_node node = xml_find_node( parent_node , "user_parameters" ); + + pugi::xml_node node1 = node.first_child(); + int i = 0; while( node1 ) { std::string name = xml_get_my_name( node1 ); - std::string units = node1.attribute( "units" ).value(); + std::string units = node1.attribute( "units" ).value(); if( units == "" ) - { units = "dimensionless"; } - + { units = "dimensionless"; } + std::string type = node1.attribute( "type" ).value(); if (type == "bool") { bool value = xml_get_my_bool_value(node1); - bools.add_parameter(name, value, units); + if ( update_parameter ) + { bools.update_parameter( name, value, units ); } + else + { bools.add_parameter( name , value, units ); } } else if (type == "int") { int value = xml_get_my_int_value(node1); - ints.add_parameter(name, value, units); + if ( update_parameter ) + { ints.update_parameter( name, value, units ); } + else + { ints.add_parameter( name , value, units ); } } else if (type == "double") { double value = xml_get_my_double_value(node1); - doubles.add_parameter(name, value, units); + if ( update_parameter ) + { doubles.update_parameter( name, value, units ); } + else + { doubles.add_parameter( name , value, units ); } } else if (type == "string") { std::string value = xml_get_my_string_value(node1); - strings.add_parameter(name, value, units); + if ( update_parameter ) + { strings.update_parameter( name, value, units ); } + else + { strings.add_parameter( name, value , units ); } } else // default if no type specified { double value = xml_get_my_double_value(node1); - doubles.add_parameter(name, value, units); + if ( update_parameter ) + { doubles.update_parameter( name, value, units ); } + else + { doubles.add_parameter( name , value, units ); } } node1 = node1.next_sibling(); @@ -600,7 +672,7 @@ template std::ostream& operator<<(std::ostream& os, const Parameter& param) template std::ostream& operator<<(std::ostream& os, const Parameter& param); template std::ostream& operator<<(std::ostream& os, const Parameter& param); -bool setup_microenvironment_from_XML( pugi::xml_node root_node ) +bool setup_microenvironment_from_XML( pugi::xml_node root_node , bool update_density ) { pugi::xml_node node; @@ -658,6 +730,8 @@ bool setup_microenvironment_from_XML( pugi::xml_node root_node ) // add the substrate if( i == 0 ) { microenvironment.set_density( 0, name, units ); } + else if( update_density ) + { microenvironment.update_density( name, units ); } else { microenvironment.add_density( name, units ); } @@ -930,7 +1004,7 @@ bool setup_microenvironment_from_XML( pugi::xml_node root_node ) return true; } -bool setup_microenvironment_from_XML( void ) -{ return setup_microenvironment_from_XML( physicell_config_root ); } +bool setup_microenvironment_from_XML( bool update_density ) +{ return setup_microenvironment_from_XML( physicell_config_root, update_density ); } -}; +}; diff --git a/modules/PhysiCell_settings.h b/modules/PhysiCell_settings.h index c422e4cda..b287c2944 100644 --- a/modules/PhysiCell_settings.h +++ b/modules/PhysiCell_settings.h @@ -89,7 +89,7 @@ namespace PhysiCell{ extern pugi::xml_node physicell_config_root; -bool load_PhysiCell_config_file( std::string filename ); +bool load_PhysiCell_config_file( std::string filename , bool update_variables = false ); class PhysiCell_Settings { @@ -174,32 +174,35 @@ template class Parameters { private: - std::unordered_map name_to_index_map; - + std::unordered_map name_to_index_map; + template - friend std::ostream& operator<<( std::ostream& os , const Parameters& params ); + friend std::ostream& operator<<( std::ostream& os , const Parameters& params ); - public: - Parameters(); + public: + Parameters(); - std::vector< Parameter > parameters; - - void add_parameter( std::string my_name ); - void add_parameter( std::string my_name , T my_value ); - void add_parameter( std::string my_name , T my_value , std::string my_units ); - + std::vector< Parameter > parameters; + + void add_parameter( std::string my_name ); + void add_parameter( std::string my_name , T my_value ); + void add_parameter( std::string my_name , T my_value , std::string my_units ); void add_parameter( Parameter param ); - - int find_index( std::string search_name ); - - // these access the values + + void update_parameter( std::string my_name , T my_value ); + void update_parameter( std::string my_name , T my_value , std::string my_units ); + void update_parameter( Parameter param ); + + int find_index( std::string search_name ); + + // these access the values T& operator()( int i ); - T& operator()( std::string str ); + T& operator()( std::string str ); - // these access the full, raw parameters + // these access the full, raw parameters Parameter& operator[]( int i ); - Parameter& operator[]( std::string str ); - + Parameter& operator[]( std::string str ); + int size( void ) const; void assert_not_exists(std::string search_name); @@ -208,25 +211,25 @@ class Parameters class User_Parameters { private: - friend std::ostream& operator<<( std::ostream& os , const User_Parameters up ); + friend std::ostream& operator<<( std::ostream& os , const User_Parameters up ); public: - Parameters bools; - Parameters ints; - Parameters doubles; - Parameters strings; - - void read_from_pugixml( pugi::xml_node parent_node ); + Parameters bools; + Parameters ints; + Parameters doubles; + Parameters strings; + + void read_from_pugixml( pugi::xml_node parent_node , bool update_parameter = false); }; -extern PhysiCell_Globals PhysiCell_globals; +extern PhysiCell_Globals PhysiCell_globals; extern PhysiCell_Settings PhysiCell_settings; extern User_Parameters parameters; -bool setup_microenvironment_from_XML( pugi::xml_node root_node ); -bool setup_microenvironment_from_XML( void ); +bool setup_microenvironment_from_XML( pugi::xml_node root_node , bool update_density = false ); +bool setup_microenvironment_from_XML( bool update_density = false ); } From 876cc60229227cfcc888a745b776b31202338ebc Mon Sep 17 00:00:00 2001 From: bue Date: Sat, 21 Sep 2024 07:57:09 -0400 Subject: [PATCH 2/7] bigfix. --- BioFVM/BioFVM_microenvironment.cpp | 2 +- modules/PhysiCell_settings.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/BioFVM/BioFVM_microenvironment.cpp b/BioFVM/BioFVM_microenvironment.cpp index 3ff10180b..db93813cb 100644 --- a/BioFVM/BioFVM_microenvironment.cpp +++ b/BioFVM/BioFVM_microenvironment.cpp @@ -493,7 +493,7 @@ void Microenvironment::add_density( std::string name , std::string units, double // check if density exist if (find_density_index( name ) != -1) { - std::cout << "Error: density named " << name << " already exists!" << std::endl; + std::cout << "Error: density named " << name << " already exists. You probably want your substrates all have unique names!" << std::endl; exit(-1); } diff --git a/modules/PhysiCell_settings.cpp b/modules/PhysiCell_settings.cpp index 9209939bb..5249bf6fd 100644 --- a/modules/PhysiCell_settings.cpp +++ b/modules/PhysiCell_settings.cpp @@ -523,6 +523,7 @@ void Parameters::assert_not_exists( std::string search_name ) exit(-1); } +template void Parameters::update_parameter( std::string my_name , T my_value ) { // check if variable already exist @@ -541,6 +542,7 @@ void Parameters::update_parameter( std::string my_name , T my_value ) return; } +template void Parameters::update_parameter( std::string my_name , T my_value , std::string my_units ) { // check if variable already exist From 47c3cc33d16fab4e0e84e11de65241e98f476946 Mon Sep 17 00:00:00 2001 From: bue Date: Sat, 21 Sep 2024 08:44:00 -0400 Subject: [PATCH 3/7] bigfix. --- modules/PhysiCell_settings.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/PhysiCell_settings.h b/modules/PhysiCell_settings.h index b287c2944..a2fd7960f 100644 --- a/modules/PhysiCell_settings.h +++ b/modules/PhysiCell_settings.h @@ -219,7 +219,7 @@ class User_Parameters Parameters doubles; Parameters strings; - void read_from_pugixml( pugi::xml_node parent_node , bool update_parameter = false); + void read_from_pugixml( pugi::xml_node parent_node , bool update_parameter = false ); }; extern PhysiCell_Globals PhysiCell_globals; From 252f0293811310d6c369c8c74a2d01163f3cac1f Mon Sep 17 00:00:00 2001 From: bue Date: Wed, 25 Sep 2024 17:40:49 -0400 Subject: [PATCH 4/7] Microenvironment::update_density and Parameters::update_parameter through an error, if one tries to update a density or parameter that not already exists. --- BioFVM/BioFVM_microenvironment.cpp | 8 ++++---- modules/PhysiCell_settings.cpp | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/BioFVM/BioFVM_microenvironment.cpp b/BioFVM/BioFVM_microenvironment.cpp index db93813cb..5a615b379 100644 --- a/BioFVM/BioFVM_microenvironment.cpp +++ b/BioFVM/BioFVM_microenvironment.cpp @@ -568,8 +568,8 @@ void Microenvironment::update_density( std::string name , std::string units ) int density_index = find_density_index( name ); if ( density_index == -1 ) { - // add density - return Microenvironment::add_density( name , units ); + std::cout << "Error: density named " << name << " does not exist. Cannot update density!" << std::endl; + exit(-1); } // update units @@ -583,8 +583,8 @@ void Microenvironment::update_density( std::string name , std::string units, dou int density_index = find_density_index( name ); if ( density_index == -1 ) { - // add density - return Microenvironment::add_density( name , units, diffusion_constant, decay_rate ); + std::cout << "Error: density named " << name << " does not exist. Cannot update density!" << std::endl; + exit(-1); } // update units diff --git a/modules/PhysiCell_settings.cpp b/modules/PhysiCell_settings.cpp index 5249bf6fd..3949a3113 100644 --- a/modules/PhysiCell_settings.cpp +++ b/modules/PhysiCell_settings.cpp @@ -531,8 +531,8 @@ void Parameters::update_parameter( std::string my_name , T my_value ) auto it = name_to_index_map.find(my_name); if (it == name_to_index_map.end()) { - // generate new variable - return Parameters::add_parameter(my_name, my_value); + std::cout << "Error: parameter named " << my_name << " does not exist. Cannot update the parameter!" << std::endl; + exit(-1); } // change value @@ -550,8 +550,8 @@ void Parameters::update_parameter( std::string my_name , T my_value , std::st auto it = name_to_index_map.find(my_name); if (it == name_to_index_map.end()) { - // generate new variable - return Parameters::add_parameter(my_name, my_value, my_units); + std::cout << "Error: parameter named " << my_name << " does not exist. Cannot update the parameter!" << std::endl; + exit(-1); } // change value and unit @@ -570,8 +570,8 @@ void Parameters::update_parameter( Parameter param ) auto it = name_to_index_map.find(param.name); if (it == name_to_index_map.end()) { - // generate new variable - return Parameters::add_parameter(param); + std::cout << "Error: parameter named " << my_name << " does not exist. Cannot update the parameter!" << std::endl; + exit(-1); } // change value and unit From a0cc6681a117a15d331f2b1fa7269759b65a0e06 Mon Sep 17 00:00:00 2001 From: bue Date: Wed, 25 Sep 2024 17:49:09 -0400 Subject: [PATCH 5/7] Microenvironment::update_density and Parameters::update_parameter through an error, if one tries to update a density or parameter that not already exists. --- modules/PhysiCell_settings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/PhysiCell_settings.cpp b/modules/PhysiCell_settings.cpp index 3949a3113..1e01e513c 100644 --- a/modules/PhysiCell_settings.cpp +++ b/modules/PhysiCell_settings.cpp @@ -570,7 +570,7 @@ void Parameters::update_parameter( Parameter param ) auto it = name_to_index_map.find(param.name); if (it == name_to_index_map.end()) { - std::cout << "Error: parameter named " << my_name << " does not exist. Cannot update the parameter!" << std::endl; + std::cout << "Error: parameter named " << param.name << " does not exist. Cannot update the parameter!" << std::endl; exit(-1); } From 58cd783a9728f4792af49395f0a4d9582acfe1f4 Mon Sep 17 00:00:00 2001 From: bue Date: Wed, 2 Oct 2024 16:33:09 -0400 Subject: [PATCH 6/7] write update_density and update_parameter in a more concise way. --- BioFVM/BioFVM_microenvironment.cpp | 37 ++++++++------------ BioFVM/BioFVM_microenvironment.h | 4 ++- modules/PhysiCell_settings.cpp | 55 ++++++++---------------------- modules/PhysiCell_settings.h | 1 + 4 files changed, 33 insertions(+), 64 deletions(-) diff --git a/BioFVM/BioFVM_microenvironment.cpp b/BioFVM/BioFVM_microenvironment.cpp index 5a615b379..c6723d5b7 100644 --- a/BioFVM/BioFVM_microenvironment.cpp +++ b/BioFVM/BioFVM_microenvironment.cpp @@ -562,36 +562,27 @@ void Microenvironment::add_density( std::string name , std::string units, double return; } -void Microenvironment::update_density( std::string name , std::string units ) +void Microenvironment::assert_exists( std::string search_name ) { - // density exist? - int density_index = find_density_index( name ); - if ( density_index == -1 ) - { - std::cout << "Error: density named " << name << " does not exist. Cannot update density!" << std::endl; - exit(-1); - } + if( find_density_index( search_name ) != -1 ) + { return; } - // update units - density_units[density_index] = units; + std::cout << "Error: density named " << search_name << " does not exist. Cannot update density!" << std::endl; + exit(-1); +} + +void Microenvironment::update_density( std::string name , std::string units ) +{ + assert_exists( name ); + density_units[ find_density_index( name ) ] = units; return; } void Microenvironment::update_density( std::string name , std::string units, double diffusion_constant, double decay_rate ) { - // density exist? - int density_index = find_density_index( name ); - if ( density_index == -1 ) - { - std::cout << "Error: density named " << name << " does not exist. Cannot update density!" << std::endl; - exit(-1); - } - - // update units - density_units[density_index] = units; - // update coefficients - diffusion_coefficients[density_index] = diffusion_constant; - decay_rates[density_index] = decay_rate; + update_density( name, units ); + diffusion_coefficients[ find_density_index( name ) ] = diffusion_constant; + decay_rates[ find_density_index( name ) ] = decay_rate; return; } diff --git a/BioFVM/BioFVM_microenvironment.h b/BioFVM/BioFVM_microenvironment.h index 5d7fa886b..377a5f05f 100644 --- a/BioFVM/BioFVM_microenvironment.h +++ b/BioFVM/BioFVM_microenvironment.h @@ -187,6 +187,8 @@ class Microenvironment void add_density( std::string name , std::string units ); void add_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); + void assert_exists( std::string search_name ); + void update_density( std::string name , std::string units ); void update_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); @@ -258,7 +260,7 @@ class Microenvironment void set_substrate_dirichlet_activation( int index, std::vector& new_value ); bool get_substrate_dirichlet_activation( int substrate_index, int index ); - double get_substrate_dirichlet_value( int substrate_index, int index ); + double get_substrate_dirichlet_value( int substrate_index, int index ); bool& is_dirichlet_node( int voxel_index ); diff --git a/modules/PhysiCell_settings.cpp b/modules/PhysiCell_settings.cpp index 1e01e513c..b89ec6c98 100644 --- a/modules/PhysiCell_settings.cpp +++ b/modules/PhysiCell_settings.cpp @@ -524,61 +524,36 @@ void Parameters::assert_not_exists( std::string search_name ) } template -void Parameters::update_parameter( std::string my_name , T my_value ) +void Parameters::assert_exists( std::string search_name ) { - // check if variable already exist - int parameter_index = -1; - auto it = name_to_index_map.find(my_name); - - if (it == name_to_index_map.end()) { - std::cout << "Error: parameter named " << my_name << " does not exist. Cannot update the parameter!" << std::endl; - exit(-1); - } + if( find_index( search_name ) != -1 ) + { return; } - // change value - parameter_index = it->second; - parameters[parameter_index].value = my_value; + std::cout << "Error: parameter named " << search_name << " does not exist. Cannot update the parameter!" << std::endl; + exit(-1); +} +template +void Parameters::update_parameter( std::string my_name , T my_value ) +{ + assert_exists( my_name ); + parameters[ find_index( my_name ) ].value = my_value; return; } template void Parameters::update_parameter( std::string my_name , T my_value , std::string my_units ) { - // check if variable already exist - int parameter_index = -1; - auto it = name_to_index_map.find(my_name); - - if (it == name_to_index_map.end()) { - std::cout << "Error: parameter named " << my_name << " does not exist. Cannot update the parameter!" << std::endl; - exit(-1); - } - - // change value and unit - parameter_index = it->second; - parameters[parameter_index].value = my_value; - parameters[parameter_index].units = my_units; - + update_parameter( my_name, my_value ); + parameters[ find_index( my_name ) ].units = my_units; return; } template void Parameters::update_parameter( Parameter param ) { - // check if variable already exist - int parameter_index = -1; - auto it = name_to_index_map.find(param.name); - - if (it == name_to_index_map.end()) { - std::cout << "Error: parameter named " << param.name << " does not exist. Cannot update the parameter!" << std::endl; - exit(-1); - } - - // change value and unit - parameter_index = it->second; - parameters[parameter_index].value = param.value; - parameters[parameter_index].units = param.units; - + update_parameter( param.name, param.value ); + parameters[ find_index( param.name ) ].units = param.units; return; } diff --git a/modules/PhysiCell_settings.h b/modules/PhysiCell_settings.h index a2fd7960f..481ed8f96 100644 --- a/modules/PhysiCell_settings.h +++ b/modules/PhysiCell_settings.h @@ -206,6 +206,7 @@ class Parameters int size( void ) const; void assert_not_exists(std::string search_name); + void assert_exists(std::string search_name); }; class User_Parameters From df4e3741a2cacb5da150d81ea3d9d9b29ed9537d Mon Sep 17 00:00:00 2001 From: bue Date: Thu, 3 Oct 2024 01:32:33 -0400 Subject: [PATCH 7/7] write update_density and update_parameter in a concise and speedy way. --- BioFVM/BioFVM_microenvironment.cpp | 18 ++++++++++-------- BioFVM/BioFVM_microenvironment.h | 3 +-- modules/PhysiCell_settings.cpp | 28 ++++++++++++++++------------ modules/PhysiCell_settings.h | 4 ++-- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/BioFVM/BioFVM_microenvironment.cpp b/BioFVM/BioFVM_microenvironment.cpp index c6723d5b7..942f8b7c3 100644 --- a/BioFVM/BioFVM_microenvironment.cpp +++ b/BioFVM/BioFVM_microenvironment.cpp @@ -562,10 +562,11 @@ void Microenvironment::add_density( std::string name , std::string units, double return; } -void Microenvironment::assert_exists( std::string search_name ) +int Microenvironment::assert_density_exists( std::string search_name ) { - if( find_density_index( search_name ) != -1 ) - { return; } + int density_index = find_density_index( search_name ); + if( density_index != -1 ) + { return density_index; } std::cout << "Error: density named " << search_name << " does not exist. Cannot update density!" << std::endl; exit(-1); @@ -573,16 +574,17 @@ void Microenvironment::assert_exists( std::string search_name ) void Microenvironment::update_density( std::string name , std::string units ) { - assert_exists( name ); - density_units[ find_density_index( name ) ] = units; + int density_index = assert_density_exists( name ); + density_units[ density_index ] = units; return; } void Microenvironment::update_density( std::string name , std::string units, double diffusion_constant, double decay_rate ) { - update_density( name, units ); - diffusion_coefficients[ find_density_index( name ) ] = diffusion_constant; - decay_rates[ find_density_index( name ) ] = decay_rate; + int density_index = assert_density_exists( name ); + density_units[ density_index ] = units; + diffusion_coefficients[ density_index ] = diffusion_constant; + decay_rates[ density_index ] = decay_rate; return; } diff --git a/BioFVM/BioFVM_microenvironment.h b/BioFVM/BioFVM_microenvironment.h index 377a5f05f..130bfe2ab 100644 --- a/BioFVM/BioFVM_microenvironment.h +++ b/BioFVM/BioFVM_microenvironment.h @@ -187,8 +187,6 @@ class Microenvironment void add_density( std::string name , std::string units ); void add_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); - void assert_exists( std::string search_name ); - void update_density( std::string name , std::string units ); void update_density( std::string name , std::string units, double diffusion_constant, double decay_rate ); @@ -196,6 +194,7 @@ class Microenvironment void set_density( int index , std::string name , std::string units , double diffusion_constant , double decay_rate ); int find_density_index( std::string name ); + int assert_density_exists( std::string search_name ); int voxel_index( int i, int j, int k ); std::vector cartesian_indices( int n ); diff --git a/modules/PhysiCell_settings.cpp b/modules/PhysiCell_settings.cpp index b89ec6c98..96ba90552 100644 --- a/modules/PhysiCell_settings.cpp +++ b/modules/PhysiCell_settings.cpp @@ -486,7 +486,7 @@ void Parameters::add_parameter( std::string my_name , T my_value ) template void Parameters::add_parameter( std::string my_name , T my_value , std::string my_units ) { - assert_not_exists(my_name); + assert_parameter_not_exists(my_name); Parameter* pNew; pNew = new Parameter ; @@ -505,7 +505,7 @@ void Parameters::add_parameter( std::string my_name , T my_value , std::strin template void Parameters::add_parameter( Parameter param ) { - assert_not_exists(param.name); + assert_parameter_not_exists(param.name); int n = parameters.size(); parameters.push_back( param); @@ -514,7 +514,7 @@ void Parameters::add_parameter( Parameter param ) } template -void Parameters::assert_not_exists( std::string search_name ) +void Parameters::assert_parameter_not_exists( std::string search_name ) { if( find_index( search_name ) == -1 ) { return; } @@ -524,10 +524,12 @@ void Parameters::assert_not_exists( std::string search_name ) } template -void Parameters::assert_exists( std::string search_name ) +int Parameters::assert_parameter_exists( std::string search_name ) { - if( find_index( search_name ) != -1 ) - { return; } + + int parameter_index = find_index( search_name ); + if( parameter_index != -1 ) + { return parameter_index; } std::cout << "Error: parameter named " << search_name << " does not exist. Cannot update the parameter!" << std::endl; exit(-1); @@ -536,24 +538,26 @@ void Parameters::assert_exists( std::string search_name ) template void Parameters::update_parameter( std::string my_name , T my_value ) { - assert_exists( my_name ); - parameters[ find_index( my_name ) ].value = my_value; + int parameter_index = assert_parameter_exists( my_name ); + parameters[ parameter_index ].value = my_value; return; } template void Parameters::update_parameter( std::string my_name , T my_value , std::string my_units ) { - update_parameter( my_name, my_value ); - parameters[ find_index( my_name ) ].units = my_units; + int parameter_index = assert_parameter_exists( my_name ); + parameters[ parameter_index ].value = my_value; + parameters[ parameter_index ].units = my_units; return; } template void Parameters::update_parameter( Parameter param ) { - update_parameter( param.name, param.value ); - parameters[ find_index( param.name ) ].units = param.units; + int parameter_index = assert_parameter_exists( param.name ); + parameters[ parameter_index ].value = param.value; + parameters[ parameter_index ].units = param.units; return; } diff --git a/modules/PhysiCell_settings.h b/modules/PhysiCell_settings.h index 481ed8f96..d981128fc 100644 --- a/modules/PhysiCell_settings.h +++ b/modules/PhysiCell_settings.h @@ -205,8 +205,8 @@ class Parameters int size( void ) const; - void assert_not_exists(std::string search_name); - void assert_exists(std::string search_name); + void assert_parameter_not_exists(std::string search_name); + int assert_parameter_exists(std::string search_name); }; class User_Parameters