VoltageInterpolaterOntoMechanicsMesh.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 "VoltageInterpolaterOntoMechanicsMesh.hpp"
00037 #include "Hdf5ToCmguiConverter.hpp"
00038 #include "FineCoarseMeshPair.hpp"
00039 #include "ReplicatableVector.hpp"
00040 #include "HeartConfig.hpp"
00041 #include "Hdf5DataReader.hpp"
00042 #include "PetscTools.hpp"
00043 #include "Hdf5DataWriter.hpp"
00044 
00045 template<unsigned DIM>
00046 VoltageInterpolaterOntoMechanicsMesh<DIM>::VoltageInterpolaterOntoMechanicsMesh(
00047                                      TetrahedralMesh<DIM,DIM>& rElectricsMesh,
00048                                      QuadraticMesh<DIM>& rMechanicsMesh,
00049                                      std::vector<std::string>& rVariableNames,
00050                                      std::string directory,
00051                                      std::string inputFileNamePrefix)
00052 {
00053     // Read the data from the HDF5 file
00054     Hdf5DataReader reader(directory,inputFileNamePrefix);
00055 
00056     unsigned num_timesteps = reader.GetUnlimitedDimensionValues().size();
00057 
00058     // set up the elements and weights for the coarse nodes in the fine mesh
00059     FineCoarseMeshPair<DIM> mesh_pair(rElectricsMesh, rMechanicsMesh);
00060     mesh_pair.SetUpBoxesOnFineMesh();
00061     mesh_pair.ComputeFineElementsAndWeightsForCoarseNodes(true);
00062     assert(mesh_pair.rGetElementsAndWeights().size()==rMechanicsMesh.GetNumNodes());
00063 
00064     // create and setup a writer
00065     Hdf5DataWriter* p_writer = new Hdf5DataWriter(*rMechanicsMesh.GetDistributedVectorFactory(),
00066                                                   directory,
00067                                                   "voltage_mechanics_mesh",
00068                                                   false, //don't clean
00069                                                   false);
00070 
00071     std::vector<int> columns_id;
00072     for (unsigned var_index = 0; var_index < rVariableNames.size(); var_index++)
00073     {
00074         std::string var_name = rVariableNames[var_index];
00075         columns_id.push_back( p_writer->DefineVariable(var_name,"mV") );
00076     }
00077 
00078     p_writer->DefineUnlimitedDimension("Time","msecs", num_timesteps);
00079     p_writer->DefineFixedDimension( rMechanicsMesh.GetNumNodes() );
00080     p_writer->EndDefineMode();
00081 
00082     assert(columns_id.size() == rVariableNames.size());
00083 
00084     // set up a vector to read into
00085     DistributedVectorFactory factory(rElectricsMesh.GetNumNodes());
00086     Vec voltage = factory.CreateVec();
00087     std::vector<double> interpolated_voltages(rMechanicsMesh.GetNumNodes());
00088     Vec voltage_coarse = NULL;
00089 
00090     for(unsigned time_step=0; time_step<num_timesteps; time_step++)
00091     {
00092         for (unsigned var_index = 0; var_index < rVariableNames.size(); var_index++)
00093         {
00094             std::string var_name = rVariableNames[var_index];
00095             // read
00096             reader.GetVariableOverNodes(voltage, var_name, time_step);
00097             ReplicatableVector voltage_repl(voltage);
00098 
00099             // interpolate
00100             for(unsigned i=0; i<mesh_pair.rGetElementsAndWeights().size(); i++)
00101             {
00102                 double interpolated_voltage = 0;
00103 
00104                 Element<DIM,DIM>& element = *(rElectricsMesh.GetElement(mesh_pair.rGetElementsAndWeights()[i].ElementNum));
00105                 for(unsigned node_index = 0; node_index<element.GetNumNodes(); node_index++)
00106                 {
00107                     unsigned global_node_index = element.GetNodeGlobalIndex(node_index);
00108                     interpolated_voltage += voltage_repl[global_node_index]*mesh_pair.rGetElementsAndWeights()[i].Weights(node_index);
00109                 }
00110 
00111                 interpolated_voltages[i] = interpolated_voltage;
00112             }
00113 
00114             if(voltage_coarse!=NULL)
00115             {
00116                 PetscTools::Destroy(voltage_coarse);
00117             }
00118             voltage_coarse = PetscTools::CreateVec(interpolated_voltages);
00119             // write
00120             p_writer->PutVector(columns_id[var_index], voltage_coarse);
00121         }
00122         p_writer->PutUnlimitedVariable(time_step);
00123         p_writer->AdvanceAlongUnlimitedDimension();
00124     }
00125 
00126     if(voltage_coarse!=NULL)
00127     {
00128         PetscTools::Destroy(voltage);
00129         PetscTools::Destroy(voltage_coarse);
00130     }
00131 
00132     // delete to flush
00133     delete p_writer;
00134 
00135     // Convert the new data to CMGUI format.
00136     // alter the directory in HeartConfig as that is where Hdf5ToCmguiConverter decides
00137     // where to output
00138     std::string config_directory = HeartConfig::Instance()->GetOutputDirectory();
00139     HeartConfig::Instance()->SetOutputDirectory(directory);
00140     Hdf5ToCmguiConverter<DIM,DIM> converter(FileFinder(directory, RelativeTo::ChasteTestOutput),
00141                                             "voltage_mechanics_mesh",
00142                                             &rMechanicsMesh,
00143                                             false);
00144     HeartConfig::Instance()->SetOutputDirectory(config_directory);
00145 }
00146 
00148 // explicit instantiation
00150 
00151 template class VoltageInterpolaterOntoMechanicsMesh<1>;
00152 template class VoltageInterpolaterOntoMechanicsMesh<2>;
00153 template class VoltageInterpolaterOntoMechanicsMesh<3>;

Generated by  doxygen 1.6.2