CellwiseDataGradient.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, 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 #include "CellwiseDataGradient.hpp"
00037 #include "LinearBasisFunction.hpp"
00038 
00039 template<unsigned DIM>
00040 c_vector<double, DIM>& CellwiseDataGradient<DIM>::rGetGradient(unsigned nodeIndex)
00041 {
00042     return mGradients[nodeIndex];
00043 }
00044 
00045 template<unsigned DIM>
00046 void CellwiseDataGradient<DIM>::SetupGradients(AbstractCellPopulation<DIM>& rCellPopulation, const std::string& rItemName)
00047 {
00048     MeshBasedCellPopulation<DIM>* pCellPopulation = static_cast<MeshBasedCellPopulation<DIM>*>(&(rCellPopulation));
00049     TetrahedralMesh<DIM,DIM>& r_mesh = pCellPopulation->rGetMesh();
00050 
00051     // Initialise gradients size
00052     unsigned num_nodes = pCellPopulation->GetNumNodes();
00053     mGradients.resize(num_nodes, zero_vector<double>(DIM));
00054 
00055     // The constant gradients at each element
00056     std::vector<c_vector<double, DIM> > gradients_on_elements;
00057     unsigned num_elements = r_mesh.GetNumElements();
00058     gradients_on_elements.resize(num_elements, zero_vector<double>(DIM));
00059 
00060     // The number of elements containing a given node (excl ghost elements)
00061     std::vector<unsigned> num_real_elems_for_node(num_nodes, 0);
00062 
00063     for (unsigned elem_index=0; elem_index<num_elements; elem_index++)
00064     {
00065         Element<DIM,DIM>& r_elem = *(r_mesh.GetElement(elem_index));
00066 
00067         // Calculate the basis functions at any point (eg zero) in the element
00068         c_matrix<double, DIM, DIM> jacobian, inverse_jacobian;
00069         double jacobian_det;
00070         r_mesh.GetInverseJacobianForElement(elem_index, jacobian, jacobian_det, inverse_jacobian);
00071         const ChastePoint<DIM> zero_point;
00072         c_matrix<double, DIM, DIM+1> grad_phi;
00073         LinearBasisFunction<DIM>::ComputeTransformedBasisFunctionDerivatives(zero_point, inverse_jacobian, grad_phi);
00074 
00075         bool is_ghost_element = false;
00076 
00077         for (unsigned node_index=0; node_index<DIM+1; node_index++)
00078         {
00079             unsigned node_global_index = r_elem.GetNodeGlobalIndex(node_index);
00080 
00081             // This code is commented because CellData can't deal with ghost nodes see #1975
00082             assert(pCellPopulation->IsGhostNode(node_global_index) == false);
00084             //if (pCellPopulation->IsGhostNode(node_global_index) == true)
00085             //{
00086             //    is_ghost_element = true;
00087             //    break;
00088             //}
00089 
00090             // If no ghost element, get PDE solution
00091             CellPtr p_cell = pCellPopulation->GetCellUsingLocationIndex(node_global_index);
00092             double pde_solution = p_cell->GetCellData()->GetItem(rItemName);
00093 
00094             // Interpolate gradient
00095             for (unsigned i=0; i<DIM; i++)
00096             {
00097                 gradients_on_elements[elem_index](i) += pde_solution* grad_phi(i, node_index);
00098             }
00099         }
00100 
00101         // Add gradient at element to gradient at node
00102         if (!is_ghost_element)
00103         {
00104             for (unsigned node_index=0; node_index<DIM+1; node_index++)
00105             {
00106                 unsigned node_global_index = r_elem.GetNodeGlobalIndex(node_index);
00107                 mGradients[node_global_index] += gradients_on_elements[elem_index];
00108                 num_real_elems_for_node[node_global_index]++;
00109             }
00110         }
00111     }
00112 
00113     // Divide to obtain average gradient
00114     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = pCellPopulation->Begin();
00115          cell_iter != pCellPopulation->End();
00116          ++cell_iter)
00117     {
00118         unsigned node_global_index = pCellPopulation->GetLocationIndexUsingCell(*cell_iter);
00119 
00120         if (!(num_real_elems_for_node[node_global_index] > 0))
00121         {
00122             NEVER_REACHED;
00123             // This code is commented because CellwiseData Can't deal with ghost nodes so won't ever come into this statement see #1975
00126             //Node<DIM>& this_node = *(pCellPopulation->GetNodeCorrespondingToCell(*cell_iter));
00127             //
00128             //mGradients[node_global_index] = zero_vector<double>(DIM);
00129             //unsigned num_real_adjacent_nodes = 0;
00130             //
00132             //std::set<Node<DIM>*> real_adjacent_nodes;
00133             //real_adjacent_nodes.clear();
00134             //
00136             //for (typename Node<DIM>::ContainingElementIterator element_iter = this_node.ContainingElementsBegin();
00137             //     element_iter != this_node.ContainingElementsEnd();
00138             //     ++element_iter)
00139             //{
00140             //    // Then loop over nodes therein
00141             //    Element<DIM,DIM>& r_adjacent_elem = *(r_mesh.GetElement(*element_iter));
00142             //    for (unsigned local_node_index=0; local_node_index<DIM+1; local_node_index++)
00143             //    {
00144             //        unsigned adjacent_node_global_index = r_adjacent_elem.GetNodeGlobalIndex(local_node_index);
00145             //
00146             //        // If not a ghost node and not the node we started with
00147             //        if (    !(pCellPopulation->IsGhostNode(adjacent_node_global_index))
00148             //             && adjacent_node_global_index != node_global_index )
00149             //        {
00150             //
00151             //            // Calculate the contribution of gradient from this node
00152             //            Node<DIM>& adjacent_node = *(r_mesh.GetNode(adjacent_node_global_index));
00153             //
00154             //            double this_cell_concentration = CellwiseData<DIM>::Instance()->GetValue(*cell_iter, 0);
00155             //            CellPtr p_adjacent_cell = pCellPopulation->GetCellUsingLocationIndex(adjacent_node_global_index);
00156             //            double adjacent_cell_concentration = CellwiseData<DIM>::Instance()->GetValue(p_adjacent_cell, 0);
00157             //
00158             //            c_vector<double, DIM> gradient_contribution = zero_vector<double>(DIM);
00159             //
00160             //            if (fabs(this_cell_concentration-adjacent_cell_concentration) > 100*DBL_EPSILON)
00161             //            {
00162             //                c_vector<double, DIM> edge_vector = r_mesh.GetVectorFromAtoB(this_node.rGetLocation(), adjacent_node.rGetLocation());
00163             //                double norm_edge_vector = norm_2(edge_vector);
00164             //                gradient_contribution = edge_vector
00165             //                                            * (adjacent_cell_concentration - this_cell_concentration)
00166             //                                            / (norm_edge_vector * norm_edge_vector);
00167             //            }
00168             //
00169             //            mGradients[node_global_index] += gradient_contribution;
00170             //            num_real_adjacent_nodes++;
00171             //        }
00172             //    }
00173             //}
00174             //mGradients[node_global_index] /= num_real_adjacent_nodes;
00175         }
00176         else
00177         {
00178             mGradients[node_global_index] /= num_real_elems_for_node[node_global_index];
00179         }
00180     }
00181 }
00182 
00184 // Explicit instantiation
00186 
00187 template class CellwiseDataGradient<1>;
00188 template class CellwiseDataGradient<2>;
00189 template class CellwiseDataGradient<3>;

Generated by  doxygen 1.6.2