CardiacSimulation.cpp

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

Generated by  doxygen 1.6.2