Compares Hardy stress computation using projected forces vs. process_dEdr with multiple OpenKIM models. The system is a single crystal Si with a crack under mode 1 loading
- Projected forces: Interatomic forces derived from forces.
- process_dEdr: Interatomic forces derived from analytical derivatives of energy w.r.t distances, provided by KIM models (if available).
For each model:
- We attempt to compute stress using projected forces first.
- Subsequently, we compute stress using process_dEdr if the model supports it.
- Both approaches use the
MethodSphere
class to compute Hardy stress on a spatial grid.
- Query KIM models: For each model in the list, extract its lattice constant and elastic constants. In this demo, the list contains only one model.
23 std::vector<std::string> modelnames={
24 "Tersoff_LAMMPS_Tersoff_1988T3_Si__MO_186459956893_004"
27 std::cout <<
"Querying models." << std::endl;
28 for (
int i = 0; i < modelnames.size(); ++i) {
29 std::cout <<
"Model: " << modelnames[i] << std::endl;
30 std::regex rgx(
"MO_\\d+_\\d{3}");
33 if (!std::regex_search(modelnames[i], match, rgx))
34 std::cout <<
"Regex error!" << std::endl;
38 "get_lattice_constant_cubic",
40 "model=[\"" + match.str()+
"\"]",
41 "crystal=[\"diamond\"]",
43 "units=[\"angstrom\"]"
46 std::cout <<
"Lattice constant = " << latticeConstant << std::endl;
48 "get_elastic_constants_isothermal_cubic",
50 "model=[\"" + match.str()+
"\"]",
51 "crystal=[\"diamond\"]",
53 "units=[\"eV/angstrom^3\"]"
56 std::cout <<
"Elastic constants = " << elasticConstants << std::endl;
57 std::cout <<
"------------------------------------------ " << std::endl;
std::string kim_query(const std::string &query_function, const std::initializer_list< std::string > &extra_args={ "species=[]", })
- Load atomic configuration: Read a MDStressLab-style configuration (
config.data
). 62 int numberOfParticles;
63 int referenceAndFinal=
true;
64 std::string configFileName=
"config.data";
65 std::ifstream file(configFileName);
66 if(!file)
MY_ERROR(
"ERROR: config.dat could not be opened for reading!");
68 file >> numberOfParticles;
69 if (numberOfParticles < 0)
MY_ERROR(
"Error: Negative number of particles.\n");
72 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.
#define MY_ERROR(message)
- Load grid: Read grid points for stress evaluation from
grid_cauchy.data
.
79 gridFromFile.read(
"grid_cauchy.data");
- Compute and compare Hardy stress:
- Start with stress computation using projected forces (always possible).
- Try
process_dEdr
method, if available.
- Write output stress fields for both methods (if available).
84 for (
const auto modelname : modelnames)
92 std::tie(hardyStress),
true);
93 hardyStress.write(
"project_hardy_" + modelname);
95 catch(
const std::runtime_error& e){
96 std::cout << e.what() << std::endl;
97 std::cout <<
"Compute stress with projected forces failed. Moving on" << std::endl;
106 std::tie(hardyStress));
107 hardyStress.write(
"hardy_" + modelname);
109 catch(
const std::runtime_error& e){
110 std::cout << e.what() << std::endl;
111 std::cout <<
"Compute stress with process_dedr failed. Moving on" << std::endl;
int calculateStress(const BoxConfiguration &body, Kim &kim, std::tuple<> stress, const bool &projectForces=false)
Implements radially symmetric weighting functions (Hardy, Virial) and its associated bond function fo...
Output:
For each KIM model, this example will generate:
project_hardy_<modelname>
: Stress computed using projected forces.
hardy_<modelname>
: Stress computed using process_dEdr.
Visual comparison
- See Visualization Utilities for contour plotting

Projected force–based stress | 
process_dEdr–based stress |

Projected force–based stress | 
process_dEdr–based stress |

Projected force–based stress | 
process_dEdr–based stress |
Full code:
23 std::vector<std::string> modelnames={
24 "Tersoff_LAMMPS_Tersoff_1988T3_Si__MO_186459956893_004"
27 std::cout <<
"Querying models." << std::endl;
28 for (
int i = 0; i < modelnames.size(); ++i) {
29 std::cout <<
"Model: " << modelnames[i] << std::endl;
30 std::regex rgx(
"MO_\\d+_\\d{3}");
33 if (!std::regex_search(modelnames[i], match, rgx))
34 std::cout <<
"Regex error!" << std::endl;
38 "get_lattice_constant_cubic",
40 "model=[\"" + match.str()+
"\"]",
41 "crystal=[\"diamond\"]",
43 "units=[\"angstrom\"]"
46 std::cout <<
"Lattice constant = " << latticeConstant << std::endl;
48 "get_elastic_constants_isothermal_cubic",
50 "model=[\"" + match.str()+
"\"]",
51 "crystal=[\"diamond\"]",
53 "units=[\"eV/angstrom^3\"]"
56 std::cout <<
"Elastic constants = " << elasticConstants << std::endl;
57 std::cout <<
"------------------------------------------ " << std::endl;
62 int numberOfParticles;
63 int referenceAndFinal=
true;
64 std::string configFileName=
"config.data";
65 std::ifstream file(configFileName);
66 if(!file)
MY_ERROR(
"ERROR: config.dat could not be opened for reading!");
68 file >> numberOfParticles;
69 if (numberOfParticles < 0)
MY_ERROR(
"Error: Negative number of particles.\n");
72 body.
read(configFileName,referenceAndFinal);
79 gridFromFile.
read(
"grid_cauchy.data");
84 for (
const auto modelname : modelnames)
92 std::tie(hardyStress),
true);
93 hardyStress.
write(
"project_hardy_" + modelname);
95 catch(
const std::runtime_error& e){
96 std::cout << e.what() << std::endl;
97 std::cout <<
"Compute stress with projected forces failed. Moving on" << std::endl;
106 std::tie(hardyStress));
107 hardyStress.
write(
"hardy_" + modelname);
109 catch(
const std::runtime_error& e){
110 std::cout << e.what() << std::endl;
111 std::cout <<
"Compute stress with process_dedr failed. Moving on" << std::endl;