Demonstrates the use of Moving Least Squares (Mls) to compute deformation gradients and push stress tensors to a spatial grid.
This test sets up a simulation involving a particle configuration and grid data, computes the Piola-Kirchhoff stress using the LDAD method, and then applies the Moving Least Squares (MLS) method to compute the deformation gradient and push the Piola stress to Cauchy stress on a grid.
Key steps in this example:
- Load atomic configuration and potential.
- Load reference grid data.
- Compute LDAD Piola stress using
MethodLdadConstant
.
- Construct an
Mls
object and compute deformation gradients.
- Push stress field to obtain Cauchy stress tensors.
- Write deformation gradient, pushed grid, and Cauchy stress to output files.
Output files:
mls.DeformationGradient
mlsPushed.grid
mlsPushed.stress
Requirements:
- Input file
config.data
with atomic data in BoxConfiguration format.
- Grid files
reference.grid
.
- A valid KIM model (here: "LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001").
- Expected Cauchy stress field
mlsPushedReference.stress
for unit testing.
- Open the configuration file (*.data in MDStressLab format) and read the number of particles
25 int referenceAndFinal=
true;
26 std::string configFileName=
"config.data";
27 std::ifstream file(configFileName);
28 if(!file)
MY_ERROR(
"ERROR: config.data could not be opened for reading!");
29 int numberOfParticles;
30 file >> numberOfParticles;
31 if (numberOfParticles < 0)
MY_ERROR(
"Error: Negative number of particles.\n");
#define MY_ERROR(message)
- Initialize the BoxConfiguration and read the reference and current atomic coordinates from the configuration file. The reference configuration is an fcc Ar crystal in the relaxed state. The deformed/current configuration is the reference crystal strained in the \(y\)-direction.
36 body.
read(configFileName,referenceAndFinal);
Represents a particle configuration including simulation box information.
void read(std::string configFileName, int referenceAndFinal)
A function to read the properties of atoms from a file in a MDStressLab format.
- Initialize the Kim model
40 std::string modelname=
"LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001";
- Load reference grid coordinates from files.
47 gridFromFile_ref.read(
"reference.grid");
- Initialize LDAD vectors and construct LDAD method MethodLdadConstant. The LDAD vectors (columns) are the three lattice vectors of the conventional unit cell.
53 ldadVectors << 5.29216036151419, 0.0, 0.0,
54 0.0, 5.29216036151419, 0.0,
55 0.0, 0.0, 5.29216036151419;
Eigen::Matrix< double, DIM, DIM, Eigen::RowMajor > Matrix3d
- Construct the Piola stress using the above method
- Construct an Mls object to compute deformation gradients and push the stress to Cauchy form.
71 double MlsRadius = 15.87648108454257;
72 Mls testMls(body,&gridFromFile_ref,MlsRadius,
"mls");
Computes deformation gradients and stress tensors using Moving Least Squares (MLS) interpolation on a...
- Write deformation gradients, pushed grid points, and Cauchy stresses to output files.
77 std::vector<Matrix3d> cauchyPushedField;
78 testMls.pushToCauchy(ldad_constant_stress_ref.field,cauchyPushedField);
79 testMls.writeDeformationGradient();
80 testMls.writeGridPushed();
81 testMls.writePushedCauchyStress(cauchyPushedField);
- Compare the pushed fields to expected data.
85 compareStress(
"mlsPushed");
Full code:
18#include "../compareStress.cpp"
25 int referenceAndFinal=
true;
26 std::string configFileName=
"config.data";
27 std::ifstream file(configFileName);
28 if(!file)
MY_ERROR(
"ERROR: config.data could not be opened for reading!");
29 int numberOfParticles;
30 file >> numberOfParticles;
31 if (numberOfParticles < 0)
MY_ERROR(
"Error: Negative number of particles.\n");
36 body.
read(configFileName,referenceAndFinal);
40 std::string modelname=
"LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001";
47 gridFromFile_ref.
read(
"reference.grid");
53 ldadVectors << 5.29216036151419, 0.0, 0.0,
54 0.0, 5.29216036151419, 0.0,
55 0.0, 0.0, 5.29216036151419;
71 double MlsRadius = 15.87648108454257;
72 Mls testMls(body,&gridFromFile_ref,MlsRadius,
"mls");
77 std::vector<Matrix3d> cauchyPushedField;
85 compareStress(
"mlsPushed");
int calculateStress(const BoxConfiguration &body, Kim &kim, std::tuple<> stress, const bool &projectForces=false)
void writeDeformationGradient()
Writes the deformation gradient at each grid point to a file.
void pushToCauchy(const std::vector< Matrix3d > &piolaStress, std::vector< Matrix3d > &cauchyStress)
Converts Piola-Kirchhoff stress to Cauchy stress at grid points.
void writePushedCauchyStress(std::vector< Matrix3d > &cauchyStress)
Writes Cauchy stresses at grid points to a file.
void writeGridPushed()
Writes the pushed (deformed) grid point coordinates to a file.
std::vector< Matrix3d > field
A three-dimensional stress field.