Chaste Release::3.1
|
00001 /* 00002 00003 Copyright (c) 2005-2012, University of Oxford. 00004 All rights reserved. 00005 00006 University of Oxford means the Chancellor, Masters and Scholars of the 00007 University of Oxford, having an administrative office at Wellington 00008 Square, Oxford OX1 2JD, UK. 00009 00010 This file is part of Chaste. 00011 00012 Redistribution and use in source and binary forms, with or without 00013 modification, are permitted provided that the following conditions are met: 00014 * Redistributions of source code must retain the above copyright notice, 00015 this list of conditions and the following disclaimer. 00016 * Redistributions in binary form must reproduce the above copyright notice, 00017 this list of conditions and the following disclaimer in the documentation 00018 and/or other materials provided with the distribution. 00019 * Neither the name of the University of Oxford nor the names of its 00020 contributors may be used to endorse or promote products derived from this 00021 software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 */ 00035 00036 #ifndef ABSTRACTCONTINUUMMECHANICSASSEMBLER_HPP_ 00037 #define ABSTRACTCONTINUUMMECHANICSASSEMBLER_HPP_ 00038 00039 #include "AbstractFeAssemblerInterface.hpp" 00040 #include "QuadraticMesh.hpp" 00041 #include "LinearBasisFunction.hpp" 00042 #include "QuadraticBasisFunction.hpp" 00043 #include "ReplicatableVector.hpp" 00044 #include "DistributedVector.hpp" 00045 #include "PetscTools.hpp" 00046 #include "PetscVecTools.hpp" 00047 #include "PetscMatTools.hpp" 00048 #include "GaussianQuadratureRule.hpp" 00049 00050 00084 template<unsigned DIM, bool CAN_ASSEMBLE_VECTOR, bool CAN_ASSEMBLE_MATRIX> 00085 class AbstractContinuumMechanicsAssembler : public AbstractFeAssemblerInterface<CAN_ASSEMBLE_VECTOR,CAN_ASSEMBLE_MATRIX> 00086 { 00090 static const bool BLOCK_SYMMETRIC_MATRIX = true; //generalise to non-block symmetric matrices later (when needed maybe) 00091 00093 static const unsigned NUM_VERTICES_PER_ELEMENT = DIM+1; 00094 00096 static const unsigned NUM_NODES_PER_ELEMENT = (DIM+1)*(DIM+2)/2; // assuming quadratic 00097 00099 static const unsigned SPATIAL_BLOCK_SIZE_ELEMENTAL = DIM*NUM_NODES_PER_ELEMENT; 00101 static const unsigned PRESSURE_BLOCK_SIZE_ELEMENTAL = NUM_VERTICES_PER_ELEMENT; 00102 00104 static const unsigned STENCIL_SIZE = DIM*NUM_NODES_PER_ELEMENT + NUM_VERTICES_PER_ELEMENT; 00105 00106 protected: 00108 QuadraticMesh<DIM>* mpMesh; 00109 00111 GaussianQuadratureRule<DIM>* mpQuadRule; 00112 00119 void DoAssemble(); 00120 00121 00140 virtual c_matrix<double,SPATIAL_BLOCK_SIZE_ELEMENTAL,SPATIAL_BLOCK_SIZE_ELEMENTAL> ComputeSpatialSpatialMatrixTerm( 00141 c_vector<double, NUM_NODES_PER_ELEMENT>& rQuadPhi, 00142 c_matrix<double, DIM, NUM_NODES_PER_ELEMENT>& rGradQuadPhi, 00143 c_vector<double,DIM>& rX, 00144 Element<DIM,DIM>* pElement) 00145 { 00146 return zero_matrix<double>(SPATIAL_BLOCK_SIZE_ELEMENTAL,SPATIAL_BLOCK_SIZE_ELEMENTAL); 00147 } 00148 00170 virtual c_matrix<double,SPATIAL_BLOCK_SIZE_ELEMENTAL,PRESSURE_BLOCK_SIZE_ELEMENTAL> ComputeSpatialPressureMatrixTerm( 00171 c_vector<double, NUM_NODES_PER_ELEMENT>& rQuadPhi, 00172 c_matrix<double, DIM, NUM_NODES_PER_ELEMENT>& rGradQuadPhi, 00173 c_vector<double, NUM_VERTICES_PER_ELEMENT>& rLinearPhi, 00174 c_matrix<double, DIM, NUM_VERTICES_PER_ELEMENT>& rGradLinearPhi, 00175 c_vector<double,DIM>& rX, 00176 Element<DIM,DIM>* pElement) 00177 { 00178 return zero_matrix<double>(SPATIAL_BLOCK_SIZE_ELEMENTAL,PRESSURE_BLOCK_SIZE_ELEMENTAL); 00179 } 00180 00181 00200 virtual c_matrix<double,PRESSURE_BLOCK_SIZE_ELEMENTAL,PRESSURE_BLOCK_SIZE_ELEMENTAL> ComputePressurePressureMatrixTerm( 00201 c_vector<double, NUM_VERTICES_PER_ELEMENT>& rLinearPhi, 00202 c_matrix<double, DIM, NUM_VERTICES_PER_ELEMENT>& rGradLinearPhi, 00203 c_vector<double,DIM>& rX, 00204 Element<DIM,DIM>* pElement) 00205 { 00206 return zero_matrix<double>(PRESSURE_BLOCK_SIZE_ELEMENTAL,PRESSURE_BLOCK_SIZE_ELEMENTAL); 00207 } 00208 00209 00231 virtual c_vector<double,SPATIAL_BLOCK_SIZE_ELEMENTAL> ComputeSpatialVectorTerm( 00232 c_vector<double, NUM_NODES_PER_ELEMENT>& rQuadPhi, 00233 c_matrix<double, DIM, NUM_NODES_PER_ELEMENT>& rGradQuadPhi, 00234 c_vector<double,DIM>& rX, 00235 Element<DIM,DIM>* pElement) 00236 { 00237 return zero_vector<double>(SPATIAL_BLOCK_SIZE_ELEMENTAL); 00238 } 00239 00240 00262 virtual c_vector<double,PRESSURE_BLOCK_SIZE_ELEMENTAL> ComputePressureVectorTerm( 00263 c_vector<double, NUM_VERTICES_PER_ELEMENT>& rLinearPhi, 00264 c_matrix<double, DIM, NUM_VERTICES_PER_ELEMENT>& rGradLinearPhi, 00265 c_vector<double,DIM>& rX, 00266 Element<DIM,DIM>* pElement) 00267 { 00268 return zero_vector<double>(PRESSURE_BLOCK_SIZE_ELEMENTAL); 00269 } 00270 00271 00272 00284 void AssembleOnElement(Element<DIM, DIM>& rElement, 00285 c_matrix<double, STENCIL_SIZE, STENCIL_SIZE >& rAElem, 00286 c_vector<double, STENCIL_SIZE>& rBElem); 00287 00288 public: 00292 AbstractContinuumMechanicsAssembler(QuadraticMesh<DIM>* pMesh, unsigned numQuadPoints = 3) 00293 : AbstractFeAssemblerInterface<CAN_ASSEMBLE_VECTOR,CAN_ASSEMBLE_MATRIX>(), 00294 mpMesh(pMesh) 00295 { 00296 assert(pMesh); 00297 mpQuadRule = new GaussianQuadratureRule<DIM>(numQuadPoints); 00298 } 00299 00300 00301 // void SetCurrentSolution(Vec currentSolution); 00302 00303 00307 virtual ~AbstractContinuumMechanicsAssembler() 00308 { 00309 delete mpQuadRule; 00310 } 00311 }; 00312 00313 00315 //template<unsigned DIM, bool CAN_ASSEMBLE_VECTOR, bool CAN_ASSEMBLE_MATRIX> 00316 //void AbstractContinuumMechanicsAssembler<DIM,CAN_ASSEMBLE_VECTOR,CAN_ASSEMBLE_MATRIX>::SetCurrentSolution(Vec currentSolution) 00317 //{ 00318 // assert(currentSolution != NULL); 00319 // 00320 // // Replicate the current solution and store so can be used in AssembleOnElement 00321 // HeartEventHandler::BeginEvent(HeartEventHandler::COMMUNICATION); 00322 // mCurrentSolutionOrGuessReplicated.ReplicatePetscVector(currentSolution); 00323 // HeartEventHandler::EndEvent(HeartEventHandler::COMMUNICATION); 00324 // 00325 // // The AssembleOnElement type methods will determine if a current solution or 00326 // // current guess exists by looking at the size of the replicated vector, so 00327 // // check the size is zero if there isn't a current solution. 00328 // assert(mCurrentSolutionOrGuessReplicated.GetSize() > 0); 00329 //} 00330 00331 template<unsigned DIM, bool CAN_ASSEMBLE_VECTOR, bool CAN_ASSEMBLE_MATRIX> 00332 void AbstractContinuumMechanicsAssembler<DIM,CAN_ASSEMBLE_VECTOR,CAN_ASSEMBLE_MATRIX>::DoAssemble() 00333 { 00334 assert(this->mAssembleMatrix || this->mAssembleVector); 00335 if (this->mAssembleMatrix) 00336 { 00337 if(this->mMatrixToAssemble==NULL) 00338 { 00339 EXCEPTION("Matrix to be assembled has not been set"); 00340 } 00341 if( PetscMatTools::GetSize(this->mMatrixToAssemble) != (DIM+1)*mpMesh->GetNumNodes() ) 00342 { 00343 EXCEPTION("Matrix provided to be assembled has size " << PetscMatTools::GetSize(this->mMatrixToAssemble) << ", not expected size of " << (DIM+1)*mpMesh->GetNumNodes() << " ((dim+1)*num_nodes)"); 00344 } 00345 } 00346 00347 if (this->mAssembleVector) 00348 { 00349 if(this->mVectorToAssemble==NULL) 00350 { 00351 EXCEPTION("Vector to be assembled has not been set"); 00352 } 00353 if( PetscVecTools::GetSize(this->mVectorToAssemble) != (DIM+1)*mpMesh->GetNumNodes() ) 00354 { 00355 EXCEPTION("Vector provided to be assembled has size " << PetscVecTools::GetSize(this->mVectorToAssemble) << ", not expected size of " << (DIM+1)*mpMesh->GetNumNodes() << " ((dim+1)*num_nodes)"); 00356 } 00357 } 00358 00359 // Zero the matrix/vector if it is to be assembled 00360 if (this->mAssembleVector && this->mZeroVectorBeforeAssembly) 00361 { 00362 // Note PetscVecTools::Finalise(this->mVectorToAssemble); on an unused matrix 00363 // would "compress" data and make any pre-allocated entries redundant. 00364 PetscVecTools::Zero(this->mVectorToAssemble); 00365 } 00366 if (this->mAssembleMatrix && this->mZeroMatrixBeforeAssembly) 00367 { 00368 // Note PetscMatTools::Finalise(this->mMatrixToAssemble); on an unused matrix 00369 // would "compress" data and make any pre-allocated entries redundant. 00370 PetscMatTools::Zero(this->mMatrixToAssemble); 00371 } 00372 00373 c_matrix<double, STENCIL_SIZE, STENCIL_SIZE> a_elem = zero_matrix<double>(STENCIL_SIZE,STENCIL_SIZE); 00374 c_vector<double, STENCIL_SIZE> b_elem = zero_vector<double>(STENCIL_SIZE); 00375 00376 00377 // Loop over elements 00378 for (typename AbstractTetrahedralMesh<DIM, DIM>::ElementIterator iter = mpMesh->GetElementIteratorBegin(); 00379 iter != mpMesh->GetElementIteratorEnd(); 00380 ++iter) 00381 { 00382 Element<DIM, DIM>& r_element = *iter; 00383 00384 // Test for ownership first, since it's pointless to test the criterion on something which we might know nothing about. 00385 if ( r_element.GetOwnership() == true /*&& ElementAssemblyCriterion(r_element)==true*/ ) 00386 { 00387 #define COVERAGE_IGNORE 00388 // note: if assemble matrix only 00389 if(CommandLineArguments::Instance()->OptionExists("-mech_very_verbose") && this->mAssembleMatrix) 00390 { 00391 std::cout << "\r[" << PetscTools::GetMyRank() << "]: Element " << r_element.GetIndex() << " of " << mpMesh->GetNumElements() << std::flush; 00392 } 00393 #undef COVERAGE_IGNORE 00394 00395 00396 AssembleOnElement(r_element, a_elem, b_elem); 00397 00398 // Note that a different ordering is used for the elemental matrix compared to the global matrix. 00399 // See comments about ordering above. 00400 unsigned p_indices[STENCIL_SIZE]; 00401 // Work out the mapping for spatial terms 00402 for (unsigned i=0; i<NUM_NODES_PER_ELEMENT; i++) 00403 { 00404 for (unsigned j=0; j<DIM; j++) 00405 { 00406 // DIM+1 on the right-hand side here is the problem dimension 00407 p_indices[DIM*i+j] = (DIM+1)*r_element.GetNodeGlobalIndex(i) + j; 00408 } 00409 } 00410 // Work out the mapping for pressure terms 00411 for (unsigned i=0; i<NUM_VERTICES_PER_ELEMENT; i++) 00412 { 00413 p_indices[DIM*NUM_NODES_PER_ELEMENT + i] = (DIM+1)*r_element.GetNodeGlobalIndex(i)+DIM; 00414 } 00415 00416 00417 if (this->mAssembleMatrix) 00418 { 00419 PetscMatTools::AddMultipleValues<STENCIL_SIZE>(this->mMatrixToAssemble, p_indices, a_elem); 00420 } 00421 00422 if (this->mAssembleVector) 00423 { 00424 PetscVecTools::AddMultipleValues<STENCIL_SIZE>(this->mVectorToAssemble, p_indices, b_elem); 00425 } 00426 } 00427 } 00428 } 00429 00430 template<unsigned DIM, bool CAN_ASSEMBLE_VECTOR, bool CAN_ASSEMBLE_MATRIX> 00431 void AbstractContinuumMechanicsAssembler<DIM,CAN_ASSEMBLE_VECTOR,CAN_ASSEMBLE_MATRIX>::AssembleOnElement(Element<DIM, DIM>& rElement, 00432 c_matrix<double, STENCIL_SIZE, STENCIL_SIZE >& rAElem, 00433 c_vector<double, STENCIL_SIZE>& rBElem) 00434 { 00435 static c_matrix<double,DIM,DIM> jacobian; 00436 static c_matrix<double,DIM,DIM> inverse_jacobian; 00437 double jacobian_determinant; 00438 00439 mpMesh->GetInverseJacobianForElement(rElement.GetIndex(), jacobian, jacobian_determinant, inverse_jacobian); 00440 00441 if (this->mAssembleMatrix) 00442 { 00443 rAElem.clear(); 00444 } 00445 00446 if (this->mAssembleVector) 00447 { 00448 rBElem.clear(); 00449 } 00450 00451 00452 // Allocate memory for the basis functions values and derivative values 00453 static c_vector<double, NUM_VERTICES_PER_ELEMENT> linear_phi; 00454 static c_vector<double, NUM_NODES_PER_ELEMENT> quad_phi; 00455 static c_matrix<double, DIM, NUM_NODES_PER_ELEMENT> grad_quad_phi; 00456 static c_matrix<double, DIM, NUM_VERTICES_PER_ELEMENT> grad_linear_phi; 00457 00458 c_vector<double,DIM> body_force; 00459 00460 // Loop over Gauss points 00461 for (unsigned quadrature_index=0; quadrature_index < mpQuadRule->GetNumQuadPoints(); quadrature_index++) 00462 { 00463 double wJ = jacobian_determinant * mpQuadRule->GetWeight(quadrature_index); 00464 const ChastePoint<DIM>& quadrature_point = mpQuadRule->rGetQuadPoint(quadrature_index); 00465 00466 // Set up basis function info 00467 LinearBasisFunction<DIM>::ComputeBasisFunctions(quadrature_point, linear_phi); 00468 QuadraticBasisFunction<DIM>::ComputeBasisFunctions(quadrature_point, quad_phi); 00469 QuadraticBasisFunction<DIM>::ComputeTransformedBasisFunctionDerivatives(quadrature_point, inverse_jacobian, grad_quad_phi); 00470 LinearBasisFunction<DIM>::ComputeTransformedBasisFunctionDerivatives(quadrature_point, inverse_jacobian, grad_linear_phi); 00471 00472 // interpolate X (ie physical location of this quad point). 00473 c_vector<double,DIM> X = zero_vector<double>(DIM); 00474 for (unsigned vertex_index=0; vertex_index<NUM_VERTICES_PER_ELEMENT; vertex_index++) 00475 { 00476 for(unsigned j=0; j<DIM; j++) 00477 { 00478 X(j) += linear_phi(vertex_index)*rElement.GetNode(vertex_index)->rGetLocation()(j); 00479 } 00480 } 00481 00482 if(this->mAssembleVector) 00483 { 00484 c_vector<double,SPATIAL_BLOCK_SIZE_ELEMENTAL> b_spatial 00485 = ComputeSpatialVectorTerm(quad_phi, grad_quad_phi, X, &rElement); 00486 c_vector<double,PRESSURE_BLOCK_SIZE_ELEMENTAL> b_pressure = ComputePressureVectorTerm(linear_phi, grad_linear_phi, X, &rElement); 00487 00488 for (unsigned i=0; i<SPATIAL_BLOCK_SIZE_ELEMENTAL; i++) 00489 { 00490 rBElem(i) += b_spatial(i)*wJ; 00491 } 00492 00493 00494 for (unsigned i=0; i<PRESSURE_BLOCK_SIZE_ELEMENTAL; i++) 00495 { 00496 rBElem(SPATIAL_BLOCK_SIZE_ELEMENTAL + i) += b_pressure(i)*wJ; 00497 } 00498 } 00499 00500 00501 if(this->mAssembleMatrix) 00502 { 00503 c_matrix<double,SPATIAL_BLOCK_SIZE_ELEMENTAL,SPATIAL_BLOCK_SIZE_ELEMENTAL> a_spatial_spatial 00504 = ComputeSpatialSpatialMatrixTerm(quad_phi, grad_quad_phi, X, &rElement); 00505 00506 c_matrix<double,SPATIAL_BLOCK_SIZE_ELEMENTAL,PRESSURE_BLOCK_SIZE_ELEMENTAL> a_spatial_pressure 00507 = ComputeSpatialPressureMatrixTerm(quad_phi, grad_quad_phi, linear_phi, grad_linear_phi, X, &rElement); 00508 00509 c_matrix<double,PRESSURE_BLOCK_SIZE_ELEMENTAL,SPATIAL_BLOCK_SIZE_ELEMENTAL> a_pressure_spatial; 00510 if(!BLOCK_SYMMETRIC_MATRIX) 00511 { 00512 NEVER_REACHED; // to-come: non-mixed problems 00513 //a_pressure_spatial = ComputeSpatialPressureMatrixTerm(quad_phi, grad_quad_phi, lin_phi, grad_lin_phi, x, &rElement); 00514 } 00515 00516 c_matrix<double,PRESSURE_BLOCK_SIZE_ELEMENTAL,PRESSURE_BLOCK_SIZE_ELEMENTAL> a_pressure_pressure 00517 = ComputePressurePressureMatrixTerm(linear_phi, grad_linear_phi, X, &rElement); 00518 00519 for (unsigned i=0; i<SPATIAL_BLOCK_SIZE_ELEMENTAL; i++) 00520 { 00521 for(unsigned j=0; j<SPATIAL_BLOCK_SIZE_ELEMENTAL; j++) 00522 { 00523 rAElem(i,j) += a_spatial_spatial(i,j)*wJ; 00524 } 00525 00526 for(unsigned j=0; j<PRESSURE_BLOCK_SIZE_ELEMENTAL; j++) 00527 { 00528 rAElem(i, SPATIAL_BLOCK_SIZE_ELEMENTAL + j) += a_spatial_pressure(i,j)*wJ; 00529 } 00530 } 00531 00532 for(unsigned i=0; i<PRESSURE_BLOCK_SIZE_ELEMENTAL; i++) 00533 { 00534 if(BLOCK_SYMMETRIC_MATRIX) 00535 { 00536 for(unsigned j=0; j<SPATIAL_BLOCK_SIZE_ELEMENTAL; j++) 00537 { 00538 rAElem(SPATIAL_BLOCK_SIZE_ELEMENTAL + i, j) += a_spatial_pressure(j,i)*wJ; 00539 } 00540 } 00541 else 00542 { 00543 NEVER_REACHED; // to-come: non-mixed problems 00544 } 00545 00546 for(unsigned j=0; j<PRESSURE_BLOCK_SIZE_ELEMENTAL; j++) 00547 { 00548 rAElem(SPATIAL_BLOCK_SIZE_ELEMENTAL + i, SPATIAL_BLOCK_SIZE_ELEMENTAL + j) += a_pressure_pressure(i,j)*wJ; 00549 } 00550 } 00551 } 00552 } 00553 } 00554 00555 00556 #endif // ABSTRACTCONTINUUMMECHANICSASSEMBLER_HPP_