FourthOrderTensor.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 "FourthOrderTensor.hpp"
00030 
00032 // Implementation
00034 
00035 
00036 template<unsigned DIM>
00037 FourthOrderTensor<DIM>::FourthOrderTensor()
00038 {
00039     // check dim>0 but <4
00040     assert(DIM>0);
00041     assert(DIM<4);
00042 
00043     mDimSqd = DIM*DIM;
00044     mDimCubed = DIM*DIM*DIM;
00045     mDimToFour = DIM*DIM*DIM*DIM;
00046 
00047     // allocate memory and zero entries
00048     mData.resize(mDimToFour, 0.0);
00049 }
00050 
00051 template<unsigned DIM>
00052 void FourthOrderTensor<DIM>::SetAsProduct(FourthOrderTensor<DIM>& rTensor, const c_matrix<double,DIM,DIM>& rMatrix, unsigned component)
00053 {
00054     Zero();
00055 
00056     // messy repeated code but needs to be efficiently over neat..
00057     switch (component)
00058     {
00059         case 0:
00060         {
00061             for (unsigned M=0; M<DIM; M++)
00062             {
00063                 for (unsigned N=0; N<DIM; N++)
00064                 {
00065                     for (unsigned P=0; P<DIM; P++)
00066                     {
00067                         for (unsigned Q=0; Q<DIM; Q++)
00068                         {
00069                             unsigned index = M*mDimCubed + N*mDimSqd + P*DIM + Q;
00070                             for (unsigned s=0; s<DIM; s++)
00071                             {
00072                                 mData[index] += rMatrix(M,s) * rTensor(s,N,P,Q);
00073                             }
00074                         }
00075                     }
00076                 }
00077             }
00078             break;
00079         }
00080         case 1:
00081         {
00082             for (unsigned M=0; M<DIM; M++)
00083             {
00084                 for (unsigned N=0; N<DIM; N++)
00085                 {
00086                     for (unsigned P=0; P<DIM; P++)
00087                     {
00088                         for (unsigned Q=0; Q<DIM; Q++)
00089                         {
00090                             unsigned index = M*mDimCubed + N*mDimSqd + P*DIM + Q;
00091                             for (unsigned s=0; s<DIM; s++)
00092                             {
00093                                 mData[index] += rMatrix(N,s) * rTensor(M,s,P,Q);
00094                             }
00095                         }
00096                     }
00097                 }
00098             }
00099             break;
00100         }
00101         case 2:
00102         {
00103             for (unsigned M=0; M<DIM; M++)
00104             {
00105                 for (unsigned N=0; N<DIM; N++)
00106                 {
00107                     for (unsigned P=0; P<DIM; P++)
00108                     {
00109                         for (unsigned Q=0; Q<DIM; Q++)
00110                         {
00111                             unsigned index = M*mDimCubed + N*mDimSqd + P*DIM + Q;
00112                             for (unsigned s=0; s<DIM; s++)
00113                             {
00114                                 mData[index] += rMatrix(P,s) * rTensor(M,N,s,Q);
00115                             }
00116                         }
00117                     }
00118                 }
00119             }
00120             break;
00121         }
00122         case 3:
00123         {
00124             for (unsigned M=0; M<DIM; M++)
00125             {
00126                 for (unsigned N=0; N<DIM; N++)
00127                 {
00128                     for (unsigned P=0; P<DIM; P++)
00129                     {
00130                         for (unsigned Q=0; Q<DIM; Q++)
00131                         {
00132                             unsigned index = M*mDimCubed + N*mDimSqd + P*DIM + Q;
00133                             for (unsigned s=0; s<DIM; s++)
00134                             {
00135                                 mData[index] += rMatrix(Q,s) * rTensor(M,N,P,s);
00136                             }
00137                         }
00138                     }
00139                 }
00140             }
00141             break;
00142         }
00143         default:
00144         {
00145             EXCEPTION("Component not 0, 1, 2, or 3");
00146         }
00147     }
00148 }
00149 
00150 template<unsigned DIM>
00151 double& FourthOrderTensor<DIM>::operator()(unsigned M, unsigned N, unsigned P, unsigned Q)
00152 {
00153     assert(M<DIM);
00154     assert(N<DIM);
00155     assert(P<DIM);
00156     assert(Q<DIM);
00157 
00158     unsigned index = M*mDimCubed + N*mDimSqd + P*DIM + Q;
00159     return mData[index];
00160 }
00161 
00162 template<unsigned DIM>
00163 void FourthOrderTensor<DIM>::Zero()
00164 {
00165     for (unsigned i=0; i<mDimToFour; i++)
00166     {
00167         mData[i] = 0.0;
00168     }
00169 }
00170 
00172 // Explicit instantiation
00174 
00175 template class FourthOrderTensor<1>;
00176 template class FourthOrderTensor<2>;
00177 template class FourthOrderTensor<3>;

Generated on Tue Aug 4 16:10:22 2009 for Chaste by  doxygen 1.5.5