Chaste  Release::3.4
ContinuumMechanicsNeumannBcsAssembler.hpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 
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 
144 
149  {
150  delete mpQuadRule;
151  }
152 };
153 
154 
155 template<unsigned DIM>
157 {
158  if(this->mVectorToAssemble==NULL)
159  {
160  EXCEPTION("Vector to be assembled has not been set");
161  }
162 
163  if( PetscVecTools::GetSize(this->mVectorToAssemble) != (DIM+1)*mpMesh->GetNumNodes() )
164  {
165  EXCEPTION("Vector provided to be assembled has size " << PetscVecTools::GetSize(this->mVectorToAssemble) << ", not expected size of " << (DIM+1)*mpMesh->GetNumNodes() << " ((dim+1)*num_nodes)");
166  }
167 
168  // Zero the matrix/vector if it is to be assembled
169  if (this->mZeroVectorBeforeAssembly)
170  {
171  PetscVecTools::Zero(this->mVectorToAssemble);
172  }
173 
174 
175  if (mpProblemDefinition->GetTractionBoundaryConditionType() != NO_TRACTIONS)
176  {
177  c_vector<double, STENCIL_SIZE> b_elem = zero_vector<double>(STENCIL_SIZE);
178 
179  for (unsigned bc_index=0; bc_index<mpProblemDefinition->rGetTractionBoundaryElements().size(); bc_index++)
180  {
181  BoundaryElement<DIM-1,DIM>& r_boundary_element = *(mpProblemDefinition->rGetTractionBoundaryElements()[bc_index]);
182  AssembleOnBoundaryElement(r_boundary_element, b_elem, bc_index);
183 
184  unsigned p_indices[STENCIL_SIZE];
185  for (unsigned i=0; i<NUM_NODES_PER_ELEMENT; i++)
186  {
187  for (unsigned j=0; j<DIM; j++)
188  {
189  p_indices[DIM*i+j] = (DIM+1)*r_boundary_element.GetNodeGlobalIndex(i) + j;
190  }
191  }
192  // Note: The pressure block of b_elem will be zero, but this bit still needs to be
193  // set to avoid memory leaks.
194  for (unsigned i=0; i<DIM /*vertices per boundary elem */; i++)
195  {
196  p_indices[DIM*NUM_NODES_PER_ELEMENT + i] = (DIM+1)*r_boundary_element.GetNodeGlobalIndex(i)+DIM;
197  }
198 
199  PetscVecTools::AddMultipleValues<STENCIL_SIZE>(this->mVectorToAssemble, p_indices, b_elem);
200  }
201  }
202 }
203 
204 
205 
206 template<unsigned DIM>
208  c_vector<double,STENCIL_SIZE>& rBelem,
209  unsigned boundaryConditionIndex)
210 {
211  rBelem.clear();
212 
213  c_vector<double, DIM> weighted_direction;
214  double jacobian_determinant;
215  mpMesh->GetWeightedDirectionForBoundaryElement(rBoundaryElement.GetIndex(), weighted_direction, jacobian_determinant);
216 
217  c_vector<double,NUM_NODES_PER_ELEMENT> phi;
218 
219  for (unsigned quad_index=0; quad_index<mpQuadRule->GetNumQuadPoints(); quad_index++)
220  {
221  double wJ = jacobian_determinant * mpQuadRule->GetWeight(quad_index);
222  const ChastePoint<DIM-1>& quad_point = mpQuadRule->rGetQuadPoint(quad_index);
224 
225  c_vector<double,DIM> traction = zero_vector<double>(DIM);
226  switch (mpProblemDefinition->GetTractionBoundaryConditionType())
227  {
228  case ELEMENTWISE_TRACTION:
229  {
230  traction = mpProblemDefinition->rGetElementwiseTractions()[boundaryConditionIndex];
231  break;
232  }
233  default:
234  // Functional traction not implemented yet..
236  }
237 
238  for (unsigned index=0; index<NUM_NODES_PER_ELEMENT*DIM; index++)
239  {
240  unsigned spatial_dim = index%DIM;
241  unsigned node_index = (index-spatial_dim)/DIM;
242 
243  assert(node_index < NUM_NODES_PER_ELEMENT);
244 
245  rBelem(index) += traction(spatial_dim) * phi(node_index) * wJ;
246  }
247  }
248 }
249 
250 
251 #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
#define NEVER_REACHED
Definition: Exception.hpp:206
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
ContinuumMechanicsProblemDefinition< DIM > * mpProblemDefinition