@@ -75,36 +75,39 @@ void MD_func::kinetic_stress(
7575// This function calculates the classical kinetic energy of atoms
7676// and its contribution to stress.
7777// ----------------------------------------------------------------------------
78- kinetic = MD_func::GetAtomKE (unit_in.nat , vel, allmass);
78+ kinetic = MD_func::GetAtomKE (unit_in.nat , vel, allmass);
7979
80- ModuleBase::matrix temp;
81- temp.create (3 ,3 ); // initialize
80+ if (GlobalV::CAL_STRESS)
81+ {
82+ ModuleBase::matrix temp;
83+ temp.create (3 ,3 ); // initialize
8284
83- for (int ion=0 ; ion<unit_in.nat ; ++ion)
84- {
85- for (int i=0 ; i<3 ; ++i)
86- {
87- for (int j=i; j<3 ; ++j)
88- {
89- temp (i, j) += allmass[ion] * vel[ion][i] * vel[ion][j];
90- }
91- }
92- }
85+ for (int ion=0 ; ion<unit_in.nat ; ++ion)
86+ {
87+ for (int i=0 ; i<3 ; ++i)
88+ {
89+ for (int j=i; j<3 ; ++j)
90+ {
91+ temp (i, j) += allmass[ion] * vel[ion][i] * vel[ion][j];
92+ }
93+ }
94+ }
9395
94- for (int i=0 ; i<3 ; ++i)
95- {
96- for (int j=0 ; j<3 ; ++j)
97- {
98- if (j<i)
99- {
100- stress (i, j) = stress (j, i);
101- }
102- else
103- {
104- stress (i, j) = temp (i, j)/unit_in.omega ;
105- }
106- }
107- }
96+ for (int i=0 ; i<3 ; ++i)
97+ {
98+ for (int j=0 ; j<3 ; ++j)
99+ {
100+ if (j<i)
101+ {
102+ stress (i, j) = stress (j, i);
103+ }
104+ else
105+ {
106+ stress (i, j) = temp (i, j)/unit_in.omega ;
107+ }
108+ }
109+ }
110+ }
108111}
109112
110113// Read Velocity from STRU liuyu 2021-09-24
@@ -239,6 +242,7 @@ void MD_func::force_virial(
239242
240243 if (mdp.md_ensolver == " LJ" )
241244 {
245+ GlobalV::CAL_STRESS = 1 ;
242246 bool which_method = unit_in.judge_big_cell ();
243247 if (which_method)
244248 {
@@ -272,6 +276,7 @@ void MD_func::force_virial(
272276 }
273277 else if (mdp.md_ensolver == " DP" )
274278 {
279+ GlobalV::CAL_STRESS = 1 ;
275280 DP_potential::DP_pot (unit_in, potential, force, stress);
276281 }
277282#ifndef __CMD
@@ -358,12 +363,15 @@ void MD_func::MDdump(const int &step,
358363 ofs << " " << unit_in.latvec .e21 << " " << unit_in.latvec .e22 << " " << unit_in.latvec .e23 << std::endl;
359364 ofs << " " << unit_in.latvec .e31 << " " << unit_in.latvec .e32 << " " << unit_in.latvec .e33 << std::endl;
360365
361- ofs << " VIRIAL (KBAR)" << std::endl;
362- for (int i=0 ; i<3 ; ++i)
366+ if (GlobalV::CAL_STRESS)
363367 {
364- ofs << " " << virial (i, 0 ) * unit_virial
365- << " " << virial (i, 1 ) * unit_virial
366- << " " << virial (i, 2 ) * unit_virial << std::endl;
368+ ofs << " VIRIAL (KBAR)" << std::endl;
369+ for (int i=0 ; i<3 ; ++i)
370+ {
371+ ofs << " " << virial (i, 0 ) * unit_virial
372+ << " " << virial (i, 1 ) * unit_virial
373+ << " " << virial (i, 2 ) * unit_virial << std::endl;
374+ }
367375 }
368376
369377 ofs << " INDEX LABEL POSITIONS FORCE (eV/Angstrom)" << std::endl;
0 commit comments