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