Chaste  Release::2017.1
ContinuumMechanicsNeumannBcsAssembler.hpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef CONTINUUMMECHANICSNEUMANNBCSASSEMBLER_HPP_
37 #define CONTINUUMMECHANICSNEUMANNBCSASSEMBLER_HPP_
38 
39 #include "AbstractFeAssemblerInterface.hpp"
40 #include "AbstractTetrahedralMesh.hpp"
41 #include "QuadraticMesh.hpp"
42 #include "DistributedQuadraticMesh.hpp"
43 #include "LinearBasisFunction.hpp"
44 #include "QuadraticBasisFunction.hpp"
45 #include "ReplicatableVector.hpp"
46 #include "DistributedVector.hpp"
47 #include "PetscTools.hpp"
48 #include "PetscVecTools.hpp"
49 #include "PetscMatTools.hpp"
50 #include "GaussianQuadratureRule.hpp"
51 #include "ContinuumMechanicsProblemDefinition.hpp"
52 
53 
69 template<unsigned DIM>
71 {
73  static const unsigned NUM_VERTICES_PER_ELEMENT = DIM;
74 
76  static const unsigned NUM_NODES_PER_ELEMENT = DIM*(DIM+1)/2; // assuming quadratic
77 
82 
84  static const unsigned STENCIL_SIZE = DIM*NUM_NODES_PER_ELEMENT + NUM_VERTICES_PER_ELEMENT;
85 
86 protected:
89 
92 
95 
100  void DoAssemble();
101 
102 
114  c_vector<double, STENCIL_SIZE>& rBElem,
115  unsigned boundaryConditionIndex);
116 
117 public:
123  ContinuumMechanicsProblemDefinition<DIM>* pProblemDefinition)
124  : AbstractFeAssemblerInterface<true,false>(),
125  mpMesh(pMesh),
126  mpProblemDefinition(pProblemDefinition)
127  {
128  assert(pMesh);
129  assert(pProblemDefinition);
130 
131  //Check that the mesh is Quadratic
132  QuadraticMesh<DIM>* p_quad_mesh = dynamic_cast<QuadraticMesh<DIM>* >(pMesh);
133  DistributedQuadraticMesh<DIM>* p_distributed_quad_mesh = dynamic_cast<DistributedQuadraticMesh<DIM>* >(pMesh);
134 
135  if ((p_quad_mesh == NULL) && (p_distributed_quad_mesh == NULL))
136  {
137  EXCEPTION("Continuum mechanics solvers require a quadratic mesh");
138  }
139  // In general a mechanics problem is non-polynomial.
140  // We therefore use the highest order integration rule available.
141  mpQuadRule = new GaussianQuadratureRule<DIM-1>(3);
142  }
143 
148  {
149  delete mpQuadRule;
150  }
151 };
152 
153 
154 template<unsigned DIM>
156 {
157  if (this->mVectorToAssemble==NULL)
158  {
159  EXCEPTION("Vector to be assembled has not been set");
160  }
161 
163  {
164  EXCEPTION("Vector provided to be assembled has size " << PetscVecTools::GetSize(this->mVectorToAssemble) << ", not expected size of " << (DIM+1)*mpMesh->GetNumNodes() << " ((dim+1)*num_nodes)");
165  }
166 
167  // Zero the matrix/vector if it is to be assembled
168  if (this->mZeroVectorBeforeAssembly)
169  {
171  }
172 
173 
174  if (mpProblemDefinition->GetTractionBoundaryConditionType() != NO_TRACTIONS)
175  {
176  c_vector<double, STENCIL_SIZE> b_elem = zero_vector<double>(STENCIL_SIZE);
177 
178  for (unsigned bc_index=0; bc_index<mpProblemDefinition->rGetTractionBoundaryElements().size(); bc_index++)
179  {
180  BoundaryElement<DIM-1,DIM>& r_boundary_element = *(mpProblemDefinition->rGetTractionBoundaryElements()[bc_index]);
181  AssembleOnBoundaryElement(r_boundary_element, b_elem, bc_index);
182 
183  unsigned p_indices[STENCIL_SIZE];
184  for (unsigned i=0; i<NUM_NODES_PER_ELEMENT; i++)
185  {
186  for (unsigned j=0; j<DIM; j++)
187  {
188  p_indices[DIM*i+j] = (DIM+1)*r_boundary_element.GetNodeGlobalIndex(i) + j;
189  }
190  }
191  // Note: The pressure block of b_elem will be zero, but this bit still needs to be
192  // set to avoid memory leaks.
193  for (unsigned i=0; i<DIM /*vertices per boundary elem */; i++)
194  {
195  p_indices[DIM*NUM_NODES_PER_ELEMENT + i] = (DIM+1)*r_boundary_element.GetNodeGlobalIndex(i)+DIM;
196  }
197 
198  PetscVecTools::AddMultipleValues<STENCIL_SIZE>(this->mVectorToAssemble, p_indices, b_elem);
199  }
200  }
201 }
202 
203 template<unsigned DIM>
205  c_vector<double,STENCIL_SIZE>& rBelem,
206  unsigned boundaryConditionIndex)
207 {
208  rBelem.clear();
209 
210  c_vector<double, DIM> weighted_direction;
211  double jacobian_determinant;
212  mpMesh->GetWeightedDirectionForBoundaryElement(rBoundaryElement.GetIndex(), weighted_direction, jacobian_determinant);
213 
214  c_vector<double,NUM_NODES_PER_ELEMENT> phi;
215 
216  for (unsigned quad_index=0; quad_index<mpQuadRule->GetNumQuadPoints(); quad_index++)
217  {
218  double wJ = jacobian_determinant * mpQuadRule->GetWeight(quad_index);
219  const ChastePoint<DIM-1>& quad_point = mpQuadRule->rGetQuadPoint(quad_index);
221 
222  c_vector<double,DIM> traction = zero_vector<double>(DIM);
223  switch (mpProblemDefinition->GetTractionBoundaryConditionType())
224  {
225  case ELEMENTWISE_TRACTION:
226  {
227  traction = mpProblemDefinition->rGetElementwiseTractions()[boundaryConditionIndex];
228  break;
229  }
230  default:
231  // Functional traction not implemented yet..
233  }
234 
235  for (unsigned index=0; index<NUM_NODES_PER_ELEMENT*DIM; index++)
236  {
237  unsigned spatial_dim = index%DIM;
238  unsigned node_index = (index-spatial_dim)/DIM;
239 
240  assert(node_index < NUM_NODES_PER_ELEMENT);
241 
242  rBelem(index) += traction(spatial_dim) * phi(node_index) * wJ;
243  }
244  }
245 }
246 
247 
248 #endif // CONTINUUMMECHANICSNEUMANNBCSASSEMBLER_HPP_
void AssembleOnBoundaryElement(BoundaryElement< DIM-1, DIM > &rElement, c_vector< double, STENCIL_SIZE > &rBElem, unsigned boundaryConditionIndex)
static unsigned GetSize(Vec vector)
#define EXCEPTION(message)
Definition: Exception.hpp:143
virtual unsigned GetNumNodes() const
#define NEVER_REACHED
Definition: Exception.hpp:206
unsigned GetNumQuadPoints() const
virtual void GetWeightedDirectionForBoundaryElement(unsigned elementIndex, c_vector< double, SPACE_DIM > &rWeightedDirection, double &rJacobianDeterminant) const
static void ComputeBasisFunctions(const ChastePoint< ELEMENT_DIM > &rPoint, c_vector< double,(ELEMENT_DIM+1)*(ELEMENT_DIM+2)/2 > &rReturnValue)
static void Zero(Vec vector)
ContinuumMechanicsNeumannBcsAssembler(AbstractTetrahedralMesh< DIM, DIM > *pMesh, ContinuumMechanicsProblemDefinition< DIM > *pProblemDefinition)
unsigned GetIndex() const
double GetWeight(unsigned index) const
ContinuumMechanicsProblemDefinition< DIM > * mpProblemDefinition
const ChastePoint< ELEMENT_DIM > & rGetQuadPoint(unsigned index) const