HeartConfig.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 
00036 #include "UblasIncludes.hpp"
00037 
00038 #include "ArchiveLocationInfo.hpp"
00039 //#include "ChasteParameters_1_1.hpp"
00040 #include "ChasteParameters_2_0.hpp"
00041 #include "SimpleStimulus.hpp"
00042 #include "ChasteCuboid.hpp"
00043 #include "AbstractTetrahedralMesh.hpp"
00044 
00045 #include <xercesc/dom/DOM.hpp>
00046 #include <xercesc/dom/DOMDocument.hpp>
00047 #include <xercesc/dom/DOMElement.hpp>
00048 
00049 
00050 #include <boost/shared_ptr.hpp>
00051 
00052 #include "ChasteSerialization.hpp"
00053 #include <boost/serialization/split_member.hpp>
00054 
00055 namespace cp = chaste::parameters::v2_0;
00056 
00057 
00068 class HeartConfig
00069 {
00070 private:
00077     void CheckTimeSteps() const;
00078 
00080     friend class boost::serialization::access;
00087     template<class Archive>
00088     void save(Archive & archive, const unsigned int version) const
00089     {
00090         //Only the Master should be writing the coonfiguration file
00091         if (PetscTools::AmMaster())
00092         {
00093             mpInstance->Write( true );
00094         }
00095         PetscTools::Barrier("HeartConfig::save");
00096     }
00097 
00104     template<class Archive>
00105     void load(Archive & archive, const unsigned int version)
00106     {
00107         /*
00108          *  This method implements the logic required by HeartConfig to be able to handle resuming a simulation via the executable.
00109          *
00110          *  When the control reaches the method mpUserParameters and mpDefaultParameters point to the files specified as resuming parameters.
00111          *  However SetDefaultsFile() and SetParametersFile() will set those variables to point to the archived parameters.
00112          *
00113          *  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
00114          *  with the resuming parameters.
00115          */
00116         assert(mpUserParameters.use_count() > 0);
00117         boost::shared_ptr<cp::chaste_parameters_type> p_new_parameters = mpUserParameters;
00118 
00119         std::string defaults_filename_xml = ArchiveLocationInfo::GetArchiveDirectory() + "ChasteDefaults.xml";
00120         HeartConfig::Instance()->SetDefaultsFile(defaults_filename_xml);
00121 
00122         /*
00123          *  When we unarchive a simulation, we load the old parameters file in order to inherit things such
00124          *  as default cell model, stimuli, heterogeneities, ... This has the side effect of inheriting the
00125          *  <CheckpointSimulation> element (if defined).
00126          *
00127          *  We disable checkpointing definition coming from the unarchived config file. We will enable it again
00128          *  if defined in the resume config file.
00129          */
00130         std::string parameters_filename_xml = ArchiveLocationInfo::GetArchiveDirectory() + "ChasteParameters.xml";
00131         HeartConfig::Instance()->SetParametersFile(parameters_filename_xml);
00132 
00133         HeartConfig::Instance()->SetCheckpointSimulation(false);
00134 
00135         // If we are resuming a simulation, update the simulation duration (and any other parameters to come...)
00136         if (p_new_parameters->ResumeSimulation().present())
00137         {
00138             if ( (p_new_parameters->ResumeSimulation().get().SpaceDimension() != HeartConfig::Instance()->GetSpaceDimension())
00139                  ||(p_new_parameters->ResumeSimulation().get().Domain() != HeartConfig::Instance()->GetDomain()))
00140             {
00141                 EXCEPTION("Problem type and space dimension should match when restarting a simulation.");
00142             }
00143 
00144             HeartConfig::Instance()->SetSimulationDuration(p_new_parameters->ResumeSimulation().get().SimulationDuration());
00145 
00146             if (p_new_parameters->ResumeSimulation().get().CheckpointSimulation().present())
00147             {
00148                 HeartConfig::Instance()->SetCheckpointSimulation(true,
00149                                                                  p_new_parameters->ResumeSimulation().get().CheckpointSimulation().get().timestep(),
00150                                                                  p_new_parameters->ResumeSimulation().get().CheckpointSimulation().get().max_checkpoints_on_disk());
00151             }
00152 
00153             //Visualization parameters are compulsory
00154             HeartConfig::Instance()->SetVisualizeWithVtk(p_new_parameters->ResumeSimulation().get().OutputVisualizer().vtk() == cp::yesno_type::yes);
00155             HeartConfig::Instance()->SetVisualizeWithCmgui(p_new_parameters->ResumeSimulation().get().OutputVisualizer().cmgui() == cp::yesno_type::yes);
00156             HeartConfig::Instance()->SetVisualizeWithMeshalyzer(p_new_parameters->ResumeSimulation().get().OutputVisualizer().meshalyzer() == cp::yesno_type::yes);
00157         }
00158     }
00159     BOOST_SERIALIZATION_SPLIT_MEMBER()
00160 
00161     
00174     xsd::cxx::xml::dom::auto_ptr<xercesc::DOMDocument> ReadFileToDomDocument(
00175         const std::string& rFileName,
00176         ::xml_schema::error_handler& rErrorHandler,
00177         const ::xml_schema::properties& rProps);
00178 
00179 public:
00184     typedef std::map<std::string, std::string> SchemaLocationsMap;
00185 
00186 private:
00190     SchemaLocationsMap mSchemaLocations;
00191 
00195     void SetDefaultSchemaLocations();
00196 
00204     std::string EscapeSpaces(const std::string& rPath);
00205 
00216     xercesc::DOMElement* AddNamespace(xercesc::DOMDocument* pDocument,
00217                                       xercesc::DOMElement* pElement,
00218                                       const std::string& rNamespace);
00219 
00230     xercesc::DOMElement* AddNamespace(xercesc::DOMDocument* pDocument,
00231                                       xercesc::DOMElement* pElement,
00232                                       const XMLCh* pNamespace);
00233 
00234 
00242     void TransformIonicModelDefinitions(xercesc::DOMDocument* pDocument,
00243                                         xercesc::DOMElement* pRootElement);
00244 
00253     void WrapContentInElement(xercesc::DOMDocument* pDocument,
00254                               xercesc::DOMElement* pElement,
00255                               const XMLCh* pNewElementLocalName);
00256 
00257 public:
00263     static HeartConfig* Instance();
00264 
00270     void SetUseFixedSchemaLocation(bool useFixedSchemaLocation);
00271 
00278     void SetFixedSchemaLocations(const SchemaLocationsMap& rSchemaLocations);
00279 
00283     void SetDefaultsFile(const std::string& rFileName);
00288     void SetParametersFile(const std::string& rFileName);
00300     void Write(bool useArchiveLocationInfo=false, std::string subfolderName="output");
00301 
00306     boost::shared_ptr<cp::chaste_parameters_type> ReadFile(const std::string& rFileName);
00307 
00312     static void Reset();
00313 
00314     /*
00315      *  Get methods
00316      */
00317 
00318     // Methods for asking the configuration file about which sections are defined.
00324     bool IsSimulationDefined() const;
00325 
00331     bool IsSimulationResumed() const;
00332 
00333     // Simulation
00334     unsigned GetSpaceDimension() const; 
00335     double GetSimulationDuration() const; 
00342     cp::domain_type GetDomain() const;
00343 
00351     cp::ionic_model_selection_type GetDefaultIonicModel() const;
00352 
00363      template<unsigned DIM>
00364      void GetIonicModelRegions(std::vector<ChasteCuboid<DIM> >& rDefinedRegions,
00365                                std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00366 
00378      void SetIonicModelRegions(std::vector<ChasteCuboid<3> >& rDefinedRegions,
00379                                std::vector<cp::ionic_model_selection_type>& rIonicModels) const;
00380 
00381     bool IsMeshProvided() const; 
00382     bool GetCreateMesh() const; 
00383     bool GetCreateSlab() const; 
00384     bool GetCreateSheet() const; 
00385     bool GetCreateFibre() const; 
00386     bool GetLoadMesh() const; 
00387 
00388 
00391     void GetSlabDimensions(c_vector<double, 3>& slabDimensions) const;
00395     void GetSheetDimensions(c_vector<double, 2>& sheetDimensions) const;
00399     void GetFibreLength(c_vector<double, 1>& fibreLength) const;
00400     double GetInterNodeSpace() const; 
00402     std::string GetMeshName() const;
00404     cp::media_type GetConductivityMedia() const;
00415      template<unsigned DIM>
00416     void GetStimuli(std::vector<boost::shared_ptr<SimpleStimulus> >& rStimuliApplied, std::vector<ChasteCuboid<DIM> >& rStimulatedAreas) const;
00417 
00431     template<unsigned DIM>
00432     void GetCellHeterogeneities( std::vector<AbstractChasteRegion<DIM>* >& rCellHeterogeneityRegions,
00433                                  std::vector<double>& rScaleFactorGks,
00434                                  std::vector<double>& rScaleFactorIto,
00435                                  std::vector<double>& rScaleFactorGkr);
00436     bool GetConductivityHeterogeneitiesProvided() const; 
00441     bool AreCellularTransmuralHeterogeneitiesRequested();
00442 
00446     bool AreCellularlHeterogeneitiesSpecifiedByCuboids();
00447 
00451     double GetEpiLayerFraction();
00452 
00456     double GetEndoLayerFraction();
00457 
00461     double GetMidLayerFraction();
00462 
00466     unsigned GetEpiLayerIndex();
00467 
00471     unsigned GetEndoLayerIndex();
00472 
00476     unsigned GetMidLayerIndex();
00477 
00478 
00487     template<unsigned DIM>
00488     void GetConductivityHeterogeneities(std::vector<ChasteCuboid<DIM> >& conductivitiesHeterogeneityAreas,
00489                                         std::vector< c_vector<double,3> >& intraConductivities,
00490                                         std::vector< c_vector<double,3> >& extraConductivities) const;
00491     std::string GetOutputDirectory() const; 
00503     std::string GetOutputFilenamePrefix() const;
00504 
00508     bool GetOutputVariablesProvided() const;
00509 
00515     void GetOutputVariables(std::vector<std::string> &outputVariables) const;
00516 
00522     bool GetCheckpointSimulation() const;
00523 
00529     double GetCheckpointTimestep() const;
00530 
00536     unsigned GetMaxCheckpointsOnDisk() const;
00537 
00538     // ResumeSimulation
00542     std::string GetArchivedSimulationDir() const;
00543 
00544 
00545     // Physiological
00550     void GetIntracellularConductivities(c_vector<double, 3>& intraConductivities) const;
00555     void GetIntracellularConductivities(c_vector<double, 2>& intraConductivities) const;
00560     void GetIntracellularConductivities(c_vector<double, 1>& intraConductivities) const;
00561 
00566     void GetExtracellularConductivities(c_vector<double, 3>& extraConductivities) const;
00571     void GetExtracellularConductivities(c_vector<double, 2>& extraConductivities) const;
00576     void GetExtracellularConductivities(c_vector<double, 1>& extraConductivities) const;
00577 
00578     double GetBathConductivity() const; 
00581     double GetSurfaceAreaToVolumeRatio() const; 
00583     double GetCapacitance() const; 
00585     // Numerical
00586     double GetOdeTimeStep() const; 
00587     double GetPdeTimeStep() const; 
00588     double GetPrintingTimeStep() const; 
00590     bool GetUseAbsoluteTolerance() const; 
00591     double GetAbsoluteTolerance() const; 
00593     bool GetUseRelativeTolerance() const; 
00594     double GetRelativeTolerance() const;  
00596     const char* GetKSPSolver() const; 
00597     const char* GetKSPPreconditioner() const; 
00600     // Adaptivity
00604     bool IsAdaptivityParametersPresent() const;
00605 
00609     double GetTargetErrorForAdaptivity() const;
00610 
00614     double GetSigmaForAdaptivity() const;
00615 
00619     double GetMaxEdgeLengthForAdaptivity() const;
00620 
00624     double GetMinEdgeLengthForAdaptivity() const;
00625 
00629     double GetGradationForAdaptivity() const;
00630 
00634     unsigned GetMaxNodesForAdaptivity() const;
00635 
00639     unsigned GetNumberOfAdaptiveSweeps() const;
00640 
00641     // Post processing
00645     bool IsPostProcessingSectionPresent() const;
00646 
00650     bool IsPostProcessingRequested() const;
00651 
00655     bool IsApdMapsRequested() const;
00661     void GetApdMaps(std::vector<std::pair<double,double> >& apdMaps) const;
00662 
00666     bool IsUpstrokeTimeMapsRequested() const;
00671     void GetUpstrokeTimeMaps (std::vector<double>& upstrokeTimeMaps) const;
00672 
00676     bool IsMaxUpstrokeVelocityMapRequested() const;
00677 
00682     void GetMaxUpstrokeVelocityMaps(std::vector<double>& upstrokeVelocityMaps) const;
00683 
00687     bool IsConductionVelocityMapsRequested() const;
00688 
00693     void GetConductionVelocityMaps(std::vector<unsigned>& conductionVelocityMaps) const;
00694 
00695 
00696     // Output visualization
00697 
00699     bool IsOutputVisualizerPresent() const;
00700 
00702     bool GetVisualizeWithMeshalyzer() const;
00703 
00705     bool GetVisualizeWithCmgui() const;
00706 
00708     bool GetVisualizeWithVtk() const;
00709 
00710      /*
00711      *  Set methods
00712      */
00713 
00714     // Simulation
00718     void SetSpaceDimension(unsigned spaceDimension);
00719 
00723     void SetSimulationDuration(double simulationDuration);
00724 
00731     void SetDomain(const cp::domain_type& rDomain);
00732 
00740     void SetDefaultIonicModel(const cp::ionic_models_available_type& rIonicModel);
00741 
00749     void SetSlabDimensions(double x, double y, double z, double inter_node_space);
00756     void SetSheetDimensions(double x, double y, double inter_node_space);
00762     void SetFibreLength(double x, double inter_node_space);
00763 
00770     void SetMeshFileName(std::string meshPrefix, cp::media_type fibreDefinition=cp::media_type::NoFibreOrientation);
00771 
00779     void SetConductivityHeterogeneities(std::vector<ChasteCuboid<3> >& conductivityAreas,
00780                                         std::vector< c_vector<double,3> >& intraConductivities,
00781                                         std::vector< c_vector<double,3> >& extraConductivities);
00782 
00786     void SetOutputDirectory(const std::string& rOutputDirectory);
00797     void SetOutputFilenamePrefix(const std::string& rOutputFilenamePrefix);
00798 
00810     void SetOutputVariables(const std::vector<std::string>& rOutputVariables);
00811 
00819      void SetCheckpointSimulation(bool checkpointSimulation, double checkpointTimestep=-1.0, unsigned maxCheckpointsOnDisk=UINT_MAX);
00820 
00821 
00822     // Physiological
00827     void SetIntracellularConductivities(const c_vector<double, 3>& intraConductivities);
00832     void SetIntracellularConductivities(const c_vector<double, 2>& intraConductivities);
00837     void SetIntracellularConductivities(const c_vector<double, 1>& intraConductivities);
00838 
00843     void SetExtracellularConductivities(const c_vector<double, 3>& extraConductivities);
00848     void SetExtracellularConductivities(const c_vector<double, 2>& extraConductivities);
00853     void SetExtracellularConductivities(const c_vector<double, 1>& extraConductivities);
00854 
00860     void SetBathConductivity(double bathConductivity);
00861 
00866     void SetSurfaceAreaToVolumeRatio(double ratio);
00867 
00872     void SetCapacitance(double capacitance);
00873 
00874     // Numerical
00881     void SetOdePdeAndPrintingTimeSteps(double odeTimeStep, double pdeTimeStep, double printingTimeStep);
00886     void SetOdeTimeStep(double odeTimeStep);
00891     void SetPdeTimeStep(double pdeTimeStep);
00892 
00897      void SetPrintingTimeStep(double printingTimeStep);
00898 
00902     void SetUseRelativeTolerance(double relativeTolerance);
00906     void SetUseAbsoluteTolerance(double absoluteTolerance);
00907 
00911     void SetKSPSolver(const char* kspSolver);
00915     void SetKSPPreconditioner(const char* kspPreconditioner);
00916 
00928     void SetAdaptivityParameters(double targetError, double sigma, double maxEdgeLength, double minEdgeLength,
00929                                  double gradation, unsigned maxNodes, unsigned numSweeps );
00930 
00936     void SetTargetErrorForAdaptivity(double targetError);
00937 
00943     void SetSigmaForAdaptivity(double sigma);
00944 
00950     void SetMaxEdgeLengthForAdaptivity(double maxEdgeLength);
00951 
00957     void SetMinEdgeLengthForAdaptivity(double minEdgeLength);
00958 
00964     void SetGradationForAdaptivity(double gradation);
00965 
00971     void SetMaxNodesForAdaptivity(unsigned maxNodes);
00972 
00978     void SetNumberOfAdaptiveSweeps(unsigned numSweeps);
00979 
00986     void SetApdMaps(const std::vector<std::pair<double,double> >& apdMaps);
00987 
00992     void SetUpstrokeTimeMaps (std::vector<double>& upstrokeTimeMaps);
00993 
00998     void SetMaxUpstrokeVelocityMaps (std::vector<double>& maxUpstrokeVelocityMaps);
00999 
01004     void SetConductionVelocityMaps (std::vector<unsigned>& conductionVelocityMaps);
01005 
01006 
01007     // Output visualization
01008 
01010     void EnsureOutputVisualizerExists(void);
01011 
01016     void SetVisualizeWithMeshalyzer(bool useMeshalyzer=true);
01017 
01022     void SetVisualizeWithCmgui(bool useCmgui=true);
01023 
01028     void SetVisualizeWithVtk(bool useVtk=true);
01029 
01030 
01031     ~HeartConfig(); 
01032 protected:
01033     // Only to be accessed by the tests
01034     friend class TestHeartConfig;
01035 
01036 private:
01037     /*Constructor is private, since the class is only accessed by the singleton instance() method*/
01038     HeartConfig();
01039 
01043     boost::shared_ptr<cp::chaste_parameters_type> mpUserParameters;
01047     boost::shared_ptr<cp::chaste_parameters_type> mpDefaultParameters;
01048 
01050     static std::auto_ptr<HeartConfig> mpInstance;
01051 
01056     bool mUseFixedSchemaLocation;
01057 
01061     double mEpiFraction;
01062 
01066     double mEndoFraction;
01067 
01071     double mMidFraction;
01072 
01076     unsigned mIndexMid;
01077 
01081     unsigned mIndexEpi;
01082 
01086     unsigned mIndexEndo;
01087 
01091     bool mUserAskedForCellularTransmuralHeterogeneities;
01092 
01096     bool mUserAskedForCuboidsForCellularHeterogeneities;
01097 
01107     template<class TYPE>
01108     TYPE* DecideLocation(TYPE* params_ptr, TYPE* defaults_ptr, const std::string& nameParameter) const;
01109 
01119      void CheckSimulationIsDefined(std::string callingMethod="") const;
01120 
01130      void CheckResumeSimulationIsDefined(std::string callingMethod="") const;
01131 
01132 };
01133 
01134 #include "SerializationExportWrapper.hpp"
01135 // Declare identifier for the serializer
01136 CHASTE_CLASS_EXPORT(HeartConfig);
01137 
01138 #endif /*HEARTCONFIG_HPP_*/

Generated by  doxygen 1.6.2