HeartConfig.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #ifndef HEARTCONFIG_HPP_
00031 #define HEARTCONFIG_HPP_
00032 
00033 #include <string>
00034 #include <vector>
00035 #include <set>
00036 
00037 #include "UblasIncludes.hpp"
00038 
00039 #include "ArchiveLocationInfo.hpp"
00040 #include "ChasteParameters_2_3.hpp"
00041 
00042 #include "AbstractStimulusFunction.hpp"
00043 // These are needed here for Boost < 1.37
00044 #include "SimpleStimulus.hpp"
00045 #include "RegularStimulus.hpp"
00046 
00047 #include "ChastePoint.hpp"
00048 #include "ChasteCuboid.hpp"
00049 #include "ChasteEllipsoid.hpp"
00050 #include "DistributedTetrahedralMeshPartitionType.hpp"
00051 
00052 #include <boost/shared_ptr.hpp>
00053 
00054 #include "ChasteSerialization.hpp"
00055 #include "ChasteSerializationVersion.hpp"
00056 #include <boost/serialization/split_member.hpp>
00057 
00058 namespace cp = chaste::parameters::v2_3;
00059 
00060 // Forward declaration to avoid circular includes
00061 class HeartFileFinder;
00062 
00063 
00074 class HeartConfig
00075 {
00076 private:
00083     void CheckTimeSteps() const;
00084 
00086     friend class boost::serialization::access;
00093     template<class Archive>
00094     void save(Archive & archive, const unsigned int version) const
00095     {
00096         //Only the Master should be writing the configuration file
00097         if (PetscTools::AmMaster())
00098         {
00099             mpInstance->Write( true );
00100         }
00101         PetscTools::Barrier("HeartConfig::save");
00102     }
00103 
00110     template<class Archive>
00111     void load(Archive & archive, const unsigned int version)
00112     {
00113         /*
00114          *  This method implements the logic required by HeartConfig to be able to handle resuming a simulation via the executable.
00115          *
00116          *  When the control reaches the method mpUserParameters and mpDefaultParameters point to the files specified as resuming parameters.
00117          *  However SetDefaultsFile() and SetParametersFile() will set those variables to point to the archived parameters.
00118          *
00119          *  We make a temporary copy of mpUserParameters so we don't lose its content. At the end of the method we update the new mpUserParameters
00120          *  with the resuming parameters.
00121          */
00122         assert(mpUserParameters.use_count() > 0);
00123         boost::shared_ptr<cp::chaste_parameters_type> p_new_parameters = mpUserParameters;
00124 
00125         std::string defaults_filename_xml = ArchiveLocationInfo::GetArchiveDirectory() + "ChasteDefaults.xml";
00126         HeartConfig::Instance()->SetDefaultsFile(defaults_filename_xml);
00127 
00128         /*
00129          *  When we unarchive a simulation, we load the old parameters file in order to inherit things such
00130          *  as default cell model, stimuli, heterogeneities, ... This has the side effect of inheriting the
00131          *  <CheckpointSimulation> element (if defined).
00132          *
00133          *  We disable checkpointing definition coming from the unarchived config file. We will enable it again
00134          *  if defined in the resume config file.
00135          */
00136         std::string parameters_filename_xml = ArchiveLocationInfo::GetArchiveDirectory() + "ChasteParameters.xml";
00137         HeartConfig::Instance()->SetParametersFile(parameters_filename_xml);
00138 
00139         HeartConfig::Instance()->SetCheckpointSimulation(false);
00140 
00141         // If we are resuming a simulation, some parameters can be altered at this point.
00142         if (p_new_parameters->ResumeSimulation().present())
00143         {
00144             UpdateParametersFromResumeSimulation(p_new_parameters);
00145         }
00146     }
00147     BOOST_SERIALIZATION_SPLIT_MEMBER()
00148 
00149     
00155     void UpdateParametersFromResumeSimulation(boost::shared_ptr<cp::chaste_parameters_type> pResumeParameters);
00156 
00157 public:
00162     typedef std::map<std::string, std::string> SchemaLocationsMap;
00163 
00164 private:
00168     SchemaLocationsMap mSchemaLocations;
00169 
00173     void SetDefaultSchemaLocations();
00174 
00175 public:
00181     static HeartConfig* Instance();
00182 
00188     void SetUseFixedSchemaLocation(bool useFixedSchemaLocation);
00189 
00196     void SetFixedSchemaLocations(const SchemaLocationsMap& rSchemaLocations);
00197 
00202     void SetDefaultsFile(const std::string& rFileName);
00203 
00208     void SetParametersFile(const std::string& rFileName);
00209 
00221     void Write(bool useArchiveLocationInfo=false, std::string subfolderName="output");
00222 
00228     void CopySchema(const std::string& rToDirectory);
00229 
00234     boost::shared_ptr<cp::chaste_parameters_type> ReadFile(const std::string& rFileName);
00235 
00240     static void Reset();
00241 
00242     ~HeartConfig(); 
00250     unsigned GetVersionFromNamespace(const std::string& rNamespaceUri);
00251 
00253     //
00254     //  Get methods
00255     //
00257 
00258     // Methods for asking the configuration file about which sections are defined.
00259 
00265     bool IsSimulationDefined() const;
00266 
00272     bool IsSimulationResumed() const;
00273 
00274     // Simulation
00275     unsigned GetSpaceDimension() const; 
00276     double GetSimulationDuration() const; 
00283     cp::domain_type GetDomain() const;
00284 
00292     cp::ionic_model_selection_type GetDefaultIonicModel() const;
00293 
00304     template<unsigned DIM>
00305     void GetIonicModelRegions(std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rDefinedRegions,
00306                                std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00307 
00319     void SetIonicModelRegions(std::vector<ChasteCuboid<3> >& rDefinedRegions,
00320                               std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00321 
00322     bool IsMeshProvided() const; 
00323     bool GetCreateMesh() const; 
00324     bool GetCreateSlab() const; 
00325     bool GetCreateSheet() const; 
00326     bool GetCreateFibre() const; 
00327     bool GetLoadMesh() const; 
00332     void GetSlabDimensions(c_vector<double, 3>& slabDimensions) const;
00336     void GetSheetDimensions(c_vector<double, 2>& sheetDimensions) const;
00340     void GetFibreLength(c_vector<double, 1>& fibreLength) const;
00341     double GetInterNodeSpace() const; 
00343     std::string GetMeshName() const;
00345     cp::media_type GetConductivityMedia() const;
00356      template<unsigned DIM>
00357     void GetStimuli(std::vector<boost::shared_ptr<AbstractStimulusFunction> >& rStimuliApplied,
00358                     std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rStimulatedAreas) const;
00359 
00375     template<unsigned DIM>
00376     void GetCellHeterogeneities( std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rCellHeterogeneityRegions,
00377                                  std::vector<double>& rScaleFactorGks,
00378                                  std::vector<double>& rScaleFactorIto,
00379                                  std::vector<double>& rScaleFactorGkr,
00380                                  std::vector<std::map<std::string, double> >* pParameterSettings);
00381 
00382     bool GetConductivityHeterogeneitiesProvided() const; 
00387     bool AreCellularTransmuralHeterogeneitiesRequested();
00388 
00392     double GetEpiLayerFraction();
00393 
00397     double GetEndoLayerFraction();
00398 
00402     double GetMidLayerFraction();
00403 
00407     unsigned GetEpiLayerIndex();
00408 
00412     unsigned GetEndoLayerIndex();
00413 
00417     unsigned GetMidLayerIndex();
00418 
00419 
00428     template<unsigned DIM>
00429     void GetConductivityHeterogeneities(std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& conductivitiesHeterogeneityAreas,
00430                                         std::vector< c_vector<double,3> >& intraConductivities,
00431                                         std::vector< c_vector<double,3> >& extraConductivities) const;
00432     std::string GetOutputDirectory() const; 
00444     std::string GetOutputFilenamePrefix() const;
00445 
00449     bool GetOutputVariablesProvided() const;
00450 
00457     void GetOutputVariables(std::vector<std::string>& rOutputVariables) const;
00458 
00464     bool GetOutputUsingOriginalNodeOrdering();
00465 
00471     bool GetCheckpointSimulation() const;
00472 
00478     double GetCheckpointTimestep() const;
00479 
00485     unsigned GetMaxCheckpointsOnDisk() const;
00486 
00487     // ResumeSimulation
00491     HeartFileFinder GetArchivedSimulationDir() const;
00492 
00493 
00494     // Physiological
00499     void GetIntracellularConductivities(c_vector<double, 3>& intraConductivities) const;
00504     void GetIntracellularConductivities(c_vector<double, 2>& intraConductivities) const;
00509     void GetIntracellularConductivities(c_vector<double, 1>& intraConductivities) const;
00510 
00515     void GetExtracellularConductivities(c_vector<double, 3>& extraConductivities) const;
00520     void GetExtracellularConductivities(c_vector<double, 2>& extraConductivities) const;
00525     void GetExtracellularConductivities(c_vector<double, 1>& extraConductivities) const;
00526 
00534     double GetBathConductivity(unsigned bathRegion=UINT_MAX) const;
00535 
00541     const std::set<unsigned>& rGetTissueIdentifiers();
00542 
00548     const std::set<unsigned>& rGetBathIdentifiers();
00549 
00550 
00551     double GetSurfaceAreaToVolumeRatio() const; 
00553     double GetCapacitance() const; 
00555     // Numerical
00556     double GetOdeTimeStep() const; 
00557     double GetPdeTimeStep() const; 
00558     double GetPrintingTimeStep() const; 
00560     bool GetUseAbsoluteTolerance() const; 
00561     double GetAbsoluteTolerance() const; 
00563     bool GetUseRelativeTolerance() const; 
00564     double GetRelativeTolerance() const;  
00566     const char* GetKSPSolver() const; 
00567     const char* GetKSPPreconditioner() const; 
00569     DistributedTetrahedralMeshPartitionType::type GetMeshPartitioning() const; 
00571     // Adaptivity
00575     bool IsAdaptivityParametersPresent() const;
00576 
00580     double GetTargetErrorForAdaptivity() const;
00581 
00585     double GetSigmaForAdaptivity() const;
00586 
00590     double GetMaxEdgeLengthForAdaptivity() const;
00591 
00595     double GetMinEdgeLengthForAdaptivity() const;
00596 
00600     double GetGradationForAdaptivity() const;
00601 
00605     unsigned GetMaxNodesForAdaptivity() const;
00606 
00610     unsigned GetNumberOfAdaptiveSweeps() const;
00611 
00612     // Post processing
00616     bool IsPostProcessingSectionPresent() const;
00617 
00621     void EnsurePostProcessingSectionPresent();
00622 
00626     bool IsPostProcessingRequested() const;
00627 
00631     bool IsApdMapsRequested() const;
00632 
00638     void GetApdMaps(std::vector<std::pair<double,double> >& rApdMaps) const;
00639 
00643     bool IsUpstrokeTimeMapsRequested() const;
00648     void GetUpstrokeTimeMaps (std::vector<double>& rUpstrokeTimeMaps) const;
00649 
00653     bool IsMaxUpstrokeVelocityMapRequested() const;
00654 
00659     void GetMaxUpstrokeVelocityMaps(std::vector<double>& rUpstrokeVelocityMaps) const;
00660 
00664     bool IsConductionVelocityMapsRequested() const;
00665 
00670     void GetConductionVelocityMaps(std::vector<unsigned>& rConductionVelocityMaps) const;
00671 
00675     bool IsAnyNodalTimeTraceRequested() const;
00676 
00680     void GetNodalTimeTraceRequested(std::vector<unsigned>& rRequestedNodes) const;
00681 
00685     bool IsPseudoEcgCalculationRequested() const;
00686 
00691     template<unsigned SPACE_DIM>
00692     void GetPseudoEcgElectrodePositions(std::vector<ChastePoint<SPACE_DIM> >& rPseudoEcgElectrodePositions) const;
00693 
00697     bool GetUseStateVariableInterpolation() const;
00698 
00699 
00700     // Output visualization
00701 
00703     bool IsOutputVisualizerPresent() const;
00704 
00706     bool GetVisualizeWithMeshalyzer() const;
00707 
00709     bool GetVisualizeWithCmgui() const;
00710 
00712     bool GetVisualizeWithVtk() const;
00713 
00715     bool GetVisualizeWithParallelVtk() const;
00716 
00720     bool IsElectrodesPresent() const;
00721 
00722 
00732     void GetElectrodeParameters(bool& rGroundSecondElectrode,
00733                                 unsigned& rIndex, double& rMagnitude,
00734                                 double& rStartTime, double& rDuration );
00735 
00739     bool GetUseMassLumping();
00740 
00744     bool GetUseMassLumpingForPrecond();
00745 
00750     bool GetUseReactionDiffusionOperatorSplitting();
00751 
00755     bool GetUseFixedNumberIterationsLinearSolver();
00756 
00760     unsigned GetEvaluateNumItsEveryNSolves();
00761 
00762 
00764     //
00765     //  Set methods
00766     //
00768 
00769     // Simulation
00773     void SetSpaceDimension(unsigned spaceDimension);
00774 
00779     void SetSimulationDuration(double simulationDuration);
00780 
00787     void SetDomain(const cp::domain_type& rDomain);
00788 
00796     void SetDefaultIonicModel(const cp::ionic_models_available_type& rIonicModel);
00797 
00805     void SetSlabDimensions(double x, double y, double z, double inter_node_space);
00812     void SetSheetDimensions(double x, double y, double inter_node_space);
00818     void SetFibreLength(double x, double inter_node_space);
00819 
00826     void SetMeshFileName(std::string meshPrefix, cp::media_type fibreDefinition=cp::media_type::NoFibreOrientation);
00827 
00835     void SetConductivityHeterogeneities(std::vector<ChasteCuboid<3> >& rConductivityAreas,
00836                                         std::vector< c_vector<double,3> >& rIntraConductivities,
00837                                         std::vector< c_vector<double,3> >& rExtraConductivities);
00845     void SetConductivityHeterogeneitiesEllipsoid(std::vector<ChasteEllipsoid<3> >& conductivityAreas,
00846             std::vector< c_vector<double,3> >& intraConductivities,
00847             std::vector< c_vector<double,3> >& extraConductivities);
00851     void SetOutputDirectory(const std::string& rOutputDirectory);
00862     void SetOutputFilenamePrefix(const std::string& rOutputFilenamePrefix);
00863 
00875     void SetOutputVariables(const std::vector<std::string>& rOutputVariables);
00876 
00885     void SetOutputUsingOriginalNodeOrdering(bool useOriginal);
00886 
00894      void SetCheckpointSimulation(bool checkpointSimulation, double checkpointTimestep=-1.0, unsigned maxCheckpointsOnDisk=UINT_MAX);
00895 
00896 
00897     // Physiological
00902     void SetIntracellularConductivities(const c_vector<double, 3>& rIntraConductivities);
00907     void SetIntracellularConductivities(const c_vector<double, 2>& rIntraConductivities);
00912     void SetIntracellularConductivities(const c_vector<double, 1>& rIntraConductivities);
00913 
00918     void SetExtracellularConductivities(const c_vector<double, 3>& rExtraConductivities);
00923     void SetExtracellularConductivities(const c_vector<double, 2>& rExtraConductivities);
00928     void SetExtracellularConductivities(const c_vector<double, 1>& rExtraConductivities);
00929 
00935     void SetBathConductivity(double bathConductivity);
00936 
00942     void SetBathMultipleConductivities(std::map<unsigned, double> bathConductivities);
00943 
00950     void SetTissueAndBathIdentifiers(const std::set<unsigned>& tissueIds, const std::set<unsigned>& bathIds);
00951 
00958     //void SetTissueIdentifiers(const std::set<unsigned>& tissueIds);
00959 
00964     void SetSurfaceAreaToVolumeRatio(double ratio);
00965 
00970     void SetCapacitance(double capacitance);
00971 
00972     // Numerical
00979     void SetOdePdeAndPrintingTimeSteps(double odeTimeStep, double pdeTimeStep, double printingTimeStep);
00980 
00985     void SetOdeTimeStep(double odeTimeStep);
00986 
00991     void SetPdeTimeStep(double pdeTimeStep);
00992 
00997      void SetPrintingTimeStep(double printingTimeStep);
00998 
01002     void SetUseRelativeTolerance(double relativeTolerance);
01003 
01007     void SetUseAbsoluteTolerance(double absoluteTolerance);
01008 
01012     void SetKSPSolver(const char* kspSolver);
01013 
01017     void SetKSPPreconditioner(const char* kspPreconditioner);
01018 
01022     void SetMeshPartitioning(const char* meshPartioningMethod);
01023 
01035     void SetAdaptivityParameters(double targetError, double sigma, double maxEdgeLength, double minEdgeLength,
01036                                  double gradation, unsigned maxNodes, unsigned numSweeps );
01037 
01043     void SetTargetErrorForAdaptivity(double targetError);
01044 
01050     void SetSigmaForAdaptivity(double sigma);
01051 
01057     void SetMaxEdgeLengthForAdaptivity(double maxEdgeLength);
01058 
01064     void SetMinEdgeLengthForAdaptivity(double minEdgeLength);
01065 
01071     void SetGradationForAdaptivity(double gradation);
01072 
01078     void SetMaxNodesForAdaptivity(unsigned maxNodes);
01079 
01085     void SetNumberOfAdaptiveSweeps(unsigned numSweeps);
01086 
01093     void SetApdMaps(const std::vector<std::pair<double,double> >& rApdMaps);
01094 
01100     void SetUpstrokeTimeMaps (std::vector<double>& rUpstrokeTimeMaps);
01101 
01107     void SetMaxUpstrokeVelocityMaps (std::vector<double>& rMaxUpstrokeVelocityMaps);
01108 
01113     void SetConductionVelocityMaps (std::vector<unsigned>& rConductionVelocityMaps);
01114 
01115 
01122     void SetRequestedNodalTimeTraces (std::vector<unsigned>& requestedNodes);
01123 
01129     template<unsigned SPACE_DIM>
01130     void SetPseudoEcgElectrodePositions(const std::vector<ChastePoint<SPACE_DIM> >& rPseudoEcgElectrodePositions);
01131 
01132 
01133     // Output visualization
01134 
01136     void EnsureOutputVisualizerExists(void);
01137 
01142     void SetVisualizeWithMeshalyzer(bool useMeshalyzer=true);
01143 
01148     void SetVisualizeWithCmgui(bool useCmgui=true);
01149 
01154     void SetVisualizeWithVtk(bool useVtk=true);
01155 
01160     void SetVisualizeWithParallelVtk(bool useParallelVtk=true);
01161 
01171     void SetElectrodeParameters( bool groundSecondElectrode,
01172                                  unsigned index, double magnitude,
01173                                  double startTime, double duration );
01174 
01181     void SetUseStateVariableInterpolation( bool useStateVariableInterpolation = true);
01182 
01188     void SetUseMassLumping(bool useMassLumping = true);
01189 
01195     void SetUseMassLumpingForPrecond(bool useMassLumping = true);
01196 
01205     void SetUseReactionDiffusionOperatorSplitting(bool useOperatorSplitting = true);
01206 
01213     void SetUseFixedNumberIterationsLinearSolver(bool useFixedNumberIterations = true, unsigned evaluateNumItsEveryNSolves=UINT_MAX);
01214 
01218     bool HasDrugDose() const;
01219 
01223     double GetDrugDose() const;
01224 
01228     void SetDrugDose(double drugDose);
01229 
01237     void SetIc50Value(const std::string& rCurrentName, double ic50, double hill=1.0);
01238 
01244     std::map<std::string, std::pair<double, double> > GetIc50Values();
01245 
01246 private:
01247     // Only to be accessed by the tests
01248     friend class TestHeartConfig;
01249 
01250     /*Constructor is private, since the class is only accessed by the singleton instance() method*/
01251     HeartConfig();
01252 
01256     boost::shared_ptr<cp::chaste_parameters_type> mpUserParameters;
01257 
01261     boost::shared_ptr<cp::chaste_parameters_type> mpDefaultParameters;
01262 
01264     static std::auto_ptr<HeartConfig> mpInstance;
01265 
01270     bool mUseFixedSchemaLocation;
01271 
01275     double mEpiFraction;
01276 
01280     double mEndoFraction;
01281 
01285     double mMidFraction;
01286 
01290     unsigned mIndexMid;
01291 
01295     unsigned mIndexEpi;
01296 
01300     unsigned mIndexEndo;
01301 
01305     bool mUserAskedForCellularTransmuralHeterogeneities;
01306 
01310     bool mUseMassLumping;
01311 
01315     bool mUseMassLumpingForPrecond;
01316 
01321     bool mUseReactionDiffusionOperatorSplitting;
01322 
01326     std::map<unsigned, double> mBathConductivities;
01327 
01331     std::set<unsigned> mTissueIdentifiers;
01332 
01336     std::set<unsigned> mBathIdentifiers;
01337 
01341     bool mUseFixedNumberIterations;
01342 
01348     unsigned mEvaluateNumItsEveryNSolves;
01349 
01359     template<class TYPE>
01360     TYPE* DecideLocation(TYPE* params_ptr, TYPE* defaults_ptr, const std::string& rNameParameter) const;
01361 
01371     void CheckSimulationIsDefined(std::string callingMethod="") const;
01372 
01382     void CheckResumeSimulationIsDefined(std::string callingMethod="") const;
01383 };
01384 
01385 
01386 BOOST_CLASS_VERSION(HeartConfig, 1)
01387 #include "SerializationExportWrapper.hpp"
01388 // Declare identifier for the serializer
01389 CHASTE_CLASS_EXPORT(HeartConfig)
01390 
01391 #endif /*HEARTCONFIG_HPP_*/

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5