MDStressLab++
Loading...
Searching...
No Matches
testMls.cpp

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:

Output files:

Requirements:

  1. 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)
    Definition typedef.h:17
  2. 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.
    35 BoxConfiguration body{numberOfParticles,referenceAndFinal};
    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.
  3. Initialize the Kim model
    40 std::string modelname= "LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001";
    41 Kim kim(modelname);
    Definition kim.h:21
  4. Load reference grid coordinates from files.
    45 int ngrid = 125;
    46 Grid<Reference> gridFromFile_ref(ngrid);
    47 gridFromFile_ref.read("reference.grid");
    Definition Grid.h:31
  5. Initialize LDAD vectors and construct LDAD method MethodLdadConstant. The LDAD vectors (columns) are the three lattice vectors of the conventional unit cell.
    52 Matrix3d ldadVectors;
    53 ldadVectors << 5.29216036151419, 0.0, 0.0,
    54 0.0, 5.29216036151419, 0.0,
    55 0.0, 0.0, 5.29216036151419;
    56 MethodLdadConstant ldad(ldadVectors);
    Eigen::Matrix< double, DIM, DIM, Eigen::RowMajor > Matrix3d
    Definition typedef.h:56
  6. Construct the Piola stress using the above method
    60 //TODO The bond function should be accepted as a reference
    61 Stress<MethodLdadConstant,Piola> ldad_constant_stress_ref("ldad",ldad,&gridFromFile_ref);
  7. 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...
    Definition Mls.h:26
  8. 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);
  9. Compare the pushed fields to expected data.
    85 compareStress("mlsPushed");

Full code:

1/*
2 * main.cpp
3 *
4 * Created on: Nov 3, 2019
5 * Author: Nikhil Admal
6 */
7
8#include <string>
9#include <iostream>
10#include <tuple>
11#include <fstream>
12#include "MethodLdad.h"
13#include "BoxConfiguration.h"
14#include "calculateStress.h"
15#include "Mls.h"
16#include "Grid.h"
17#include "typedef.h"
18#include "../compareStress.cpp"
19
20
21
22int main()
23{
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");
35 BoxConfiguration body{numberOfParticles,referenceAndFinal};
36 body.read(configFileName,referenceAndFinal);
40 std::string modelname= "LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001";
41 Kim kim(modelname);
45 int ngrid = 125;
46 Grid<Reference> gridFromFile_ref(ngrid);
47 gridFromFile_ref.read("reference.grid");
52 Matrix3d ldadVectors;
53 ldadVectors << 5.29216036151419, 0.0, 0.0,
54 0.0, 5.29216036151419, 0.0,
55 0.0, 0.0, 5.29216036151419;
56 MethodLdadConstant ldad(ldadVectors);
60 //TODO The bond function should be accepted as a reference
61 Stress<MethodLdadConstant,Piola> ldad_constant_stress_ref("ldad",ldad,&gridFromFile_ref);
66 calculateStress(body,kim,std::tie(ldad_constant_stress_ref));
71 double MlsRadius = 15.87648108454257;
72 Mls testMls(body,&gridFromFile_ref,MlsRadius,"mls");
77 std::vector<Matrix3d> cauchyPushedField;
78 testMls.pushToCauchy(ldad_constant_stress_ref.field,cauchyPushedField);
80 testMls.writeGridPushed();
81 testMls.writePushedCauchyStress(cauchyPushedField);
85 compareStress("mlsPushed");
89 return 0;
90}
91
92
int calculateStress(const BoxConfiguration &body, Kim &kim, std::tuple<> stress, const bool &projectForces=false)
void read(std::string)
Definition Grid.cpp:133
void writeDeformationGradient()
Writes the deformation gradient at each grid point to a file.
Definition Mls.cpp:832
void pushToCauchy(const std::vector< Matrix3d > &piolaStress, std::vector< Matrix3d > &cauchyStress)
Converts Piola-Kirchhoff stress to Cauchy stress at grid points.
Definition Mls.cpp:796
void writePushedCauchyStress(std::vector< Matrix3d > &cauchyStress)
Writes Cauchy stresses at grid points to a file.
Definition Mls.cpp:887
void writeGridPushed()
Writes the pushed (deformed) grid point coordinates to a file.
Definition Mls.cpp:872
std::vector< Matrix3d > field
A three-dimensional stress field.
Definition Stress.h:39
int main()