Chaste Release::3.1
HeartConfig.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 
00037 #ifndef HEARTCONFIG_HPP_
00038 #define HEARTCONFIG_HPP_
00039 
00040 #include <string>
00041 #include <vector>
00042 #include <set>
00043 
00044 #include "UblasIncludes.hpp"
00045 
00046 #include "ArchiveLocationInfo.hpp"
00047 #include "ChasteParameters_3_1.hpp"
00048 
00049 #include "AbstractStimulusFunction.hpp"
00050 // These are needed here for Boost < 1.37
00051 #include "SimpleStimulus.hpp"
00052 #include "RegularStimulus.hpp"
00053 
00054 #include "ChastePoint.hpp"
00055 #include "ChasteCuboid.hpp"
00056 #include "ChasteEllipsoid.hpp"
00057 #include "DistributedTetrahedralMeshPartitionType.hpp"
00058 
00059 #include <boost/shared_ptr.hpp>
00060 
00061 #include "ChasteSerialization.hpp"
00062 #include "ChasteSerializationVersion.hpp"
00063 #include <boost/serialization/split_member.hpp>
00064 
00065 namespace cp = chaste::parameters::v3_1;
00066 
00067 // Forward declaration to avoid circular includes
00068 class HeartFileFinder;
00069 
00070 
00081 class HeartConfig
00082 {
00083 private:
00090     void CheckTimeSteps() const;
00091 
00093     friend class boost::serialization::access;
00100     template<class Archive>
00101     void save(Archive & archive, const unsigned int version) const
00102     {
00103         //Only the Master should be writing the configuration file
00104         if (PetscTools::AmMaster())
00105         {
00106             mpInstance->Write( true );
00107         }
00108         PetscTools::Barrier("HeartConfig::save");
00109     }
00110 
00117     template<class Archive>
00118     void load(Archive & archive, const unsigned int version)
00119     {
00120         LoadFromCheckpoint();
00121     }
00122     BOOST_SERIALIZATION_SPLIT_MEMBER()
00123 
00124     
00128     void LoadFromCheckpoint();
00129 
00136     void UpdateParametersFromResumeSimulation(boost::shared_ptr<cp::chaste_parameters_type> pResumeParameters);
00137 
00138 public:
00143     typedef std::map<std::string, std::string> SchemaLocationsMap;
00144 
00145 private:
00149     SchemaLocationsMap mSchemaLocations;
00150 
00154     void SetDefaultSchemaLocations();
00155 
00156 public:
00162     static HeartConfig* Instance();
00163 
00169     void SetUseFixedSchemaLocation(bool useFixedSchemaLocation);
00170 
00177     void SetFixedSchemaLocations(const SchemaLocationsMap& rSchemaLocations);
00178 
00183     void SetParametersFile(const std::string& rFileName);
00184 
00196     void Write(bool useArchiveLocationInfo=false, std::string subfolderName="output");
00197 
00203     void CopySchema(const std::string& rToDirectory);
00204 
00209     boost::shared_ptr<cp::chaste_parameters_type> ReadFile(const std::string& rFileName);
00210 
00215     static void Reset();
00216 
00217     ~HeartConfig(); 
00225     unsigned GetVersionFromNamespace(const std::string& rNamespaceUri);
00226 
00228     //
00229     //  Get methods
00230     //
00232 
00237     FileFinder GetParametersFilePath();
00238 
00239     // Methods for asking the configuration file about which sections are defined.
00240 
00246     bool IsSimulationDefined() const;
00247 
00253     bool IsSimulationResumed() const;
00254 
00255     // Simulation
00256     unsigned GetSpaceDimension() const; 
00257     double GetSimulationDuration() const; 
00264     cp::domain_type GetDomain() const;
00265 
00273     cp::ionic_model_selection_type GetDefaultIonicModel() const;
00274 
00285     template<unsigned DIM>
00286     void GetIonicModelRegions(std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rDefinedRegions,
00287                                std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00288 
00300     void SetIonicModelRegions(std::vector<ChasteCuboid<3> >& rDefinedRegions,
00301                               std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00302 
00303     bool IsMeshProvided() const; 
00304     bool GetCreateMesh() const; 
00305     bool GetCreateSlab() const; 
00306     bool GetCreateSheet() const; 
00307     bool GetCreateFibre() const; 
00308     bool GetLoadMesh() const; 
00313     void GetSlabDimensions(c_vector<double, 3>& slabDimensions) const;
00317     void GetSheetDimensions(c_vector<double, 2>& sheetDimensions) const;
00321     void GetFibreLength(c_vector<double, 1>& fibreLength) const;
00322     double GetInterNodeSpace() const; 
00324     std::string GetMeshName() const;
00326     cp::media_type GetConductivityMedia() const;
00337     template<unsigned DIM>
00338     void GetStimuli(std::vector<boost::shared_ptr<AbstractStimulusFunction> >& rStimuliApplied,
00339                     std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rStimulatedAreas) const;
00340 
00356     template<unsigned DIM>
00357     void GetCellHeterogeneities( std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rCellHeterogeneityRegions,
00358                                  std::vector<double>& rScaleFactorGks,
00359                                  std::vector<double>& rScaleFactorIto,
00360                                  std::vector<double>& rScaleFactorGkr,
00361                                  std::vector<std::map<std::string, double> >* pParameterSettings);
00362 
00363     bool GetConductivityHeterogeneitiesProvided() const; 
00368     bool AreCellularTransmuralHeterogeneitiesRequested();
00369 
00373     double GetEpiLayerFraction();
00374 
00378     double GetEndoLayerFraction();
00379 
00383     double GetMidLayerFraction();
00384 
00388     unsigned GetEpiLayerIndex();
00389 
00393     unsigned GetEndoLayerIndex();
00394 
00398     unsigned GetMidLayerIndex();
00399 
00400 
00409     template<unsigned DIM>
00410     void GetConductivityHeterogeneities(std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& conductivitiesHeterogeneityAreas,
00411                                         std::vector< c_vector<double,3> >& intraConductivities,
00412                                         std::vector< c_vector<double,3> >& extraConductivities) const;
00413     std::string GetOutputDirectory() const; 
00425     std::string GetOutputFilenamePrefix() const;
00426 
00430     bool GetOutputVariablesProvided() const;
00431 
00438     void GetOutputVariables(std::vector<std::string>& rOutputVariables) const;
00439 
00445     bool GetOutputUsingOriginalNodeOrdering();
00446 
00452     bool GetCheckpointSimulation() const;
00453 
00459     double GetCheckpointTimestep() const;
00460 
00466     unsigned GetMaxCheckpointsOnDisk() const;
00467 
00468     // ResumeSimulation
00472     HeartFileFinder GetArchivedSimulationDir() const;
00473 
00474 
00475     // Physiological
00480     void GetIntracellularConductivities(c_vector<double, 3>& intraConductivities) const;
00485     void GetIntracellularConductivities(c_vector<double, 2>& intraConductivities) const;
00490     void GetIntracellularConductivities(c_vector<double, 1>& intraConductivities) const;
00491 
00496     void GetExtracellularConductivities(c_vector<double, 3>& extraConductivities) const;
00501     void GetExtracellularConductivities(c_vector<double, 2>& extraConductivities) const;
00506     void GetExtracellularConductivities(c_vector<double, 1>& extraConductivities) const;
00507 
00515     double GetBathConductivity(unsigned bathRegion=UINT_MAX) const;
00516 
00522     const std::set<unsigned>& rGetTissueIdentifiers();
00523 
00529     const std::set<unsigned>& rGetBathIdentifiers();
00530 
00531 
00532     double GetSurfaceAreaToVolumeRatio() const; 
00534     double GetCapacitance() const; 
00536     // Numerical
00537     double GetOdeTimeStep() const; 
00538     double GetPdeTimeStep() const; 
00539     double GetPrintingTimeStep() const; 
00541     bool GetUseAbsoluteTolerance() const; 
00542     double GetAbsoluteTolerance() const; 
00544     bool GetUseRelativeTolerance() const; 
00545     double GetRelativeTolerance() const;  
00547     const char* GetKSPSolver() const; 
00548     const char* GetKSPPreconditioner() const; 
00550     DistributedTetrahedralMeshPartitionType::type GetMeshPartitioning() const; 
00552     // Adaptivity
00556     bool IsAdaptivityParametersPresent() const;
00557 
00561     double GetTargetErrorForAdaptivity() const;
00562 
00566     double GetSigmaForAdaptivity() const;
00567 
00571     double GetMaxEdgeLengthForAdaptivity() const;
00572 
00576     double GetMinEdgeLengthForAdaptivity() const;
00577 
00581     double GetGradationForAdaptivity() const;
00582 
00586     unsigned GetMaxNodesForAdaptivity() const;
00587 
00591     unsigned GetNumberOfAdaptiveSweeps() const;
00592 
00593     // Post processing
00597     bool IsPostProcessingSectionPresent() const;
00598 
00602     void EnsurePostProcessingSectionPresent();
00603 
00607     bool IsPostProcessingRequested() const;
00608 
00612     bool IsApdMapsRequested() const;
00613 
00619     void GetApdMaps(std::vector<std::pair<double,double> >& rApdMaps) const;
00620 
00624     bool IsUpstrokeTimeMapsRequested() const;
00629     void GetUpstrokeTimeMaps (std::vector<double>& rUpstrokeTimeMaps) const;
00630 
00634     bool IsMaxUpstrokeVelocityMapRequested() const;
00635 
00640     void GetMaxUpstrokeVelocityMaps(std::vector<double>& rUpstrokeVelocityMaps) const;
00641 
00645     bool IsConductionVelocityMapsRequested() const;
00646 
00651     void GetConductionVelocityMaps(std::vector<unsigned>& rConductionVelocityMaps) const;
00652 
00656     bool IsAnyNodalTimeTraceRequested() const;
00657 
00661     void GetNodalTimeTraceRequested(std::vector<unsigned>& rRequestedNodes) const;
00662 
00666     bool IsPseudoEcgCalculationRequested() const;
00667 
00672     template<unsigned SPACE_DIM>
00673     void GetPseudoEcgElectrodePositions(std::vector<ChastePoint<SPACE_DIM> >& rPseudoEcgElectrodePositions) const;
00674 
00678     bool GetUseStateVariableInterpolation() const;
00679 
00680 
00681     // Output visualization
00682 
00684     bool IsOutputVisualizerPresent() const;
00685 
00687     bool GetVisualizeWithMeshalyzer() const;
00688 
00690     bool GetVisualizeWithCmgui() const;
00691 
00693     bool GetVisualizeWithVtk() const;
00694 
00696     bool GetVisualizeWithParallelVtk() const;
00697 
00699      unsigned GetVisualizerOutputPrecision();
00700 
00701 
00705     bool IsElectrodesPresent() const;
00706 
00707 
00717     void GetElectrodeParameters(bool& rGroundSecondElectrode,
00718                                 unsigned& rIndex, double& rMagnitude,
00719                                 double& rStartTime, double& rDuration );
00720 
00724     bool GetUseMassLumping();
00725 
00729     bool GetUseMassLumpingForPrecond();
00730 
00735     bool GetUseReactionDiffusionOperatorSplitting();
00736 
00740     bool GetUseFixedNumberIterationsLinearSolver();
00741 
00745     unsigned GetEvaluateNumItsEveryNSolves();
00746 
00747 
00749     //
00750     //  Set methods
00751     //
00753 
00754     // Simulation
00758     void SetSpaceDimension(unsigned spaceDimension);
00759 
00764     void SetSimulationDuration(double simulationDuration);
00765 
00772     void SetDomain(const cp::domain_type& rDomain);
00773 
00781     void SetDefaultIonicModel(const cp::ionic_models_available_type& rIonicModel);
00782 
00790     void SetSlabDimensions(double x, double y, double z, double inter_node_space);
00797     void SetSheetDimensions(double x, double y, double inter_node_space);
00803     void SetFibreLength(double x, double inter_node_space);
00804 
00811     void SetMeshFileName(std::string meshPrefix, cp::media_type fibreDefinition=cp::media_type::NoFibreOrientation);
00812 
00820     void SetConductivityHeterogeneities(std::vector<ChasteCuboid<3> >& rConductivityAreas,
00821                                         std::vector< c_vector<double,3> >& rIntraConductivities,
00822                                         std::vector< c_vector<double,3> >& rExtraConductivities);
00830     void SetConductivityHeterogeneitiesEllipsoid(std::vector<ChasteEllipsoid<3> >& conductivityAreas,
00831             std::vector< c_vector<double,3> >& intraConductivities,
00832             std::vector< c_vector<double,3> >& extraConductivities);
00836     void SetOutputDirectory(const std::string& rOutputDirectory);
00847     void SetOutputFilenamePrefix(const std::string& rOutputFilenamePrefix);
00848 
00855     void SetOutputVariables(const std::vector<std::string>& rOutputVariables);
00856 
00865     void SetOutputUsingOriginalNodeOrdering(bool useOriginal);
00866 
00874      void SetCheckpointSimulation(bool checkpointSimulation, double checkpointTimestep=-1.0, unsigned maxCheckpointsOnDisk=UINT_MAX);
00875 
00876     // Physiological
00881     void SetIntracellularConductivities(const c_vector<double, 3>& rIntraConductivities);
00886     void SetIntracellularConductivities(const c_vector<double, 2>& rIntraConductivities);
00891     void SetIntracellularConductivities(const c_vector<double, 1>& rIntraConductivities);
00892 
00897     void SetExtracellularConductivities(const c_vector<double, 3>& rExtraConductivities);
00902     void SetExtracellularConductivities(const c_vector<double, 2>& rExtraConductivities);
00907     void SetExtracellularConductivities(const c_vector<double, 1>& rExtraConductivities);
00908 
00914     void SetBathConductivity(double bathConductivity);
00915 
00921     void SetBathMultipleConductivities(std::map<unsigned, double> bathConductivities);
00922 
00929     void SetTissueAndBathIdentifiers(const std::set<unsigned>& tissueIds, const std::set<unsigned>& bathIds);
00930 
00937     //void SetTissueIdentifiers(const std::set<unsigned>& tissueIds);
00938 
00943     void SetSurfaceAreaToVolumeRatio(double ratio);
00944 
00949     void SetCapacitance(double capacitance);
00950 
00951     // Numerical
00975     void SetOdePdeAndPrintingTimeSteps(double odeTimeStep, double pdeTimeStep, double printingTimeStep);
00976 
00989     void SetOdeTimeStep(double odeTimeStep);
00990 
00995     void SetPdeTimeStep(double pdeTimeStep);
00996 
01001      void SetPrintingTimeStep(double printingTimeStep);
01002 
01006     void SetUseRelativeTolerance(double relativeTolerance);
01007 
01011     void SetUseAbsoluteTolerance(double absoluteTolerance);
01012 
01016     void SetKSPSolver(const char* kspSolver);
01017 
01021     void SetKSPPreconditioner(const char* kspPreconditioner);
01022 
01026     void SetMeshPartitioning(const char* meshPartioningMethod);
01027 
01039     void SetAdaptivityParameters(double targetError, double sigma, double maxEdgeLength, double minEdgeLength,
01040                                  double gradation, unsigned maxNodes, unsigned numSweeps );
01041 
01047     void SetTargetErrorForAdaptivity(double targetError);
01048 
01054     void SetSigmaForAdaptivity(double sigma);
01055 
01061     void SetMaxEdgeLengthForAdaptivity(double maxEdgeLength);
01062 
01068     void SetMinEdgeLengthForAdaptivity(double minEdgeLength);
01069 
01075     void SetGradationForAdaptivity(double gradation);
01076 
01082     void SetMaxNodesForAdaptivity(unsigned maxNodes);
01083 
01089     void SetNumberOfAdaptiveSweeps(unsigned numSweeps);
01090 
01097     void SetApdMaps(const std::vector<std::pair<double,double> >& rApdMaps);
01098 
01104     void SetUpstrokeTimeMaps(std::vector<double>& rUpstrokeTimeMaps);
01105 
01111     void SetMaxUpstrokeVelocityMaps(std::vector<double>& rMaxUpstrokeVelocityMaps);
01112 
01117     void SetConductionVelocityMaps(std::vector<unsigned>& rConductionVelocityMaps);
01118 
01125     void SetRequestedNodalTimeTraces(std::vector<unsigned>& requestedNodes);
01126 
01132     template<unsigned SPACE_DIM>
01133     void SetPseudoEcgElectrodePositions(const std::vector<ChastePoint<SPACE_DIM> >& rPseudoEcgElectrodePositions);
01134 
01135 
01136     // Output visualization
01137 
01139     void EnsureOutputVisualizerExists(void);
01140 
01145     void SetVisualizeWithMeshalyzer(bool useMeshalyzer=true);
01146 
01151     void SetVisualizeWithCmgui(bool useCmgui=true);
01152 
01157     void SetVisualizeWithVtk(bool useVtk=true);
01158 
01163     void SetVisualizeWithParallelVtk(bool useParallelVtk=true);
01164 
01171     void SetVisualizerOutputPrecision(unsigned numberOfDigits);
01172 
01182     void SetElectrodeParameters( bool groundSecondElectrode,
01183                                  unsigned index, double magnitude,
01184                                  double startTime, double duration );
01185 
01192     void SetUseStateVariableInterpolation( bool useStateVariableInterpolation = true);
01193 
01199     void SetUseMassLumping(bool useMassLumping = true);
01200 
01206     void SetUseMassLumpingForPrecond(bool useMassLumping = true);
01207 
01216     void SetUseReactionDiffusionOperatorSplitting(bool useOperatorSplitting = true);
01217 
01224     void SetUseFixedNumberIterationsLinearSolver(bool useFixedNumberIterations = true, unsigned evaluateNumItsEveryNSolves=UINT_MAX);
01225 
01229     bool HasDrugDose() const;
01230 
01234     double GetDrugDose() const;
01235 
01239     void SetDrugDose(double drugDose);
01240 
01248     void SetIc50Value(const std::string& rCurrentName, double ic50, double hill=1.0);
01249 
01255     std::map<std::string, std::pair<double, double> > GetIc50Values();
01256 
01257     //
01258     // Purkinje-related methods
01259     //
01260 
01264     bool HasPurkinje();
01265 
01269     double GetPurkinjeCapacitance();
01270 
01275     void SetPurkinjeCapacitance(double capacitance);
01276 
01280     double GetPurkinjeSurfaceAreaToVolumeRatio();
01281 
01286     void SetPurkinjeSurfaceAreaToVolumeRatio(double ratio);
01287 
01291     double GetPurkinjeConductivity();
01292 
01297     void SetPurkinjeConductivity(double conductivity);
01298 
01299 private:
01300     // Only to be accessed by the tests
01301     friend class TestHeartConfig;
01302 
01303     /*Constructor is private, since the class is only accessed by the singleton instance() method*/
01304     HeartConfig();
01305 
01307     boost::shared_ptr<cp::chaste_parameters_type> mpParameters;
01308 
01310     static std::auto_ptr<HeartConfig> mpInstance;
01311 
01315     FileFinder mParametersFilePath;
01316 
01321     bool mUseFixedSchemaLocation;
01322 
01326     double mEpiFraction;
01327 
01331     double mEndoFraction;
01332 
01336     double mMidFraction;
01337 
01341     unsigned mIndexMid;
01342 
01346     unsigned mIndexEpi;
01347 
01351     unsigned mIndexEndo;
01352 
01356     bool mUserAskedForCellularTransmuralHeterogeneities;
01357 
01361     bool mUseMassLumping;
01362 
01366     bool mUseMassLumpingForPrecond;
01367 
01372     bool mUseReactionDiffusionOperatorSplitting;
01373 
01377     std::map<unsigned, double> mBathConductivities;
01378 
01382     std::set<unsigned> mTissueIdentifiers;
01383 
01387     std::set<unsigned> mBathIdentifiers;
01388 
01392     bool mUseFixedNumberIterations;
01393 
01399     unsigned mEvaluateNumItsEveryNSolves;
01400 
01410     void CheckSimulationIsDefined(std::string callingMethod="") const;
01411 
01421     void CheckResumeSimulationIsDefined(std::string callingMethod="") const;
01422 };
01423 
01424 
01425 BOOST_CLASS_VERSION(HeartConfig, 1)
01426 #include "SerializationExportWrapper.hpp"
01427 // Declare identifier for the serializer
01428 CHASTE_CLASS_EXPORT(HeartConfig)
01429 
01430 #endif /*HEARTCONFIG_HPP_*/