Skip to content

Commit f49e49f

Browse files
author
Han Wang
committed
record ntypes in the graph. graph incompatible with v0.8. lammps support using with pppm (does nothing in the direct space). Fix bugs
1 parent 7faa186 commit f49e49f

File tree

13 files changed

+329
-76
lines changed

13 files changed

+329
-76
lines changed

source/ipi/driver.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ int main(int argc, char * argv[])
114114
double * msg_buff = NULL;
115115
double ener;
116116
double virial[9];
117+
char msg_needinit[] = "NEEDINIT ";
118+
char msg_havedata[] = "HAVEDATA ";
119+
char msg_ready[] = "READY ";
120+
char msg_forceready[] = "FORCEREADY ";
121+
char msg_nothing[] = "nothing";
117122

118123
open_socket_ (&socket, &inet, &port, host);
119124

@@ -126,15 +131,15 @@ int main(int argc, char * argv[])
126131

127132
if (header_str == "STATUS"){
128133
if (! isinit) {
129-
writebuffer_ (&socket, "NEEDINIT ", MSGLEN);
134+
writebuffer_ (&socket, msg_needinit, MSGLEN);
130135
if (b_verb) cout << "# send back " << "NEEDINIT" << endl;
131136
}
132137
else if (hasdata) {
133-
writebuffer_ (&socket, "HAVEDATA ", MSGLEN);
138+
writebuffer_ (&socket, msg_havedata, MSGLEN);
134139
if (b_verb) cout << "# send back " << "HAVEDATA" << endl;
135140
}
136141
else {
137-
writebuffer_ (&socket, "READY ", MSGLEN);
142+
writebuffer_ (&socket, msg_ready, MSGLEN);
138143
if (b_verb) cout << "# send back " << "READY" << endl;
139144
}
140145
}
@@ -190,14 +195,14 @@ int main(int argc, char * argv[])
190195
virial[ii] = dvirial[ii] * icvt_ener * (1.0);
191196
}
192197
if (b_verb) cout << "# energy of sys. : " << scientific << setprecision(10) << dener << endl;
193-
writebuffer_ (&socket, "FORCEREADY ", MSGLEN);
198+
writebuffer_ (&socket, msg_forceready, MSGLEN);
194199
writebuffer_ (&socket, (char *)(&ener), sizeof(double));
195200
writebuffer_ (&socket, (char *)(&natoms), sizeof(int32_t));
196201
writebuffer_ (&socket, (char *)(msg_buff), 3 * natoms * sizeof(double));
197202
writebuffer_ (&socket, (char *)(virial), 9 * sizeof(double));
198203
cbuf = 7;
199204
writebuffer_ (&socket, (char *)(&cbuf), sizeof(int32_t));
200-
writebuffer_ (&socket, "nothing", 7);
205+
writebuffer_ (&socket, msg_nothing, 7);
201206
hasdata = false;
202207
}
203208
else {

source/lib/include/NNPInter.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ class NNPInter
9292
GraphDef graph_def;
9393
bool inited;
9494
VALUETYPE get_rcut () const;
95+
int get_ntypes () const;
9596
VALUETYPE rcut;
9697
VALUETYPE cell_size;
98+
int ntypes;
9799
};
98100

