@@ -11,7 +11,7 @@ checkStatus(const tensorflow::Status& status) {
1111 }
1212}
1313
14- static int
14+ static void
1515convert_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
3838shuffle_nlist (InternalNeighborList & list,
3939 const NNPAtomMap<VALUETYPE> & map)
4040{
@@ -55,6 +55,7 @@ shuffle_nlist (InternalNeighborList & list,
5555static int
5656make_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,
182182static int
183183make_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+
427442void
428443NNPInter::
429444compute (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
595634void
596635NNPInterModelDevi::
@@ -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
675753void
676754NNPInterModelDevi::
@@ -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+
713837void
714838NNPInterModelDevi::
715839compute_std_f (vector<VALUETYPE> & std,
0 commit comments