AbstractConductivityTensors.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 "AbstractConductivityTensors.hpp"
00030 #include "Exception.hpp"
00031 #include <sstream>
00032 
00033 template<unsigned SPACE_DIM>
00034 void AbstractConductivityTensors<SPACE_DIM>::OpenFibreOrientationFile()
00035 {
00036     mDataFile.open(mFibreOrientationFilename.c_str());
00037     if (!mDataFile.is_open())
00038     {
00039         EXCEPTION("Wrong fibre orientation file name "+mFibreOrientationFilename);
00040     }
00041 }
00042 
00043 template<unsigned SPACE_DIM>
00044 void AbstractConductivityTensors<SPACE_DIM>::CloseFibreOrientationFile()
00045 {
00046     mDataFile.close();
00047 }
00048 
00049 template<unsigned SPACE_DIM>
00050 unsigned AbstractConductivityTensors<SPACE_DIM>::GetTokensAtNextLine(std::vector<double>& rTokens)
00051 {
00052     std::string line;
00053 
00054     bool comment_line;
00055     bool blank_line;
00056     //We've assuming this is a fresh vector.  Why?
00057     assert(rTokens.size() == 0);
00058     do
00059     {
00060         getline(mDataFile, line);
00061 
00062         if (mDataFile.eof())
00063         {
00064             CloseFibreOrientationFile();
00065             EXCEPTION("Fibre orientation file contains less fibre definitions than the number of elements in the mesh");
00066         }
00067 
00068         comment_line = (line.find('#',0) != std::string::npos);
00069         blank_line = (line.find_first_not_of(" \t",0) == std::string::npos);
00070     }
00071     while(comment_line || blank_line);
00072 
00073 
00074     std::stringstream line_stream(line);
00075 
00076     // Read all the numbers from the line
00077     while (!line_stream.eof())
00078     {
00079         double item;
00080         line_stream >> item;
00081         rTokens.push_back(item);
00082     }
00083 
00084     return rTokens.size();
00085 }
00086 
00087 template<unsigned SPACE_DIM>
00088 unsigned AbstractConductivityTensors<SPACE_DIM>::GetNumElementsFromFile()
00089 {
00090     std::vector<double> tokens;
00091 
00092     if (GetTokensAtNextLine(tokens) != 1)
00093     {
00094         CloseFibreOrientationFile();
00095         EXCEPTION("First (non comment) line of the fibre orientation file should contain the number of elements of the mesh (and nothing else)");
00096     }
00097 
00098     return (unsigned) tokens[0];
00099 }
00100 
00101 
00102 template<unsigned SPACE_DIM>
00103 AbstractConductivityTensors<SPACE_DIM>::AbstractConductivityTensors()
00104     : mNumElements(1),
00105       mUseNonConstantConductivities(false),
00106       mUseFibreOrientation(false),
00107       mInitialised(false)
00108 {
00109     double init_data[]={DBL_MAX, DBL_MAX, DBL_MAX};
00110 
00111     for (unsigned dim=0; dim<SPACE_DIM; dim++)
00112     {
00113          mConstantConductivities[dim] = init_data[dim];
00114     }
00115 }
00116 
00117 template<unsigned SPACE_DIM>
00118 AbstractConductivityTensors<SPACE_DIM>::~AbstractConductivityTensors()
00119 {
00120 }
00121 
00122 template<unsigned SPACE_DIM>
00123 void AbstractConductivityTensors<SPACE_DIM>::SetFibreOrientationFile(const std::string &rFibreOrientationFilename)
00124 {
00125     mUseFibreOrientation = true;
00126     mFibreOrientationFilename = rFibreOrientationFilename;
00127 }
00128 
00129 template<unsigned SPACE_DIM>
00130 void AbstractConductivityTensors<SPACE_DIM>::SetConstantConductivities(c_vector<double, 1> constantConductivities)
00131 {
00132     if (SPACE_DIM != 1)
00133     {
00134         EXCEPTION("Wrong number of conductivities provided");
00135     }
00136 
00137     mUseNonConstantConductivities = false;
00138     mConstantConductivities = constantConductivities;
00139 }
00140 
00141 template<unsigned SPACE_DIM>
00142 void AbstractConductivityTensors<SPACE_DIM>::SetConstantConductivities(c_vector<double, 2> constantConductivities)
00143 {
00144     if (SPACE_DIM != 2)
00145     {
00146         EXCEPTION("Wrong number of conductivities provided");
00147     }
00148 
00149     mUseNonConstantConductivities = false;
00150     mConstantConductivities = constantConductivities;
00151 }
00152 
00153 template<unsigned SPACE_DIM>
00154 void AbstractConductivityTensors<SPACE_DIM>::SetConstantConductivities(c_vector<double, 3> constantConductivities)
00155 {
00156     if (SPACE_DIM != 3)
00157     {
00158         EXCEPTION("Wrong number of conductivities provided");
00159     }
00160 
00161     mUseNonConstantConductivities = false;
00162     mConstantConductivities = constantConductivities;
00163 }
00164 
00165 template<unsigned SPACE_DIM>
00166 void AbstractConductivityTensors<SPACE_DIM>::SetNonConstantConductivities(std::vector<c_vector<double, SPACE_DIM> >* pNonConstantConductivities)
00167 {
00168     mUseNonConstantConductivities = true;
00169     mpNonConstantConductivities = pNonConstantConductivities;
00170 }
00171 
00172 template<unsigned SPACE_DIM>
00173 c_matrix<double,SPACE_DIM,SPACE_DIM>& AbstractConductivityTensors<SPACE_DIM>::operator[](const unsigned index)
00174 {
00175     assert(mInitialised);
00176 
00177     if (!mUseNonConstantConductivities && !mUseFibreOrientation)
00178     {
00179         return mTensors[0];
00180     }
00181     else
00182     {
00183         assert(index < mNumElements);
00184         return mTensors[index];
00185     }
00186 }
00187 
00189 // Explicit instantiation
00191 
00192 template class AbstractConductivityTensors<1>;
00193 template class AbstractConductivityTensors<2>;
00194 template class AbstractConductivityTensors<3>;

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