CardiacSimulation.cpp

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 #include "CardiacSimulationArchiver.hpp"  // Must go first
00030 #include "CardiacSimulation.hpp"
00031 
00032 
00033 boost::shared_ptr<AbstractUntemplatedCardiacProblem> CardiacSimulation::GetSavedProblem()
00034 {
00035     return mSavedProblem;
00036 }
00037 
00038 std::string CardiacSimulation::BoolToString(bool yesNo)
00039 {
00040     std::string result;
00041     if (yesNo)
00042     {
00043         result = "yes";
00044     }
00045     else
00046     {
00047         result = "no";
00048     }
00049     return result;
00050 }
00051 
00052 void CardiacSimulation::CreateResumeXmlFile(const std::string& rOutputDirectory, const std::string& rArchiveDirectory)
00053 {
00054     OutputFileHandler handler(rOutputDirectory, false);
00055     if (PetscTools::AmMaster())
00056     {
00057         out_stream p_file = handler.OpenOutputFile("ResumeParameters.xml");
00058         (*p_file) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00059         (*p_file) << "<ChasteParameters xmlns='https://chaste.comlab.ox.ac.uk/nss/parameters/3_0' "
00060                   << "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
00061                   << "xsi:schemaLocation='https://chaste.comlab.ox.ac.uk/nss/parameters/3_0 ChasteParameters_3_0.xsd'>" << std::endl;
00062         (*p_file) << std::endl;
00063         (*p_file) << "    <ResumeSimulation>" << std::endl;
00064         (*p_file) << "        <ArchiveDirectory relative_to='this_file'>" << rArchiveDirectory << "</ArchiveDirectory>" << std::endl;
00065         (*p_file) << "        <SpaceDimension>" << HeartConfig::Instance()->GetSpaceDimension() << "</SpaceDimension>" << std::endl;
00066         (*p_file) << "        <SimulationDuration unit='ms'>0.0</SimulationDuration> <!-- Edit with new simulation duration. Please "
00067                   << "note that the simulation does not restart at t=0 but at the time where the checkpoint was created.-->" << std::endl;
00068         (*p_file) << "        <Domain>" << HeartConfig::Instance()->GetDomain() << "</Domain>" << std::endl;
00069         (*p_file) << "        <CheckpointSimulation timestep='" << HeartConfig::Instance()->GetCheckpointTimestep()
00070                   << "' unit='ms' max_checkpoints_on_disk='" << HeartConfig::Instance()->GetMaxCheckpointsOnDisk()
00071                   << "'/> <!-- This is optional; if not given, the loaded simulation will NOT itself be checkpointed -->" << std::endl;
00072         (*p_file) << "        <OutputVisualizer meshalyzer='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithMeshalyzer())
00073                   << "' vtk='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithVtk())
00074                   << "' parallel_vtk='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithParallelVtk())
00075                   << "' cmgui='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithCmgui()) << "'/>" << std::endl;
00076         (*p_file) << "    </ResumeSimulation>" << std::endl;
00077         (*p_file) << std::endl;
00078         (*p_file) << "    <!-- These elements must exist, but their contents are ignored -->" << std::endl;
00079         (*p_file) << "    <Physiological/>" << std::endl;
00080         (*p_file) << "    <Numerical/>" << std::endl;
00081         (*p_file) << "</ChasteParameters>" << std::endl;
00082         p_file->close();
00083     }
00084     HeartConfig::Instance()->CopySchema(handler.GetOutputDirectoryFullPath());
00085 }
00086 
00087 CardiacSimulation::CardiacSimulation(std::string parameterFileName,
00088                                      bool writeProvenanceInfo,
00089                                      bool saveProblemInstance)
00090     : mSaveProblemInstance(saveProblemInstance)
00091 {
00092     // If we have been passed an XML file then parse the XML file, otherwise throw
00093     if (parameterFileName == "")
00094     {
00095         EXCEPTION("No XML file name given");
00096     }
00097     ReadParametersFromFile(parameterFileName);
00098     Run();
00099     HeartEventHandler::Headings();
00100     HeartEventHandler::Report();
00101     if (writeProvenanceInfo)
00102     {
00103         ExecutableSupport::SetOutputDirectory(HeartConfig::Instance()->GetOutputDirectory());
00104         ExecutableSupport::WriteProvenanceInfoFile();
00105         ExecutableSupport::WriteMachineInfoFile("machine_info");
00106     }
00107 }
00108 
00109 void CardiacSimulation::ReadParametersFromFile(std::string parameterFileName)
00110 {
00111     // Ensure the singleton is in a clean state
00112     HeartConfig::Reset();
00113     try
00114     {
00115         // Try the hardcoded schema location first
00116         HeartConfig::Instance()->SetUseFixedSchemaLocation(true);
00117         HeartConfig::Instance()->SetParametersFile(parameterFileName);
00118     }
00119     catch (Exception& e)
00120     {
00121         if (e.CheckShortMessageContains("Missing file parsing configuration") == "")
00122         {
00123             // Try using the schema location given in the XML
00124             HeartConfig::Reset();
00125             HeartConfig::Instance()->SetUseFixedSchemaLocation(false);
00126             HeartConfig::Instance()->SetParametersFile(parameterFileName);
00127         }
00128         else
00129         {
00130             throw e;
00131         }
00132     }
00133 }
00134 
00135 
00136 #define DOMAIN_CASE(VALUE, CLASS, DIM)    \
00137     case VALUE:                           \
00138     {                                     \
00139         CreateAndRun<CLASS<DIM>, DIM>();  \
00140         break;                            \
00141     }
00142 
00143 #define DOMAIN_SWITCH(DIM)                                                     \
00144     switch (HeartConfig::Instance()->GetDomain())                              \
00145     {                                                                          \
00146         DOMAIN_CASE(cp::domain_type::Mono, MonodomainProblem, DIM)             \
00147         DOMAIN_CASE(cp::domain_type::Bi, BidomainProblem, DIM)                 \
00148         DOMAIN_CASE(cp::domain_type::BiWithBath, BidomainWithBathProblem, DIM) \
00149         default:                                                               \
00150             NEVER_REACHED;                                                     \
00151     }                                                                          \
00152     break
00153 // Note that if the domain is not set correctly then the XML parser will have picked it up before now!
00154 // Missing semi-colon after break so we can put it after the macro call.
00155 
00156 void CardiacSimulation::Run()
00157 {
00158     switch (HeartConfig::Instance()->GetSpaceDimension())
00159     {
00160         case 3:
00161         {
00162             DOMAIN_SWITCH(3);
00163         }
00164         case 2:
00165         {
00166             DOMAIN_SWITCH(2);
00167         }
00168         case 1:
00169         {
00170             DOMAIN_SWITCH(1);
00171         }
00172         default:
00173             // We could check for this too in the XML Schema...
00174             EXCEPTION("Space dimension not supported: should be 1, 2 or 3");
00175     }
00176 }
00177 
00178 // These aren't needed externally
00179 #undef DOMAIN_SWITCH
00180 #undef DOMAIN_CASE
00181 
Generated on Thu Dec 22 13:00:06 2011 for Chaste by  doxygen 1.6.3