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_2.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 <xercesc/dom/DOM.hpp>
00053 #include <xercesc/dom/DOMDocument.hpp>
00054 #include <xercesc/dom/DOMElement.hpp>
00055 
00056 #include <boost/shared_ptr.hpp>
00057 
00058 #include "ChasteSerialization.hpp"
00059 #include "ChasteSerializationVersion.hpp"
00060 #include <boost/serialization/split_member.hpp>
00061 
00062 namespace cp = chaste::parameters::v2_2;
00063 
00064 // Forward declaration to avoid circular includes
00065 class HeartFileFinder;
00066 
00067 
00078 class HeartConfig
00079 {
00080 private:
00087     void CheckTimeSteps() const;
00088 
00090     friend class boost::serialization::access;
00097     template<class Archive>
00098     void save(Archive & archive, const unsigned int version) const
00099     {
00100         //Only the Master should be writing the configuration file
00101         if (PetscTools::AmMaster())
00102         {
00103             mpInstance->Write( true );
00104         }
00105         PetscTools::Barrier("HeartConfig::save");
00106     }
00107 
00114     template<class Archive>
00115     void load(Archive & archive, const unsigned int version)
00116     {
00117         /*
00118          *  This method implements the logic required by HeartConfig to be able to handle resuming a simulation via the executable.
00119          *
00120          *  When the control reaches the method mpUserParameters and mpDefaultParameters point to the files specified as resuming parameters.
00121          *  However SetDefaultsFile() and SetParametersFile() will set those variables to point to the archived parameters.
00122          *
00123          *  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
00124          *  with the resuming parameters.
00125          */
00126         assert(mpUserParameters.use_count() > 0);
00127         boost::shared_ptr<cp::chaste_parameters_type> p_new_parameters = mpUserParameters;
00128 
00129         std::string defaults_filename_xml = ArchiveLocationInfo::GetArchiveDirectory() + "ChasteDefaults.xml";
00130         HeartConfig::Instance()->SetDefaultsFile(defaults_filename_xml);
00131 
00132         /*
00133          *  When we unarchive a simulation, we load the old parameters file in order to inherit things such
00134          *  as default cell model, stimuli, heterogeneities, ... This has the side effect of inheriting the
00135          *  <CheckpointSimulation> element (if defined).
00136          *
00137          *  We disable checkpointing definition coming from the unarchived config file. We will enable it again
00138          *  if defined in the resume config file.
00139          */
00140         std::string parameters_filename_xml = ArchiveLocationInfo::GetArchiveDirectory() + "ChasteParameters.xml";
00141         HeartConfig::Instance()->SetParametersFile(parameters_filename_xml);
00142 
00143         HeartConfig::Instance()->SetCheckpointSimulation(false);
00144 
00145         // If we are resuming a simulation, some parameters can be altered at this point.
00146         if (p_new_parameters->ResumeSimulation().present())
00147         {
00148             UpdateParametersFromResumeSimulation(p_new_parameters);
00149         }
00150     }
00151     BOOST_SERIALIZATION_SPLIT_MEMBER()
00152     
00153     
00159     void UpdateParametersFromResumeSimulation(boost::shared_ptr<cp::chaste_parameters_type> pResumeParameters);
00160 
00161 public:
00166     typedef std::map<std::string, std::string> SchemaLocationsMap;
00167 
00168 private:
00172     SchemaLocationsMap mSchemaLocations;
00173 
00177     void SetDefaultSchemaLocations();
00178 
00186     std::string EscapeSpaces(const std::string& rPath);
00187 
00188 public:
00194     static HeartConfig* Instance();
00195 
00201     void SetUseFixedSchemaLocation(bool useFixedSchemaLocation);
00202 
00209     void SetFixedSchemaLocations(const SchemaLocationsMap& rSchemaLocations);
00210 
00215     void SetDefaultsFile(const std::string& rFileName);
00216     
00221     void SetParametersFile(const std::string& rFileName);
00222     
00234     void Write(bool useArchiveLocationInfo=false, std::string subfolderName="output");
00235 
00241     void CopySchema(const std::string& rToDirectory);
00242 
00247     boost::shared_ptr<cp::chaste_parameters_type> ReadFile(const std::string& rFileName);
00248 
00253     static void Reset();
00254     
00255     ~HeartConfig(); 
00263     unsigned GetVersionFromNamespace(const std::string& rNamespaceUri);
00264 
00266     //
00267     //  Get methods
00268     //
00270 
00271     // Methods for asking the configuration file about which sections are defined.
00272 
00278     bool IsSimulationDefined() const;
00279 
00285     bool IsSimulationResumed() const;
00286 
00287     // Simulation
00288     unsigned GetSpaceDimension() const; 
00289     double GetSimulationDuration() const; 
00296     cp::domain_type GetDomain() const;
00297 
00305     cp::ionic_model_selection_type GetDefaultIonicModel() const;
00306 
00317     template<unsigned DIM>
00318     void GetIonicModelRegions(std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rDefinedRegions,
00319                                std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00320 
00332     void SetIonicModelRegions(std::vector<ChasteCuboid<3> >& rDefinedRegions,
00333                               std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00334 
00335     bool IsMeshProvided() const; 
00336     bool GetCreateMesh() const; 
00337     bool GetCreateSlab() const; 
00338     bool GetCreateSheet() const; 
00339     bool GetCreateFibre() const; 
00340     bool GetLoadMesh() const; 
00345     void GetSlabDimensions(c_vector<double, 3>& slabDimensions) const;
00349     void GetSheetDimensions(c_vector<double, 2>& sheetDimensions) const;
00353     void GetFibreLength(c_vector<double, 1>& fibreLength) const;
00354     double GetInterNodeSpace() const; 
00356     std::string GetMeshName() const;
00358     cp::media_type GetConductivityMedia() const;
00369      template<unsigned DIM>
00370     void GetStimuli(std::vector<boost::shared_ptr<AbstractStimulusFunction> >& rStimuliApplied, 
00371                     std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rStimulatedAreas) const;
00372 
00388     template<unsigned DIM>
00389     void GetCellHeterogeneities( std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& rCellHeterogeneityRegions,
00390                                  std::vector<double>& rScaleFactorGks,
00391                                  std::vector<double>& rScaleFactorIto,
00392                                  std::vector<double>& rScaleFactorGkr,
00393                                  std::vector<std::map<std::string, double> >* pParameterSettings);
00394 
00395     bool GetConductivityHeterogeneitiesProvided() const; 
00400     bool AreCellularTransmuralHeterogeneitiesRequested();
00401 
00405     double GetEpiLayerFraction();
00406 
00410     double GetEndoLayerFraction();
00411 
00415     double GetMidLayerFraction();
00416 
00420     unsigned GetEpiLayerIndex();
00421 
00425     unsigned GetEndoLayerIndex();
00426 
00430     unsigned GetMidLayerIndex();
00431 
00432 
00441     template<unsigned DIM>
00442     void GetConductivityHeterogeneities(std::vector<boost::shared_ptr<AbstractChasteRegion<DIM> > >& conductivitiesHeterogeneityAreas,
00443                                         std::vector< c_vector<double,3> >& intraConductivities,
00444                                         std::vector< c_vector<double,3> >& extraConductivities) const;
00445     std::string GetOutputDirectory() const; 
00457     std::string GetOutputFilenamePrefix() const;
00458 
00462     bool GetOutputVariablesProvided() const;
00463 
00470     void GetOutputVariables(std::vector<std::string>& rOutputVariables) const;
00471     
00477     bool GetOutputUsingOriginalNodeOrdering();
00478 
00484     bool GetCheckpointSimulation() const;
00485 
00491     double GetCheckpointTimestep() const;
00492 
00498     unsigned GetMaxCheckpointsOnDisk() const;
00499 
00500     // ResumeSimulation
00504     HeartFileFinder GetArchivedSimulationDir() const;
00505 
00506 
00507     // Physiological
00512     void GetIntracellularConductivities(c_vector<double, 3>& intraConductivities) const;
00517     void GetIntracellularConductivities(c_vector<double, 2>& intraConductivities) const;
00522     void GetIntracellularConductivities(c_vector<double, 1>& intraConductivities) const;
00523 
00528     void GetExtracellularConductivities(c_vector<double, 3>& extraConductivities) const;
00533     void GetExtracellularConductivities(c_vector<double, 2>& extraConductivities) const;
00538     void GetExtracellularConductivities(c_vector<double, 1>& extraConductivities) const;
00539 
00547     double GetBathConductivity(unsigned bathRegion=UINT_MAX) const; 
00548 
00554     const std::set<unsigned>& rGetTissueIdentifiers();
00555 
00561     const std::set<unsigned>& rGetBathIdentifiers();
00562 
00563 
00564     double GetSurfaceAreaToVolumeRatio() const; 
00566     double GetCapacitance() const; 
00568     // Numerical
00569     double GetOdeTimeStep() const; 
00570     double GetPdeTimeStep() const; 
00571     double GetPrintingTimeStep() const; 
00573     bool GetUseAbsoluteTolerance() const; 
00574     double GetAbsoluteTolerance() const; 
00576     bool GetUseRelativeTolerance() const; 
00577     double GetRelativeTolerance() const;  
00579     const char* GetKSPSolver() const; 
00580     const char* GetKSPPreconditioner() const; 
00582     DistributedTetrahedralMeshPartitionType::type GetMeshPartitioning() const; 
00584     // Adaptivity
00588     bool IsAdaptivityParametersPresent() const;
00589 
00593     double GetTargetErrorForAdaptivity() const;
00594 
00598     double GetSigmaForAdaptivity() const;
00599 
00603     double GetMaxEdgeLengthForAdaptivity() const;
00604 
00608     double GetMinEdgeLengthForAdaptivity() const;
00609 
00613     double GetGradationForAdaptivity() const;
00614 
00618     unsigned GetMaxNodesForAdaptivity() const;
00619 
00623     unsigned GetNumberOfAdaptiveSweeps() const;
00624 
00625     // Post processing
00629     bool IsPostProcessingSectionPresent() const;
00630     
00634     void EnsurePostProcessingSectionPresent();
00635 
00639     bool IsPostProcessingRequested() const;
00640 
00644     bool IsApdMapsRequested() const;
00650     void GetApdMaps(std::vector<std::pair<double,double> >& rApdMaps) const;
00651 
00655     bool IsUpstrokeTimeMapsRequested() const;
00660     void GetUpstrokeTimeMaps (std::vector<double>& rUpstrokeTimeMaps) const;
00661 
00665     bool IsMaxUpstrokeVelocityMapRequested() const;
00666 
00671     void GetMaxUpstrokeVelocityMaps(std::vector<double>& rUpstrokeVelocityMaps) const;
00672 
00676     bool IsConductionVelocityMapsRequested() const;
00677 
00682     void GetConductionVelocityMaps(std::vector<unsigned>& rConductionVelocityMaps) const;
00683 
00687     bool IsAnyNodalTimeTraceRequested() const;
00688 
00692     void GetNodalTimeTraceRequested(std::vector<unsigned>& rRequestedNodes) const;
00693     
00697     bool IsPseudoEcgCalculationRequested() const;
00698     
00703     template<unsigned SPACE_DIM>
00704     void GetPseudoEcgElectrodePositions(std::vector<ChastePoint<SPACE_DIM> >& rPseudoEcgElectrodePositions) const;
00705     
00709     bool GetUseStateVariableInterpolation() const;
00710 
00711 
00712     // Output visualization
00713 
00715     bool IsOutputVisualizerPresent() const;
00716 
00718     bool GetVisualizeWithMeshalyzer() const;
00719 
00721     bool GetVisualizeWithCmgui() const;
00722 
00724     bool GetVisualizeWithVtk() const;
00725      
00727     bool GetVisualizeWithParallelVtk() const;
00728     
00732     bool IsElectrodesPresent() const;    
00733 
00734 
00744     void GetElectrodeParameters(bool& rGroundSecondElectrode,
00745                                 unsigned& rIndex, double& rMagnitude,
00746                                 double& rStartTime, double& rDuration );
00747 
00751     bool GetUseMassLumping();
00752 
00756     bool GetUseMassLumpingForPrecond();
00757 
00762     bool GetUseReactionDiffusionOperatorSplitting();
00763     
00767     bool GetUseFixedNumberIterationsLinearSolver();
00768 
00772     unsigned GetEvaluateNumItsEveryNSolves();
00773     
00774 
00776     //
00777     //  Set methods
00778     //
00780 
00781     // Simulation
00785     void SetSpaceDimension(unsigned spaceDimension);
00786 
00790     void SetSimulationDuration(double simulationDuration);
00791 
00798     void SetDomain(const cp::domain_type& rDomain);
00799 
00807     void SetDefaultIonicModel(const cp::ionic_models_available_type& rIonicModel);
00808 
00816     void SetSlabDimensions(double x, double y, double z, double inter_node_space);
00823     void SetSheetDimensions(double x, double y, double inter_node_space);
00829     void SetFibreLength(double x, double inter_node_space);
00830 
00837     void SetMeshFileName(std::string meshPrefix, cp::media_type fibreDefinition=cp::media_type::NoFibreOrientation);
00838 
00846     void SetConductivityHeterogeneities(std::vector<ChasteCuboid<3> >& rConductivityAreas,
00847                                         std::vector< c_vector<double,3> >& rIntraConductivities,
00848                                         std::vector< c_vector<double,3> >& rExtraConductivities);
00856     void SetConductivityHeterogeneitiesEllipsoid(std::vector<ChasteEllipsoid<3> >& conductivityAreas,
00857             std::vector< c_vector<double,3> >& intraConductivities,
00858             std::vector< c_vector<double,3> >& extraConductivities);
00862     void SetOutputDirectory(const std::string& rOutputDirectory);
00873     void SetOutputFilenamePrefix(const std::string& rOutputFilenamePrefix);
00874 
00886     void SetOutputVariables(const std::vector<std::string>& rOutputVariables);
00887 
00896     void SetOutputUsingOriginalNodeOrdering(bool useOriginal);
00897 
00905      void SetCheckpointSimulation(bool checkpointSimulation, double checkpointTimestep=-1.0, unsigned maxCheckpointsOnDisk=UINT_MAX);
00906 
00907 
00908     // Physiological
00913     void SetIntracellularConductivities(const c_vector<double, 3>& rIntraConductivities);
00918     void SetIntracellularConductivities(const c_vector<double, 2>& rIntraConductivities);
00923     void SetIntracellularConductivities(const c_vector<double, 1>& rIntraConductivities);
00924 
00929     void SetExtracellularConductivities(const c_vector<double, 3>& rExtraConductivities);
00934     void SetExtracellularConductivities(const c_vector<double, 2>& rExtraConductivities);
00939     void SetExtracellularConductivities(const c_vector<double, 1>& rExtraConductivities);
00940 
00946     void SetBathConductivity(double bathConductivity);
00947 
00953     void SetBathMultipleConductivities(std::map<unsigned, double> bathConductivities);
00954 
00961     void SetTissueAndBathIdentifiers(const std::set<unsigned>& tissueIds, const std::set<unsigned>& bathIds);
00962 
00969     //void SetTissueIdentifiers(const std::set<unsigned>& tissueIds);
00970 
00975     void SetSurfaceAreaToVolumeRatio(double ratio);
00976 
00981     void SetCapacitance(double capacitance);
00982 
00983     // Numerical
00990     void SetOdePdeAndPrintingTimeSteps(double odeTimeStep, double pdeTimeStep, double printingTimeStep);
00991     
00996     void SetOdeTimeStep(double odeTimeStep);
00997     
01002     void SetPdeTimeStep(double pdeTimeStep);
01003 
01008      void SetPrintingTimeStep(double printingTimeStep);
01009 
01013     void SetUseRelativeTolerance(double relativeTolerance);
01014     
01018     void SetUseAbsoluteTolerance(double absoluteTolerance);
01019 
01023     void SetKSPSolver(const char* kspSolver);
01024     
01028     void SetKSPPreconditioner(const char* kspPreconditioner);
01029 
01033     void SetMeshPartitioning(const char* meshPartioningMethod);
01034 
01046     void SetAdaptivityParameters(double targetError, double sigma, double maxEdgeLength, double minEdgeLength,
01047                                  double gradation, unsigned maxNodes, unsigned numSweeps );
01048 
01054     void SetTargetErrorForAdaptivity(double targetError);
01055 
01061     void SetSigmaForAdaptivity(double sigma);
01062 
01068     void SetMaxEdgeLengthForAdaptivity(double maxEdgeLength);
01069 
01075     void SetMinEdgeLengthForAdaptivity(double minEdgeLength);
01076 
01082     void SetGradationForAdaptivity(double gradation);
01083 
01089     void SetMaxNodesForAdaptivity(unsigned maxNodes);
01090 
01096     void SetNumberOfAdaptiveSweeps(unsigned numSweeps);
01097 
01104     void SetApdMaps(const std::vector<std::pair<double,double> >& rApdMaps);
01105 
01111     void SetUpstrokeTimeMaps (std::vector<double>& rUpstrokeTimeMaps);
01112 
01118     void SetMaxUpstrokeVelocityMaps (std::vector<double>& rMaxUpstrokeVelocityMaps);
01119 
01124     void SetConductionVelocityMaps (std::vector<unsigned>& rConductionVelocityMaps);
01125 
01126 
01133     void SetRequestedNodalTimeTraces (std::vector<unsigned>& requestedNodes);
01134 
01140     template<unsigned SPACE_DIM>
01141     void SetPseudoEcgElectrodePositions(const std::vector<ChastePoint<SPACE_DIM> >& rPseudoEcgElectrodePositions);
01142 
01143 
01144     // Output visualization
01145 
01147     void EnsureOutputVisualizerExists(void);
01148 
01153     void SetVisualizeWithMeshalyzer(bool useMeshalyzer=true);
01154 
01159     void SetVisualizeWithCmgui(bool useCmgui=true);
01160 
01165     void SetVisualizeWithVtk(bool useVtk=true);
01166     
01171     void SetVisualizeWithParallelVtk(bool useParallelVtk=true);
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 
01226 
01227 private:
01228     // Only to be accessed by the tests
01229     friend class TestHeartConfig;
01230 
01231     /*Constructor is private, since the class is only accessed by the singleton instance() method*/
01232     HeartConfig();
01233 
01237     boost::shared_ptr<cp::chaste_parameters_type> mpUserParameters;
01241     boost::shared_ptr<cp::chaste_parameters_type> mpDefaultParameters;
01242 
01244     static std::auto_ptr<HeartConfig> mpInstance;
01245 
01250     bool mUseFixedSchemaLocation;
01251 
01255     double mEpiFraction;
01256 
01260     double mEndoFraction;
01261 
01265     double mMidFraction;
01266 
01270     unsigned mIndexMid;
01271 
01275     unsigned mIndexEpi;
01276 
01280     unsigned mIndexEndo;
01281 
01285     bool mUserAskedForCellularTransmuralHeterogeneities;
01286 
01290     bool mUseMassLumping;
01291 
01295     bool mUseMassLumpingForPrecond;
01296 
01301     bool mUseReactionDiffusionOperatorSplitting;
01302 
01306     std::map<unsigned, double> mBathConductivities;
01307     
01311     std::set<unsigned> mTissueIdentifiers;
01312     
01316     std::set<unsigned> mBathIdentifiers;
01317     
01321     bool mUseFixedNumberIterations;
01322     
01328     unsigned mEvaluateNumItsEveryNSolves;
01329         
01339     template<class TYPE>
01340     TYPE* DecideLocation(TYPE* params_ptr, TYPE* defaults_ptr, const std::string& rNameParameter) const;
01341 
01351     void CheckSimulationIsDefined(std::string callingMethod="") const;
01352 
01362     void CheckResumeSimulationIsDefined(std::string callingMethod="") const;
01363 };
01364 
01365 
01366 BOOST_CLASS_VERSION(HeartConfig, 1) 
01367 #include "SerializationExportWrapper.hpp"
01368 // Declare identifier for the serializer
01369 CHASTE_CLASS_EXPORT(HeartConfig)
01370 
01371 #endif /*HEARTCONFIG_HPP_*/

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5