Chaste Commit::29c3c4ab2bee00ddd311a035a1e522c03cbe928a
CardiacElectroMechanicsVtkHandler.cpp
1/*
2
3Copyright (c) 2005-2026, 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 //create an internal copy of the quadratic mesh (we will modify node locations for oputput)
52 mpVtkOutputMesh->ConstructFromMesh(rQuadMesh);
53 assert(mpVtkOutputMesh->GetNumNodes() == rQuadMesh.GetNumNodes());
54
55 //cretae the writer, using the output mesh voltage and other quantities
56 mpVtkWriter = new VtkDeformedMeshWriter<DIM>(mpVtkOutputMesh, rOutputDir + "/vtk","deformed_mechanics_mesh_",true);
57 //set up voltage interpolating object
60
61 //allocate memory for the interpolated voltages (once on initialize)
62 mInterpolatedVoltagesNodeWise.assign(mpVtkOutputMesh->GetNumNodes(),0.0);
63 //allocate memory for displacements
64 c_vector<double,DIM> no_displ;
65 c_matrix<double,DIM,DIM> zero_strains;
66 for (unsigned i=0u ; i < DIM; ++i)
67 {
68 no_displ(i) = 0.0;
69 for (unsigned j=0u; j < DIM; ++j)
70 {
71 zero_strains(i,j) = 0.0;
72 }
73 }
74 mDisplacements.assign(mpVtkOutputMesh->GetNumNodes(),no_displ);
75 mStrains.assign(mpVtkOutputMesh->GetNumElements(),zero_strains);
76
77 //Create initial condition vector for the voltages (passed in)
78 ReplicatableVector init_cond(rElectricsMesh.GetNumNodes());
79 for (unsigned i = 0u; i < init_cond.GetSize(); ++i)
80 {
81 //the following line assumes interleaved solution for ELEC_PROB_DIM>1 (e.g, [Vm_0, phi_e_0, Vm1, phi_e_1...])
82 init_cond[i] = rElectricsInitialCondition[ELEC_PROB_DIM*i];
83 }
84 mpInterpolater->InterpolateOnCoarseMesh(mInterpolatedVoltagesNodeWise,init_cond);
85
86 mpVtkWriter->SetOutputBaseFileName("deformed_mechanics_mesh_" + std::to_string(0));
88 mpVtkWriter->AddPointData("displacements", mDisplacements);
89 mpVtkWriter->AddTensorCellData("deformation_gradient_F", mStrains);
90 assert(mInterpolatedVoltagesNodeWise.size()==rQuadMesh.GetNumNodes());
91 mpVtkWriter->WriteDeformedFiles();
92}
93
94template<unsigned DIM, unsigned ELEC_PROB_DIM>
96{
97 delete mpVtkWriter;
98 delete mpVtkOutputMesh;
99 delete mpInterpolater;
100 delete mpVtkElastictyWriter;
101}
102
103template<unsigned DIM, unsigned ELEC_PROB_DIM>
105{
106 //Apply deformation solution to mechanics mesh (the one in the writer object, not the one used by the solver!)
107 mpVtkWriter->ApplyDeformation(mrMechanicsSolver.rGetDeformedPosition());
108 mpVtkWriter->SetOutputBaseFileName("deformed_mechanics_mesh_" + std::to_string(counter));
109 mpVtkWriter->SetWriteMeshCells(false);
110
111 //voltage
112 mpInterpolater->InterpolateOnCoarseMesh(mInterpolatedVoltagesNodeWise,rElectricsSolution);//VTK output. Determine voltage on coarse nodes
113 mpVtkWriter->AddPointData("V", mInterpolatedVoltagesNodeWise);
114
115 //displacement
116 mpVtkElastictyWriter->CalculateDisplacements(mDisplacements);
117 mpVtkWriter->AddPointData("displacements", mDisplacements);
118
119 //Strains
120 mpVtkElastictyWriter->SetWriteElementWiseStrains(DEFORMATION_GRADIENT_F);
121 std::string name;
122 mpVtkElastictyWriter->CalculateStrains(name, mStrains);
123 mpVtkWriter->AddTensorCellData(name,mStrains);
124
125 //write to file
126 mpVtkWriter->WriteDeformedFiles();
127}
128
129
130// 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