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

An example demonstrating the computation of Piola and Cauchy stress tensors using hardy and virial spherical averaging domains, and 1D and 2D grids

  1. Open the configuration file (*.data in MDStressLab format) and read the number of particles
    21 int numberOfParticles;
    22 int referenceAndFinal= true; // Does the input file include reference and final configurations
    23 std::string configFileName= "config.data";
    24 std::ifstream file(configFileName);
    25 if(!file) MY_ERROR("ERROR: config.data could not be opened for reading!\n");
    26 file >> numberOfParticles;
    27 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.
    31 BoxConfiguration body{numberOfParticles,referenceAndFinal};
    32 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
    36 std::string modelname= "LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001";
    37 Kim kim(modelname);
    Definition kim.h:21
  4. Create 1D and 2D grids
    41 int ngrid=200;
    42 Grid<Current> current2DGrid(Vector3d(0,0,0),Vector3d(60,60,60),ngrid,ngrid);
    43 Grid<Reference> reference2DGrid(Vector3d(0,0,0),Vector3d(60,60,60),ngrid,ngrid);
    44
    45 ngrid= 500;
    46 Grid<Reference> reference1DGrid(Vector3d(0,0,0),Vector3d(60,60,60),ngrid);
    Definition Grid.h:31
    Eigen::Matrix< double, 1, DIM, Eigen::RowMajor > Vector3d
    Definition typedef.h:60
  5. Construct hardy and virial MethodSphere objects
    50 MethodSphere hardy5(5.0,"hardy");
    51 MethodSphere virial5(5.0,"virial");
    Implements radially symmetric weighting functions (Hardy, Virial) and its associated bond function fo...
  6. Construct Stress objects using the MethodSphere and Grid objects
    55 Stress<MethodSphere,Cauchy> hardyCauchy("hardyCauchy",hardy5,&current2DGrid);
    56 Stress<MethodSphere,Cauchy> virialCauchy("virialCauchy",virial5,&current2DGrid);
    57
    58 Stress<MethodSphere,Piola> hardyPiola("hardyPiola",hardy5,&reference1DGrid);
    59 Stress<MethodSphere,Piola> virialPiola("virialPiola",virial5,&reference1DGrid);
  7. Calculate stresses. The following snippet shows that stresses can be calculated all at once or with various combinations.
    64 calculateStress(body,kim,
    65 std::tie(),
    66 std::tie());
    67
    68 calculateStress(body,kim,
    69 std::tie(hardyCauchy));
    70
    71 calculateStress(body,kim,
    72 std::tie(virialPiola));
    73
    74 calculateStress(body,kim,
    75 std::tie(hardyPiola,virialPiola),
    76 std::tie(hardyCauchy,virialCauchy));
    77
    int calculateStress(const BoxConfiguration &body, Kim &kim, std::tuple<> stress, const bool &projectForces=false)
  8. Write stresses
    81 hardyCauchy.write();
    82 hardyPiola.write();
    83 virialCauchy.write();
    84 virialPiola.write();
  9. We compare our results with the exact results for unit testing purposes.
    88 compareStress("hardyCauchy");
    89 compareStress("hardyPiola");
    90 compareStress("virialCauchy");
    91 compareStress("virialPiola");

Visual comparison

Full code:

1/*
2 * main.cpp
3 *
4 * Created on: Nov 3, 2019
5 * Author: Nikhil Admal
6 */
7#include "MethodSphere.h"
8#include <string>
9#include <iostream>
10#include <tuple>
11#include <fstream>
12#include "BoxConfiguration.h"
13#include "calculateStress.h"
14#include "Grid.h"
15#include "typedef.h"
16#include "../compareStress.cpp"
17
18int main()
19{
21 int numberOfParticles;
22 int referenceAndFinal= true; // Does the input file include reference and final configurations
23 std::string configFileName= "config.data";
24 std::ifstream file(configFileName);
25 if(!file) MY_ERROR("ERROR: config.data could not be opened for reading!\n");
26 file >> numberOfParticles;
27 if (numberOfParticles < 0) MY_ERROR("Error: Negative number of particles.\n");
31 BoxConfiguration body{numberOfParticles,referenceAndFinal};
32 body.read(configFileName,referenceAndFinal);
36 std::string modelname= "LJ_Smoothed_Bernardes_1958_Ar__MO_764178710049_001";
37 Kim kim(modelname);
41 int ngrid=200;
42 Grid<Current> current2DGrid(Vector3d(0,0,0),Vector3d(60,60,60),ngrid,ngrid);
43 Grid<Reference> reference2DGrid(Vector3d(0,0,0),Vector3d(60,60,60),ngrid,ngrid);
44
45 ngrid= 500;
46 Grid<Reference> reference1DGrid(Vector3d(0,0,0),Vector3d(60,60,60),ngrid);
50 MethodSphere hardy5(5.0,"hardy");
51 MethodSphere virial5(5.0,"virial");
55 Stress<MethodSphere,Cauchy> hardyCauchy("hardyCauchy",hardy5,&current2DGrid);
56 Stress<MethodSphere,Cauchy> virialCauchy("virialCauchy",virial5,&current2DGrid);
57
58 Stress<MethodSphere,Piola> hardyPiola("hardyPiola",hardy5,&reference1DGrid);
59 Stress<MethodSphere,Piola> virialPiola("virialPiola",virial5,&reference1DGrid);
64 calculateStress(body,kim,
65 std::tie(),
66 std::tie());
67
68 calculateStress(body,kim,
69 std::tie(hardyCauchy));
70
71 calculateStress(body,kim,
72 std::tie(virialPiola));
73
74 calculateStress(body,kim,
75 std::tie(hardyPiola,virialPiola),
76 std::tie(hardyCauchy,virialCauchy));
77
81 hardyCauchy.write();
82 hardyPiola.write();
83 virialCauchy.write();
84 virialPiola.write();
88 compareStress("hardyCauchy");
89 compareStress("hardyPiola");
90 compareStress("virialCauchy");
91 compareStress("virialPiola");
95 return 0;
96}
97
98
void write()
Definition Stress.h:90
int main()