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

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5