Chaste Commit::30a3e656d4b131f8c595cc6eb2becd297337570f
AveragedSourceEllipticPde.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#include "AveragedSourceEllipticPde.hpp"
37#include "ApoptoticCellProperty.hpp"
38
39template<unsigned DIM>
41 double constantSourceCoefficient,
42 double linearSourceCoefficient,
43 double diffusionCoefficient,
44 bool scaleByCellVolume)
45 : mrCellPopulation(rCellPopulation),
46 mConstantSourceCoefficient(constantSourceCoefficient),
47 mLinearSourceCoefficient(linearSourceCoefficient),
48 mDiffusionCoefficient(diffusionCoefficient),
49 mScaleByCellVolume(scaleByCellVolume)
50{
51}
52
53template<unsigned DIM>
58
59template<unsigned DIM>
61{
62 return mConstantSourceCoefficient;
63}
64
65template<unsigned DIM>
67{
68 return mLinearSourceCoefficient;
69}
70
71template<unsigned DIM>
73{
74 return mDiffusionCoefficient;
75}
76
77template<unsigned DIM>
79{
80 return mScaleByCellVolume;
81}
82
83template<unsigned DIM>
84void AveragedSourceEllipticPde<DIM>::SetupSourceTerms(TetrahedralMesh<DIM,DIM>& rCoarseMesh, std::map< CellPtr, unsigned >* pCellPdeElementMap) // must be called before solve
85{
86 // Allocate memory
87 mCellDensityOnCoarseElements.resize(rCoarseMesh.GetNumElements());
88 for (double & mCellDensityOnCoarseElement : mCellDensityOnCoarseElements)
89 {
90 mCellDensityOnCoarseElement = 0.0;
91 }
92
93 // Loop over cells, find which coarse element it is in, and add 1 to mSourceTermOnCoarseElements[elem_index]
94 for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = mrCellPopulation.Begin();
95 cell_iter != mrCellPopulation.End();
96 ++cell_iter)
97 {
98 unsigned elem_index = 0;
99 const ChastePoint<DIM>& r_position_of_cell = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
100
101 if (pCellPdeElementMap != nullptr)
102 {
103 elem_index = (*pCellPdeElementMap)[*cell_iter];
104 }
105 else
106 {
107 elem_index = rCoarseMesh.GetContainingElementIndex(r_position_of_cell);
108 }
109
110 // Update element map if cell has moved
111 bool cell_is_apoptotic = cell_iter->template HasCellProperty<ApoptoticCellProperty>();
112
113 if (!cell_is_apoptotic)
114 {
115 double cell_weight = 1.0;
116
117 if (mScaleByCellVolume)
118 {
119 // If scaling by cell volume then use volume her instead of cell count
120 cell_weight = mrCellPopulation.GetVolumeOfCell(*cell_iter);
121
122 if (cell_weight <1e-6)
123 {
124 EXCEPTION("The volume of one of the cells is " << cell_weight <<
125 " and you are scaling by cell volume. Either turn scaling off or use"
126 " a cell model with non zero areas (i.e. a Bounded Voronoi Tesselation model).");
127 }
128 }
129
130 mCellDensityOnCoarseElements[elem_index] += cell_weight;
131 }
132 }
133
134 // Then divide each entry of mSourceTermOnCoarseElements by the element's area
135 c_matrix<double, DIM, DIM> jacobian;
136 double det;
137 for (unsigned elem_index=0; elem_index<mCellDensityOnCoarseElements.size(); elem_index++)
138 {
139 rCoarseMesh.GetElement(elem_index)->CalculateJacobian(jacobian, det);
140 mCellDensityOnCoarseElements[elem_index] /= rCoarseMesh.GetElement(elem_index)->GetVolume(det);
141 }
142}
143
144template<unsigned DIM>
146{
147 assert(!mCellDensityOnCoarseElements.empty());
148 EXCEPT_IF(pElement == nullptr);
149 return mConstantSourceCoefficient * mCellDensityOnCoarseElements[pElement->GetIndex()];
150}
151
152template<unsigned DIM>
154{
155 assert(!mCellDensityOnCoarseElements.empty());
156 EXCEPT_IF(pElement == nullptr);
157 return mLinearSourceCoefficient * mCellDensityOnCoarseElements[pElement->GetIndex()];
158}
159
160template<unsigned DIM>
162{
163 return mDiffusionCoefficient*identity_matrix<double>(DIM);
164}
165
166template<unsigned DIM>
168{
169 return this->mCellDensityOnCoarseElements[elementIndex];
170}
171
172// Explicit instantiation
173template class AveragedSourceEllipticPde<1>;
174template class AveragedSourceEllipticPde<2>;
175template class AveragedSourceEllipticPde<3>;
176
177// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define EXCEPT_IF(test)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
unsigned GetIndex() const
unsigned GetContainingElementIndex(const ChastePoint< SPACE_DIM > &rTestPoint, bool strict=false, std::set< unsigned > testElements=std::set< unsigned >(), bool onlyTryWithTestElements=false)
Element< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
virtual unsigned GetNumElements() const
double ComputeConstantInUSourceTerm(const ChastePoint< DIM > &rX, Element< DIM, DIM > *pElement) override
double GetUptakeRateForElement(unsigned elementIndex)
double ComputeLinearInUCoeffInSourceTerm(const ChastePoint< DIM > &rX, Element< DIM, DIM > *pElement) override
c_matrix< double, DIM, DIM > ComputeDiffusionTerm(const ChastePoint< DIM > &rX) override
AveragedSourceEllipticPde(AbstractCellPopulation< DIM > &rCellPopulation, double constantSourceCoefficient=0.0, double linearSourceCoefficient=0.0, double diffusionCoefficient=1.0, bool scaleByCellVolume=false)
virtual void SetupSourceTerms(TetrahedralMesh< DIM, DIM > &rCoarseMesh, std::map< CellPtr, unsigned > *pCellPdeElementMap=nullptr)
const AbstractCellPopulation< DIM > & rGetCellPopulation() const