![]() |
MechSys
1.0
Computing library for simulations in continuum and discrete mechanics
|
00001 /************************************************************************ 00002 * MechSys - Open Library for Mechanical Systems * 00003 * Copyright (C) 2005 Dorival M. Pedroso * 00004 * * 00005 * This program is free software: you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation, either version 3 of the License, or * 00008 * any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program. If not, see <http://www.gnu.org/licenses/> * 00017 ************************************************************************/ 00018 00019 /* Output - Copyright (C) 2007 Dorival de Moraes Pedroso */ 00020 00021 #ifndef MPM_OUTPUT_H 00022 #define MPM_OUTPUT_H 00023 00024 // STL 00025 #include <iostream> 00026 #include <fstream> 00027 #include <string> 00028 00029 // External 00030 #include <sys/stat.h> // for mkdir() 00031 00032 // MechSys 00033 #include <mechsys/util/array.h> 00034 #include <mechsys/util/fatal.h> 00035 00036 // Local 00037 #include <mechsys/mpm/defs.h> 00038 00039 namespace MPM { 00040 00042 inline void SeparatedLine (std::string const & Line, std::string const & Separator, Array<std::string> & Res) 00043 { 00044 /* Ex.: /home/dorival/teste/A File.txt 00045 * R[0] R[1] R[2] R[3] 00046 * 00047 * ->1234->456a->789as 00048 * R[0] R[1] R[2] 00049 */ 00050 00051 // Clear resulting array 00052 Res.Resize (0); 00053 00054 // Fill resulting array 00055 if (Separator.empty()) Res.Push (Line); 00056 else 00057 { 00058 // Loop along the line 00059 size_t start = 0; 00060 size_t end = Line.find (Separator, start); 00061 while (end!=std::string::npos) 00062 { 00063 Res.Push (Line.substr(start, end-start)); 00064 start = end + Separator.size(); 00065 end = Line.find (Separator, start); 00066 } 00067 Res.Push (Line.substr(start)); 00068 } 00069 } 00070 00072 bool MkdirP (char const * Path) 00073 { 00074 // Tree 00075 Array<std::string> tree; 00076 SeparatedLine (Path, "/", tree); 00077 00078 int res = -1; 00079 if (tree.Size()>0) 00080 { 00081 std::string left = ""; 00082 for (size_t i=0; i<tree.Size(); ++i) 00083 { 00084 left += tree[i] + "/"; 00085 // Upon successful completion, mkdir() shall return 0. 00086 // Otherwise, -1 shall be returned, no directory shall be created, and errno shall be set to indicate the error. 00087 res = mkdir (left.c_str(),S_IRWXU); //if (res!=) // directory may be existent, so, it is ok... 00088 } 00089 } 00090 00091 return res==0 ? true : false; 00092 } 00093 00095 namespace VTU 00096 { 00097 00098 namespace Out 00099 { 00100 00102 inline void PointDataVectors (Array<Vector3D> const & V, char const * Name, std::ostream & F) 00103 { 00104 F << " <DataArray type=\"Float64\" Name=\"" << Name << "\" format=\"ascii\" NumberOfComponents=\"3\">" << std::endl; 00105 int k = 0; F << " "; 00106 for (size_t i=0; i<V.Size(); ++i) 00107 { 00108 F << " " << V[i](0) << " "; 00109 F << V[i](1) << " "; 00110 F << V[i](2); 00111 k++; 00112 if (k>7) { F<<std::endl; if (i<V.Size()-1) F<<" "; k=0; } 00113 else { if (i==V.Size()-1) F<<std::endl; } 00114 } 00115 F << " </DataArray>" << std::endl; 00116 } 00117 00119 inline void PointDataStressComp (Array<EquilibState*> const & S, int iComp, std::ostream & F) 00120 { 00121 char name[32]; 00122 double coef = 1.0; 00123 switch (iComp) 00124 { 00125 case 0: { snprintf(name, 32, "stress_xx"); break; } 00126 case 1: { snprintf(name, 32, "stress_yy"); break; } 00127 case 2: { snprintf(name, 32, "stress_zz"); break; } 00128 case 3: { snprintf(name, 32, "stress_xy"); coef=SQ2; break; } 00129 case 4: { snprintf(name, 32, "stress_yz"); coef=SQ2; break; } 00130 case 5: { snprintf(name, 32, "stress_xz"); coef=SQ2; break; } 00131 default: { throw new Fatal("VTU::Out::PointsStressComp: Invalid iComp=%d. Must be 0<=iComp<=5.",iComp); } 00132 } 00133 F << " <DataArray type=\"Float64\" Name=\"" << name << "\" format=\"ascii\">" << std::endl; 00134 int k = 0; F << " "; 00135 for (size_t i=0; i<S.Size(); ++i) 00136 { 00137 if (iComp>(int)size(S[i]->Sig)-1) F << " " << 0.0; 00138 else F << " " << S[i]->Sig(iComp)*coef; 00139 k++; 00140 if (k>3*7) { F<<std::endl; if (i<S.Size()-1) F<<" "; k=0; } 00141 else { if (i==S.Size()-1) F<<std::endl; } 00142 } 00143 F << " </DataArray>" << std::endl; 00144 } 00145 00147 inline void PointDataStrainComp (Array<EquilibState*> const & S, int iComp, std::ostream & F) 00148 { 00149 char name[32]; 00150 double coef = 1.0; 00151 switch (iComp) 00152 { 00153 case 0: { snprintf(name, 32, "strain_xx"); break; } 00154 case 1: { snprintf(name, 32, "strain_yy"); break; } 00155 case 2: { snprintf(name, 32, "strain_zz"); break; } 00156 case 3: { snprintf(name, 32, "strain_xy"); coef=SQ2; break; } 00157 case 4: { snprintf(name, 32, "strain_yz"); coef=SQ2; break; } 00158 case 5: { snprintf(name, 32, "strain_xz"); coef=SQ2; break; } 00159 default: { throw new Fatal("VTU::Out::PointsStrainComp: Invalid iComp=%d. Must be 0<=iComp<=5.",iComp); } 00160 } 00161 F << " <DataArray type=\"Float64\" Name=\"" << name << "\" format=\"ascii\">" << std::endl; 00162 int k = 0; F << " "; 00163 for (size_t i=0; i<S.Size(); ++i) 00164 { 00165 if (iComp>(int)size(S[i]->Eps)-1) F << " " << 0.0; 00166 else F << " " << S[i]->Eps(iComp)*coef; 00167 k++; 00168 if (k>3*7) { F<<std::endl; if (i<S.Size()-1) F<<" "; k=0; } 00169 else { if (i==S.Size()-1) F<<std::endl; } 00170 } 00171 F << " </DataArray>" << std::endl; 00172 } 00173 00175 inline void PointDataTensors (Array<ATensor2> const & T, char const * Name, std::ostream & F) 00176 { 00177 F << " <DataArray type=\"Float64\" Name=\"" << Name << "\" format=\"ascii\" NumberOfComponents=\"9\">" << std::endl; 00178 int k = 0; F << " "; 00179 for (size_t i=0; i<T.Size(); ++i) 00180 { 00181 F << " " << T[i](0) << " " << T[i](3) << " " << T[i](5) << " "; 00182 F << " " << T[i](6) << " " << T[i](1) << " " << T[i](4) << " "; 00183 F << " " << T[i](8) << " " << T[i](7) << " " << T[i](2); 00184 k++; 00185 if (k>7) { F<<std::endl; if (i<T.Size()-1) F<<" "; k=0; } 00186 else { if (i==T.Size()-1) F<<std::endl; } 00187 } 00188 F << " </DataArray>" << std::endl; 00189 } 00190 00192 inline void PointDataTensors (Array<STensor2> const & T, char const * Name, std::ostream & F) 00193 { 00194 F << " <DataArray type=\"Float64\" Name=\"" << Name << "\" format=\"ascii\" NumberOfComponents=\"9\">" << std::endl; 00195 int k = 0; F << " "; 00196 for (size_t i=0; i<T.Size(); ++i) 00197 { 00198 F << " " << T[i](0) << " " << T[i](3)/SQ2 << " " << T[i](5)/SQ2 << " "; 00199 F << " " << T[i](3)/SQ2 << " " << T[i](1) << " " << T[i](4)/SQ2 << " "; 00200 F << " " << T[i](5)/SQ2 << " " << T[i](4)/SQ2 << " " << T[i](2); 00201 k++; 00202 if (k>7) { F<<std::endl; if (i<T.Size()-1) F<<" "; k=0; } 00203 else { if (i==T.Size()-1) F<<std::endl; } 00204 } 00205 F << " </DataArray>" << std::endl; 00206 } 00207 00209 inline void PointDataSig (Array<EquilibState*> const & S, char const * Name, std::ostream & F) 00210 { 00211 F << " <DataArray type=\"Float64\" Name=\"" << Name << "\" format=\"ascii\" NumberOfComponents=\"9\">" << std::endl; 00212 int k = 0; F << " "; 00213 for (size_t i=0; i<S.Size(); ++i) 00214 { 00215 if (size(S[i]->Sig)==4) 00216 { 00217 F << " " << S[i]->Sig(0) << " " << S[i]->Sig(3)/SQ2 << " " << 0.0 << " "; 00218 F << " " << S[i]->Sig(3)/SQ2 << " " << S[i]->Sig(1) << " " << 0.0 << " "; 00219 F << " " << 0.0 << " " << 0.0 << " " << S[i]->Sig(2); 00220 } 00221 else 00222 { 00223 F << " " << S[i]->Sig(0) << " " << S[i]->Sig(3)/SQ2 << " " << S[i]->Sig(5)/SQ2 << " "; 00224 F << " " << S[i]->Sig(3)/SQ2 << " " << S[i]->Sig(1) << " " << S[i]->Sig(4)/SQ2 << " "; 00225 F << " " << S[i]->Sig(5)/SQ2 << " " << S[i]->Sig(4)/SQ2 << " " << S[i]->Sig(2); 00226 } 00227 k++; 00228 if (k>7) { F<<std::endl; if (i<S.Size()-1) F<<" "; k=0; } 00229 else { if (i==S.Size()-1) F<<std::endl; } 00230 } 00231 F << " </DataArray>" << std::endl; 00232 } 00233 00235 inline void PointDataIvs (Array<EquilibState*> const & S, char const * Name, std::ostream & F) 00236 { 00237 size_t max_nivs = 0; 00238 for (size_t i=0; i<S.Size(); ++i) { size_t nivs = size(S[i]->Ivs); if (nivs>max_nivs) max_nivs = nivs; } 00239 F << " <DataArray type=\"Float64\" Name=\"ivs\" format=\"ascii\" NumberOfComponents=\""<<max_nivs<<"\">" << std::endl; 00240 int k = 0; F << " "; 00241 for (size_t i=0; i<S.Size(); ++i) 00242 { 00243 for (size_t iv=0; iv<max_nivs; ++iv) 00244 { 00245 if (size(S[i]->Ivs)>iv) F << " " << S[i]->Ivs(iv); 00246 else F << " " << 0.0; 00247 } 00248 k++; 00249 if (k>7) { F<<std::endl; if (i<S.Size()-1) F<<" "; k=0; } 00250 else { if (i==S.Size()-1) F<<std::endl; } 00251 } 00252 F << " </DataArray>" << std::endl; 00253 } 00254 00255 }; // namespace Out 00256 00257 }; // namespace VTU 00258 00259 }; // namespace MPM 00260 00261 #endif // MPM_OUTPUT_H