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