VoltageInterpolaterOntoMechanicsMesh.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00054 Hdf5DataReader reader(directory,inputFileNamePrefix);
00055
00056 unsigned num_timesteps = reader.GetUnlimitedDimensionValues().size();
00057
00058
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
00065 Hdf5DataWriter* p_writer = new Hdf5DataWriter(*rMechanicsMesh.GetDistributedVectorFactory(),
00066 directory,
00067 "voltage_mechanics_mesh",
00068 false,
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
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
00096 reader.GetVariableOverNodes(voltage, var_name, time_step);
00097 ReplicatableVector voltage_repl(voltage);
00098
00099
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
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
00133 delete p_writer;
00134
00135
00136
00137
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
00150
00151 template class VoltageInterpolaterOntoMechanicsMesh<1>;
00152 template class VoltageInterpolaterOntoMechanicsMesh<2>;
00153 template class VoltageInterpolaterOntoMechanicsMesh<3>;