Chaste Commit::675f9facbe008c5eacb9006feaeb6423206579ea
CardiacElectroMechanicsVtkHandler.cpp
1/*
2
3Copyright (c) 2005-2025, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36
37#include "CardiacElectroMechanicsVtkHandler.hpp"
38
39template<unsigned DIM, unsigned ELEC_PROB_DIM>
41 QuadraticMesh<DIM>& rQuadMesh,
42 TetrahedralMesh<DIM,DIM>& rElectricsMesh,
43 ReplicatableVector& rElectricsInitialCondition,
44 const std::string& rOutputDir)
45 : mrMechanicsSolver(rMechanicsSolver),
46 mpVtkElastictyWriter(NULL),
47 mpInterpolater(NULL)
48{
49
50#ifdef CHASTE_VTK // Requires "sudo aptitude install libvtk5-dev" or similar
51 //create an internal copy of the quadratic mesh (we will modify node locations for oputput)
53 mpVtkOutputMesh->ConstructFromMesh(rQuadMesh);
54 assert(mpVtkOutputMesh->GetNumNodes() == rQuadMesh.GetNumNodes());
55
56 //cretae the writer, using the output mesh voltage and other quantities
57 mpVtkWriter = new VtkDeformedMeshWriter<DIM>(mpVtkOutputMesh, rOutputDir + "/vtk","deformed_mechanics_mesh_",true);
58 //set up voltage interpolating object
61
62 //allocate memory for the interpolated voltages (once on initialize)
63 mInterpolatedVoltagesNodeWise.assign(mpVtkOutputMesh->GetNumNodes(),0.0);
64 //allocate memory for displacements
65 c_vector<double,DIM> no_displ;
66 c_matrix<double,DIM,DIM> zero_strains;
67 for (unsigned i=0u ; i < DIM; ++i)
68 {
69 no_displ(i) = 0.0;
70 for (unsigned j=0u; j < DIM; ++j)
71 {
72 zero_strains(i,j) = 0.0;
73 }
74 }
75 mDisplacements.assign(mpVtkOutputMesh->GetNumNodes(),no_displ);
76 mStrains.assign(mpVtkOutputMesh->GetNumElements(),zero_strains);
77
78 //Create initial condition vector for the voltages (passed in)
79 ReplicatableVector init_cond(rElectricsMesh.GetNumNodes());
80 for (unsigned i = 0u; i < init_cond.GetSize(); ++i)
81 {
82 //the following line assumes interleaved solution for ELEC_PROB_DIM>1 (e.g, [Vm_0, phi_e_0, Vm1, phi_e_1...])
83 init_cond[i] = rElectricsInitialCondition[ELEC_PROB_DIM*i];
84 }
85 mpInterpolater->InterpolateOnCoarseMesh(mInterpolatedVoltagesNodeWise,init_cond);
86
87 mpVtkWriter->SetOutputBaseFileName("deformed_mechanics_mesh_" + std::to_string(0));
89 mpVtkWriter->AddPointData("displacements", mDisplacements);
90 mpVtkWriter->AddTensorCellData("deformation_gradient_F", mStrains);
91 assert(mInterpolatedVoltagesNodeWise.size()==rQuadMesh.GetNumNodes());
92 mpVtkWriter->WriteDeformedFiles();
93
94#endif //CHASTE_VTK
95}
96
97template<unsigned DIM, unsigned ELEC_PROB_DIM>
99{
100#ifdef CHASTE_VTK // Requires "sudo aptitude install libvtk5-dev" or similar
101 delete mpVtkWriter;
102 delete mpVtkOutputMesh;
103#endif
104 delete mpInterpolater;
105 delete mpVtkElastictyWriter;
106}
107
108template<unsigned DIM, unsigned ELEC_PROB_DIM>
110{
111#ifdef CHASTE_VTK // Requires "sudo aptitude install libvtk5-dev" or similar
112 //Apply deformation solution to mechanics mesh (the one in the writer object, not the one used by the solver!)
113 mpVtkWriter->ApplyDeformation(mrMechanicsSolver.rGetDeformedPosition());
114 mpVtkWriter->SetOutputBaseFileName("deformed_mechanics_mesh_" + std::to_string(counter));
115 mpVtkWriter->SetWriteMeshCells(false);
116
117 //voltage
118 mpInterpolater->InterpolateOnCoarseMesh(mInterpolatedVoltagesNodeWise,rElectricsSolution);//VTK output. Determine voltage on coarse nodes
119 mpVtkWriter->AddPointData("V", mInterpolatedVoltagesNodeWise);
120
121 //displacement
122 mpVtkElastictyWriter->CalculateDisplacements(mDisplacements);
123 mpVtkWriter->AddPointData("displacements", mDisplacements);
124
125 //Strains
126 mpVtkElastictyWriter->SetWriteElementWiseStrains(DEFORMATION_GRADIENT_F);
127 std::string name;
128 mpVtkElastictyWriter->CalculateStrains(name, mStrains);
129 mpVtkWriter->AddTensorCellData(name,mStrains);
130
131 //write to file
132 mpVtkWriter->WriteDeformedFiles();
133#endif
134}
135
136
137// Explicit instantiation
virtual unsigned GetNumNodes() const
void WriteSolution(unsigned counter, ReplicatableVector &rElectricsSolution)
VtkNonlinearElasticitySolutionWriter< DIM > * mpVtkElastictyWriter
CardiacElectroMechanicsVtkHandler(AbstractNonlinearElasticitySolver< DIM > &rMechanicsSolver, QuadraticMesh< DIM > &rQuadMesh, TetrahedralMesh< DIM, DIM > &rElectricsMesh, ReplicatableVector &rElectricsInitialCondition, const std::string &rOutputDir)
std::vector< c_matrix< double, DIM, DIM > > mStrains
std::vector< c_vector< double, DIM > > mDisplacements
VoltageInterpolaterOntoMechanicsMesh< DIM > * mpInterpolater