AbstractConductivityTensors.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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
00191
00192 template class AbstractConductivityTensors<1>;
00193 template class AbstractConductivityTensors<2>;
00194 template class AbstractConductivityTensors<3>;