99101
class NNPInterModelDevi
@@ -118,12 +120,28 @@ class NNPInterModelDevi
118120
const vector<VALUETYPE> & box,
119121
const int nghost,
120122
const LammpsNeighborList & lmp_list);
123+
void compute (vector<VALUETYPE> & all_ener,
124+
vector<vector<VALUETYPE> > & all_force,
125+
vector<vector<VALUETYPE> > & all_virial,
126+
vector<vector<VALUETYPE> > & all_atom_energy,
127+
vector<vector<VALUETYPE> > & all_atom_virial,
128+
const vector<VALUETYPE> & coord,
129+
const vector<int> & atype,
130+
const vector<VALUETYPE> & box,
131+
const int nghost,
132+
const LammpsNeighborList & lmp_list);
121133
VALUETYPE cutoff () const {return rcut;};
122-
void compute_avg (VALUETYPE & dener,
134+
void compute_avg (VALUETYPE & dener,
123135
const vector<VALUETYPE > & all_energy);
124-
void compute_avg (vector<VALUETYPE> & avg,
136+
void compute_avg (vector<VALUETYPE> & avg,
125137
const vector<vector<VALUETYPE> > & xx);
126-
void compute_std_f (vector<VALUETYPE> & std,
138+
void compute_std (VALUETYPE & std,
139+
const VALUETYPE & avg,
140+
const vector<VALUETYPE >& xx);
141+
void compute_std_e (vector<VALUETYPE> & std,
142+
const vector<VALUETYPE> & avg,
143+
const vector<vector<VALUETYPE> >& xx);
144+
void compute_std_f (vector<VALUETYPE> & std,
127145
const vector<VALUETYPE> & avg,
128146
const vector<vector<VALUETYPE> >& xx);
129147
private:
@@ -132,8 +150,10 @@ class NNPInterModelDevi
132150
vector<GraphDef> graph_defs;
133151
bool inited;
134152
VALUETYPE get_rcut () const;
153+
int get_ntypes () const;
135154
VALUETYPE rcut;
136155
VALUETYPE cell_size;
156+
int ntypes;
137157
};
138158

139159

source/lib/src/NNPInter.cc

Lines changed: 135 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ checkStatus(const tensorflow::Status& status) {
1111
}
1212
}
1313

14-
static int
14+
static void
1515
convert_nlist_lmp_internal (InternalNeighborList & list,
1616
const LammpsNeighborList & lmp_list)
1717
{
@@ -34,7 +34,7 @@ convert_nlist_lmp_internal (InternalNeighborList & list,
3434
}
3535
}
3636

37-
static int
37+
static void
3838
shuffle_nlist (InternalNeighborList & list,
3939
const NNPAtomMap<VALUETYPE> & map)
4040
{
@@ -55,6 +55,7 @@ shuffle_nlist (InternalNeighborList & list,
5555
static int
5656
make_input_tensors (std::vector<std::pair<string, Tensor>> & input_tensors,
5757
const vector<VALUETYPE> & dcoord_,
58+
const int & ntypes,
5859
const vector<int> & datype_,
5960
const vector<VALUETYPE> & dbox,
6061
const VALUETYPE & cell_size,
@@ -71,7 +72,6 @@ make_input_tensors (std::vector<std::pair<string, Tensor>> & input_tensors,
7172
assert (nall == datype_.size());
7273

7374
vector<int > datype = nnpmap.get_type();
74-
int ntypes = datype.back () + 1;
7575
vector<int > type_count (ntypes, 0);
7676
for (unsigned ii = 0; ii < datype.size(); ++ii){
7777
type_count[datype[ii]] ++;
@@ -182,6 +182,7 @@ make_input_tensors (std::vector<std::pair<string, Tensor>> & input_tensors,
182182
static int
183183
make_input_tensors (std::vector<std::pair<string, Tensor>> & input_tensors,
184184
const vector<VALUETYPE> & dcoord_,
185+
const int & ntypes,
185186
const vector<int> & datype_,
186187
const vector<VALUETYPE> & dbox,
187188
InternalNeighborList & dlist,
@@ -196,8 +197,6 @@ make_input_tensors (std::vector<std::pair<string, Tensor>> & input_tensors,
196197
assert (nall == datype_.size());
197198

198199
vector<int > datype = nnpmap.get_type();
199-
200-
int ntypes = datype.back () + 1;
201200
vector<int > type_count (ntypes, 0);
202201
for (unsigned ii = 0; ii < datype.size(); ++ii){
203202
type_count[datype[ii]] ++;
@@ -394,6 +393,7 @@ NNPInter (const string & model)
394393
checkStatus (session->Create(graph_def));
395394
rcut = get_rcut();
396395
cell_size = rcut;
396+
ntypes = get_ntypes();
397397
inited = true;
398398
}
399399

@@ -407,6 +407,7 @@ init (const string & model)
407407
checkStatus (session->Create(graph_def));
408408
rcut = get_rcut();
409409
cell_size = rcut;
410+
ntypes = get_ntypes();
410411
inited = true;
411412
}
412413

@@ -424,6 +425,20 @@ get_rcut () const
424425
return orc(0);
425426
}
426427

428+
int
429+
NNPInter::
430+
get_ntypes () const
431+
{
432+
std::vector<Tensor> output_tensors;
433+
checkStatus (session->Run(std::vector<std::pair<string, Tensor>> ({}),
434+
{"t_ntypes"},
435+
{},
436+
&output_tensors));
437+
Tensor output_rc = output_tensors[0];
438+
auto orc = output_rc.flat <int> ();
439+
return orc(0);
440+
}
441+
427442
void
428443
NNPInter::
429444
compute (VALUETYPE & dener,
@@ -440,7 +455,7 @@ compute (VALUETYPE & dener,
440455
assert (nloc == nnpmap.get_type().size());
441456

442457
std::vector<std::pair<string, Tensor>> input_tensors;
443-
int ret = make_input_tensors (input_tensors, dcoord_, datype_, dbox, cell_size, nnpmap, nghost);
458+
int ret = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, cell_size, nnpmap, nghost);
444459
assert (ret == nloc);
445460

446461
run_model (dener, dforce_, dvirial, session, input_tensors, nnpmap, nghost);
@@ -467,7 +482,7 @@ compute (VALUETYPE & dener,
467482
shuffle_nlist (nlist, nnpmap);
468483

469484
std::vector<std::pair<string, Tensor>> input_tensors;
470-
int ret = make_input_tensors (input_tensors, dcoord_, datype_, dbox, nlist, nnpmap, nghost);
485+
int ret = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, nlist, nnpmap, nghost);
471486
assert (nloc == ret);
472487

473488
run_model (dener, dforce_, dvirial, session, input_tensors, nnpmap, nghost);
@@ -488,7 +503,7 @@ compute (VALUETYPE & dener,
488503
NNPAtomMap<VALUETYPE> nnpmap (datype_.begin(), datype_.end());
489504

490505
std::vector<std::pair<string, Tensor>> input_tensors;
491-
int nloc = make_input_tensors (input_tensors, dcoord_, datype_, dbox, cell_size, nnpmap);
506+
int nloc = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, cell_size, nnpmap);
492507

493508
run_model (dener, dforce_, dvirial, datom_energy_, datom_virial_, session, input_tensors, nnpmap);
494509
}
@@ -518,7 +533,7 @@ compute (VALUETYPE & dener,
518533
shuffle_nlist (nlist, nnpmap);
519534

520535
std::vector<std::pair<string, Tensor>> input_tensors;
521-
int ret = make_input_tensors (input_tensors, dcoord_, datype_, dbox, nlist, nnpmap, nghost);
536+
int ret = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, nlist, nnpmap, nghost);
522537
assert (nloc == ret);
523538

524539
run_model (dener, dforce_, dvirial, datom_energy_, datom_virial_, session, input_tensors, nnpmap, nghost);
@@ -547,6 +562,7 @@ NNPInterModelDevi (const vector<string> & models)
547562
}
548563
rcut = get_rcut();
549564
cell_size = rcut;
565+
ntypes = get_ntypes();
550566
inited = true;
551567
}
552568

@@ -565,6 +581,7 @@ init (const vector<string> & models)
565581
}
566582
rcut = get_rcut();
567583
cell_size = rcut;
584+
ntypes = get_ntypes();
568585
inited = true;
569586
}
570587

@@ -591,6 +608,28 @@ get_rcut () const
591608
return myrcut;
592609
}
593610

611+
int
612+
NNPInterModelDevi::
613+
get_ntypes () const
614+
{
615+
int myntypes = 0;
616+
for (unsigned ii = 0; ii < numb_models; ++ii){
617+
std::vector<Tensor> output_tensors;
618+
checkStatus (sessions[ii]->Run(std::vector<std::pair<string, Tensor>> ({}),
619+
{"t_ntypes"},
620+
{},
621+
&output_tensors));
622+
Tensor output_rc = output_tensors[0];
623+
auto orc = output_rc.flat <int> ();
624+
if (ii == 0){
625+
myntypes = orc(0);
626+
}
627+
else {
628+
assert (myntypes == orc(0));
629+
}
630+
}
631+
return myntypes;
632+
}
594633

595634
void
596635
NNPInterModelDevi::
@@ -607,7 +646,7 @@ compute (VALUETYPE & dener,
607646
NNPAtomMap<VALUETYPE> nnpmap (datype_.begin(), datype_.end());
608647

609648
std::vector<std::pair<string, Tensor>> input_tensors;
610-
int nloc = make_input_tensors (input_tensors, dcoord_, datype_, dbox, cell_size, nnpmap);
649+
int nloc = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, cell_size, nnpmap);
611650

612651
vector<VALUETYPE > all_energy (numb_models);
613652
vector<vector<VALUETYPE > > all_force (numb_models);
@@ -659,7 +698,7 @@ compute (vector<VALUETYPE> & all_energy,
659698
shuffle_nlist (nlist, nnpmap);
660699

661700
std::vector<std::pair<string, Tensor>> input_tensors;
662-
int ret = make_input_tensors (input_tensors, dcoord_, datype_, dbox, nlist, nnpmap, nghost);
701+
int ret = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, nlist, nnpmap, nghost);
663702
assert (nloc == ret);
664703

665704
all_energy.resize (numb_models);
@@ -671,6 +710,45 @@ compute (vector<VALUETYPE> & all_energy,
671710
}
672711
}
673712

713+
void
714+
NNPInterModelDevi::
715+
compute (vector<VALUETYPE> & all_energy,
716+
vector<vector<VALUETYPE>> & all_force,
717+
vector<vector<VALUETYPE>> & all_virial,
718+
vector<vector<VALUETYPE>> & all_atom_energy,
719+
vector<vector<VALUETYPE>> & all_atom_virial,
720+
const vector<VALUETYPE> & dcoord_,
721+
const vector<int> & datype_,
722+
const vector<VALUETYPE> & dbox,
723+
const int nghost,
724+
const LammpsNeighborList & lmp_list)
725+
{
726+
if (numb_models == 0) return;
727+
728+
int nall = dcoord_.size() / 3;
729+
int nloc = nall - nghost;
730+
NNPAtomMap<VALUETYPE> nnpmap (datype_.begin(), datype_.begin() + nloc);
731+
assert (nloc == nnpmap.get_type().size());
732+
733+
InternalNeighborList nlist;
734+
convert_nlist_lmp_internal (nlist, lmp_list);
735+
shuffle_nlist (nlist, nnpmap);
736+
737+
std::vector<std::pair<string, Tensor>> input_tensors;
738+
int ret = make_input_tensors (input_tensors, dcoord_, ntypes, datype_, dbox, nlist, nnpmap, nghost);
739+
assert (nloc == ret);
740+
741+
all_energy.resize (numb_models);
742+
all_force .resize (numb_models);
743+
all_virial.resize (numb_models);
744+
all_atom_energy.resize (numb_models);
745+
all_atom_virial.resize (numb_models);
746+
747+
for (unsigned ii = 0; ii < numb_models; ++ii){
748+
run_model (all_energy[ii], all_force[ii], all_virial[ii], all_atom_energy[ii], all_atom_virial[ii], sessions[ii], input_tensors, nnpmap, nghost);
749+
}
750+
}
751+
674752

675753
void
676754
NNPInterModelDevi::
@@ -710,6 +788,52 @@ compute_avg (vector<VALUETYPE> & avg,
710788
}
711789

712790

791+
void
792+
NNPInterModelDevi::
793+
compute_std (VALUETYPE & std,
794+
const VALUETYPE & avg,
795+
const vector<VALUETYPE >& xx)
796+
{
797+
std = 0;
798+
assert(xx.size() == numb_models);
799+
for (unsigned jj = 0; jj < xx.size(); ++jj){
800+
std += (xx[jj] - avg) * (xx[jj] - avg);
801+
}
802+
std = sqrt(std / VALUETYPE(numb_models));
803+
// std = sqrt(std / VALUETYPE(numb_models-));
804+
}
805+
806+
void
807+
NNPInterModelDevi::
808+
compute_std_e (vector<VALUETYPE> & std,
809+
const vector<VALUETYPE> & avg,
810+
const vector<vector<VALUETYPE> >&xx)
811+
{
812+
assert (xx.size() == numb_models);
813+
if (numb_models == 0) return;
814+
815+
unsigned ndof = avg.size();
816+
unsigned nloc = ndof;
817+
assert (nloc == ndof);
818+
819+
std.resize(nloc);
820+
fill (std.begin(), std.end(), VALUETYPE(0.));
821+
822+
for (unsigned ii = 0; ii < numb_models; ++ii) {
823+
for (unsigned jj = 0 ; jj < nloc; ++jj){
824+
const VALUETYPE * tmp_f = &(xx[ii][jj]);
825+
const VALUETYPE * tmp_avg = &(avg[jj]);
826+
VALUETYPE vdiff = xx[ii][jj] - avg[jj];
827+
std[jj] += vdiff * vdiff;
828+
}
829+
}
830+
831+
for (unsigned jj = 0; jj < nloc; ++jj){
832+
std[jj] = sqrt(std[jj] / VALUETYPE(numb_models));
833+
// std[jj] = sqrt(std[jj] / VALUETYPE(numb_models-1));
834+
}
835+
}
836+
713837
void
714838
NNPInterModelDevi::
715839
compute_std_f (vector<VALUETYPE> & std,

0 commit comments

Comments
 (0)