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

Generated on Wed Mar 18 12:51:51 2009 for Chaste by  doxygen 1.5.5