39template<
typename TMethod,
41 typename TGrid =
typename std::conditional<stressType==Piola,Grid<Reference>,
Grid<Current>>::type>
98 for(
auto& matrix :
field)
99 matrix= Matrix3d::Zero();
100 if constexpr (stressType==
Cauchy)
115 for(
auto& matrix :
field)
116 matrix= Matrix3d::Zero();
117 if constexpr (stressType==
Cauchy)
149 MY_ERROR(
"Stress object created without specifying a name. Use write(filename) instead of write()");
150 std::ofstream file(
name+
".stress");
152 file <<
field.size() <<
"\n";
156 Eigen::IOFormat fmt(Eigen::FullPrecision, 0,
" ",
"\n",
"",
"",
"");
157 file << std::fixed << std::setprecision(std::numeric_limits<double>::max_digits10);
158 file <<
"Properties=pos:R:3:stress:R:6" << std::endl;
159 for (
auto& stress :
field)
163 file <<
pgrid->coordinates[index].format(fmt)
164 << std::setw(25) << stress(0,0)
165 << std::setw(25) << stress(1,1)
166 << std::setw(25) << stress(2,2)
167 << std::setw(25) << stress(0,1)
168 << std::setw(25) << stress(0,2)
169 << std::setw(25) << stress(1,2)
174 if constexpr (stressType==
Cauchy)
176 std::ofstream momentumDensityFile(
name+
".momentum_density");
177 std::ofstream massDensityFile(
name+
".mass_density");
180 momentumDensityFile << std::fixed << std::setprecision(std::numeric_limits<double>::max_digits10);
181 massDensityFile << std::fixed << std::setprecision(std::numeric_limits<double>::max_digits10);
182 momentumDensityFile <<
"Properties=pos:R:3:momentum_density:R:3" << std::endl;
183 massDensityFile <<
"Properties=pos:R:3:mass_density:R:1" << std::endl;
184 for (
int i_grid=0; i_grid<
pgrid->coordinates.size(); ++i_grid)
186 momentumDensityFile <<
pgrid->coordinates[i_grid].format(fmt)
191 massDensityFile <<
pgrid->coordinates[i_grid].format(fmt)
198 void write(
const std::string& filename)
203 std::cout <<
"Stress object created with name " <<
name <<
". Ignoring the filename: " << filename <<
"." << std::endl;
234 MY_ERROR(
"Stress object created without specifying a name. Use write_voxel_grid(filename,...) instead of write_voxel_grid(...)");
235 validateVoxelGridDimensions(nx,ny,nz);
237 std::ofstream stressFile(
name+
".voxel_grid_stress");
238 writeVoxelGridHeader(stressFile,nx,ny,nz,lowerLimit,upperLimit,
"SXX SYY SZZ SYZ SXZ SXY");
239 for (
const auto& stress :
field)
241 stressFile << std::setw(25) << stress(0,0)
242 << std::setw(25) << stress(1,1)
243 << std::setw(25) << stress(2,2)
244 << std::setw(25) << stress(1,2)
245 << std::setw(25) << stress(0,2)
246 << std::setw(25) << stress(0,1)
250 if constexpr (stressType==
Cauchy)
252 std::ofstream momentumDensityFile(
name+
".voxel_grid_momentum_density");
253 writeVoxelGridHeader(momentumDensityFile,nx,ny,nz,lowerLimit,upperLimit,
"PX PY PZ");
256 momentumDensityFile << std::setw(25) << momentumDensity(0)
257 << std::setw(25) << momentumDensity(1)
258 << std::setw(25) << momentumDensity(2)
262 std::ofstream massDensityFile(
name+
".voxel_grid_mass_density");
263 writeVoxelGridHeader(massDensityFile,nx,ny,nz,lowerLimit,upperLimit,
"RHO");
265 massDensityFile << std::setw(25) << massDensity << std::endl;
279 std::cout <<
"Stress object created with name " <<
name <<
". Ignoring the filename: " << filename <<
"." << std::endl;
289 void validateVoxelGridDimensions(
const int nx,
293 if (nx<=0 || ny<=0 || nz<=0)
294 MY_ERROR(
"Voxel grid dimensions must be positive.");
295 const auto numberOfGridPoints=
static_cast<std::size_t
>(nx)*
296 static_cast<std::size_t
>(ny)*
297 static_cast<std::size_t
>(nz);
298 if (numberOfGridPoints !=
field.size())
299 MY_ERROR(
"Voxel grid dimensions do not match the number of stress grid points.");
302 void writeVoxelGridHeader(std::ofstream& file,
308 const std::string& columns)
const
310 file << std::fixed << std::setprecision(std::numeric_limits<double>::max_digits10);
311 file <<
"ITEM: TIMESTEP\n";
313 const auto xBounds= voxelGridBounds(nx,lowerLimit(0),upperLimit(0));
314 const auto yBounds= voxelGridBounds(ny,lowerLimit(1),upperLimit(1));
315 const auto zBounds= voxelGridBounds(nz,lowerLimit(2),upperLimit(2));
316 file <<
"ITEM: BOX BOUNDS pp pp pp\n";
317 file << xBounds.first <<
" " << xBounds.second <<
"\n";
318 file << yBounds.first <<
" " << yBounds.second <<
"\n";
319 file << zBounds.first <<
" " << zBounds.second <<
"\n";
320 file <<
"ITEM: DIMENSION\n";
321 file << voxelGridDimension(nx,ny,nz) <<
"\n";
322 file <<
"ITEM: GRID SIZE nx ny nz\n";
323 file << nx <<
" " << ny <<
" " << nz <<
"\n";
324 file <<
"ITEM: GRID CELLS " << columns <<
"\n";
327 int voxelGridDimension(
const int nx,
332 if (nx>1) ++dimension;
333 if (ny>1) ++dimension;
334 if (nz>1) ++dimension;
335 return std::max(1,dimension);
338 std::pair<double,double> voxelGridBounds(
const int n,
339 const double lowerLimit,
340 const double upperLimit)
const
344 const double spacing= (upperLimit-lowerLimit)/
static_cast<double>(n);
345 return {lowerLimit-0.5*spacing,upperLimit-0.5*spacing};
347 return {lowerLimit,lowerLimit};
Three-dimensional stress field on a grid.
std::vector< Matrix3d > field
A three-dimensional stress field.
std::string name
The prefix of the filename that will be outputted when the stress field is written.
void write()
Write stress and, for Cauchy stress, density fields.
Stress(const Method< TMethod > &method, TGrid *pgrid)
Constructs a Stress object.
std::vector< Vector3d > momentumDensityField
Cauchy-grid momentum density .
Stress(std::string name, const Method< TMethod > &method, TGrid *pgrid)
Constructs a Stress object.
void write_voxel_grid(const int nx, const int ny, const int nz, const Vector3d &lowerLimit, const Vector3d &upperLimit)
Write structured grid fields in LAMMPS dump-grid format.
void write(const std::string &filename)
TGrid * pgrid
Pointer to the Grid on which the stress field is defined.
std::vector< Vector3d > velocityField
Internal Cauchy-grid continuum velocity.
void write_voxel_grid(const std::string &filename, const int nx, const int ny, const int nz, const Vector3d &lowerLimit, const Vector3d &upperLimit)
const Method< TMethod > & method
The method used to compute the stress field. The Method object provides details about the weighting f...
std::vector< double > massDensityField
Cauchy-grid mass density .
#define MY_ERROR(message)
Eigen::Matrix< double, 1, DIM, Eigen::RowMajor > Vector3d