CellwiseDataGradient.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "CellwiseDataGradient.hpp"
00030 #include "LinearBasisFunction.hpp"
00031 
00032 template<unsigned DIM>
00033 c_vector<double, DIM>& CellwiseDataGradient<DIM>::rGetGradient(unsigned nodeIndex)
00034 {
00035     return mGradients[nodeIndex];
00036 }
00037 
00038 
00039 template<unsigned DIM>
00040 void CellwiseDataGradient<DIM>::SetupGradients()
00041 {
00042     MeshBasedCellPopulation<DIM>* p_cell_population = static_cast<MeshBasedCellPopulation<DIM>*>(&(CellwiseData<DIM>::Instance()->rGetCellPopulation()));
00043     TetrahedralMesh<DIM,DIM>& r_mesh = p_cell_population->rGetMesh();
00044 
00045     // Initialise gradients size
00046     unsigned num_nodes = p_cell_population->GetNumNodes();
00047     mGradients.resize(num_nodes, zero_vector<double>(DIM));
00048 
00049     // The constant gradients at each element
00050     std::vector<c_vector<double, DIM> > gradients_on_elements;
00051     unsigned num_elements = r_mesh.GetNumElements();
00052     gradients_on_elements.resize(num_elements, zero_vector<double>(DIM));
00053 
00054     // The number of elements containing a given node (excl ghost elements)
00055     std::vector<unsigned> num_real_elems_for_node(num_nodes, 0);
00056 
00057     for (unsigned elem_index=0; elem_index<num_elements; elem_index++)
00058     {
00059         Element<DIM,DIM>& r_elem = *(r_mesh.GetElement(elem_index));
00060 
00061         // Calculate the basis functions at any point (eg zero) in the element
00062         c_matrix<double, DIM, DIM> jacobian, inverse_jacobian;
00063         double jacobian_det;
00064         r_mesh.GetInverseJacobianForElement(elem_index, jacobian, jacobian_det, inverse_jacobian);
00065         const ChastePoint<DIM> zero_point;
00066         c_matrix<double, DIM, DIM+1> grad_phi;
00067         LinearBasisFunction<DIM>::ComputeTransformedBasisFunctionDerivatives(zero_point, inverse_jacobian, grad_phi);
00068 
00069         bool is_ghost_element = false;
00070 
00071         for (unsigned node_index=0; node_index<DIM+1; node_index++)
00072         {
00073             unsigned node_global_index = r_elem.GetNodeGlobalIndex(node_index);
00074 
00075             // This code is commented because CelwiseData Can't deal with ghost nodes see #1975
00076             assert(p_cell_population->IsGhostNode(node_global_index) == false);
00078             //if (p_cell_population->IsGhostNode(node_global_index) == true)
00079             //{
00080             //    is_ghost_element = true;
00081             //    break;
00082             //}
00083 
00084             // If no ghost element, get PDE solution
00085             CellPtr p_cell = p_cell_population->GetCellUsingLocationIndex(node_global_index);
00086             double pde_solution = CellwiseData<DIM>::Instance()->GetValue(p_cell, 0);
00087 
00088             // Interpolate gradient
00089             for (unsigned i=0; i<DIM; i++)
00090             {
00091                 gradients_on_elements[elem_index](i) += pde_solution* grad_phi(i, node_index);
00092             }
00093         }
00094 
00095         // Add gradient at element to gradient at node
00096         if (!is_ghost_element)
00097         {
00098             for (unsigned node_index=0; node_index<DIM+1; node_index++)
00099             {
00100                 unsigned node_global_index = r_elem.GetNodeGlobalIndex(node_index);
00101                 mGradients[node_global_index] += gradients_on_elements[elem_index];
00102                 num_real_elems_for_node[node_global_index]++;
00103             }
00104         }
00105     }
00106 
00107     // Divide to obtain average gradient
00108     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = p_cell_population->Begin();
00109          cell_iter != p_cell_population->End();
00110          ++cell_iter)
00111     {
00112         unsigned node_global_index = p_cell_population->GetLocationIndexUsingCell(*cell_iter);
00113 
00114         if (!num_real_elems_for_node[node_global_index] > 0)
00115         {
00116             NEVER_REACHED;
00117             // This code is commented because CellwiseData Can't deal with ghost nodes so won't ever come into this statement see #1975
00120             //Node<DIM>& this_node = *(p_cell_population->GetNodeCorrespondingToCell(*cell_iter));
00121             //
00122             //mGradients[node_global_index] = zero_vector<double>(DIM);
00123             //unsigned num_real_adjacent_nodes = 0;
00124             //
00126             //std::set<Node<DIM>*> real_adjacent_nodes;
00127             //real_adjacent_nodes.clear();
00128             //
00130             //for (typename Node<DIM>::ContainingElementIterator element_iter = this_node.ContainingElementsBegin();
00131             //     element_iter != this_node.ContainingElementsEnd();
00132             //     ++element_iter)
00133             //{
00134             //    // Then loop over nodes therein
00135             //    Element<DIM,DIM>& r_adjacent_elem = *(r_mesh.GetElement(*element_iter));
00136             //    for (unsigned local_node_index=0; local_node_index<DIM+1; local_node_index++)
00137             //    {
00138             //        unsigned adjacent_node_global_index = r_adjacent_elem.GetNodeGlobalIndex(local_node_index);
00139             //
00140             //        // If not a ghost node and not the node we started with
00141             //        if (    !(p_cell_population->IsGhostNode(adjacent_node_global_index))
00142             //             && adjacent_node_global_index != node_global_index )
00143             //        {
00144             //
00145             //            // Calculate the contribution of gradient from this node
00146             //            Node<DIM>& adjacent_node = *(r_mesh.GetNode(adjacent_node_global_index));
00147             //
00148             //            double this_cell_concentration = CellwiseData<DIM>::Instance()->GetValue(*cell_iter, 0);
00149             //            CellPtr p_adjacent_cell = p_cell_population->GetCellUsingLocationIndex(adjacent_node_global_index);
00150             //            double adjacent_cell_concentration = CellwiseData<DIM>::Instance()->GetValue(p_adjacent_cell, 0);
00151             //
00152             //            c_vector<double, DIM> gradient_contribution = zero_vector<double>(DIM);
00153             //
00154             //            if (fabs(this_cell_concentration-adjacent_cell_concentration) > 100*DBL_EPSILON)
00155             //            {
00156             //                c_vector<double, DIM> edge_vector = r_mesh.GetVectorFromAtoB(this_node.rGetLocation(), adjacent_node.rGetLocation());
00157             //                double norm_edge_vector = norm_2(edge_vector);
00158             //                gradient_contribution = edge_vector
00159             //                                            * (adjacent_cell_concentration - this_cell_concentration)
00160             //                                            / (norm_edge_vector * norm_edge_vector);
00161             //            }
00162             //
00163             //            mGradients[node_global_index] += gradient_contribution;
00164             //            num_real_adjacent_nodes++;
00165             //        }
00166             //    }
00167             //}
00168             //mGradients[node_global_index] /= num_real_adjacent_nodes;
00169         }
00170         else
00171         {
00172             mGradients[node_global_index] /= num_real_elems_for_node[node_global_index];
00173         }
00174     }
00175 }
00176 
00178 // Explicit instantiation
00180 
00181 template class CellwiseDataGradient<1>;
00182 template class CellwiseDataGradient<2>;
00183 template class CellwiseDataGradient<3>;
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3