@@ -17,13 +17,13 @@ ML_EXX::ML_EXX()
1717
1818ML_EXX ::~ML_EXX (){}
1919
20- void ML_EXX::set_para (const Input_para& inp, const UnitCell* ucell_in, const ModulePW::PW_Basis* rho_basis_in)
20+ void ML_EXX::set_para (const Input_para& inp, const UnitCell* ucell_in, const ModulePW::PW_Basis* rho_basis_in, std::ostream& ofs_running )
2121{
2222 torch::set_default_dtype (caffe2::TypeMeta::fromScalarType (torch::kDouble ));
2323 auto output = torch::get_default_dtype ();
24- std::cout << " Default type: " << output << std::endl;
24+ ofs_running << " Default type: " << output << std::endl;
2525
26- this ->set_device (inp.of_ml_device );
26+ this ->set_device (inp.of_ml_device , ofs_running );
2727
2828 this ->nx = rho_basis_in->nrxx ;
2929 this ->nx_tot = rho_basis_in->nrxx ;
@@ -48,15 +48,23 @@ void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const Mod
4848 inp.of_ml_tanhp_nl ,
4949 inp.of_ml_tanhq_nl );
5050
51- std::cout << " ninput = " << this ->ninput << std::endl;
51+ ofs_running << " ninput = " << this ->ninput << std::endl;
5252
5353 if (PARAM .inp .ml_exx )
5454 {
5555 int nnode = 100 ;
5656 int nlayer = 3 ;
57- this ->nn = std::make_shared<NN_OFImpl>(this ->nx , 0 , this ->ninput , nnode, nlayer, this ->device );
58- torch::load (this ->nn , " net.pt" , this ->device_type );
59- std::cout << " load net done" << std::endl;
57+ this ->nn = std::make_shared<NN_OFImpl>(this ->nx , 0 , this ->ninput , nnode, nlayer, this ->device , ofs_running);
58+ try
59+ {
60+ torch::load (this ->nn , " net.pt" , this ->device_type );
61+ }
62+ catch (const std::exception& e)
63+ {
64+ ModuleBase::WARNING_QUIT (" ML_EXX::set_para" ,
65+ " Failed to load neural network model from net.pt: " + std::string (e.what ()));
66+ }
67+ ofs_running << " load net done (ML EXX neural network functional model loaded successfully)" << std::endl;
6068 if (PARAM .inp .of_ml_feg != 0 )
6169 {
6270 torch::Tensor feg_inpt = torch::zeros (this ->ninput , this ->device_type );
@@ -74,7 +82,7 @@ void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const Mod
7482 this ->feg_net_F = this ->nn ->forward (feg_inpt).to (this ->device_CPU ).contiguous ().data_ptr <double >()[0 ];
7583 }
7684
77- std::cout << " feg_net_F = " << this ->feg_net_F << std::endl;
85+ ofs_running << " feg_net_F = " << this ->feg_net_F << std::endl;
7886 }
7987 }
8088
@@ -88,8 +96,24 @@ void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const Mod
8896 this ->chi_pnl = inp.of_ml_chi_pnl ;
8997 this ->chi_qnl = inp.of_ml_chi_qnl ;
9098
91- this ->cal_tool ->set_para (this ->nx , inp.nelec , inp.of_tf_weight , inp.of_vw_weight , this ->chi_p , this ->chi_q ,
92- this ->chi_xi , this ->chi_pnl , this ->chi_qnl , this ->nkernel , inp.of_ml_kernel , inp.of_ml_kernel_scaling , inp.of_ml_yukawa_alpha , inp.of_ml_kernel_file , this ->dV * rho_basis_in->nxyz , rho_basis_in);
99+ this ->cal_tool ->set_para (
100+ this ->nx ,
101+ inp.nelec ,
102+ inp.of_tf_weight ,
103+ inp.of_vw_weight ,
104+ this ->chi_p ,
105+ this ->chi_q ,
106+ this ->chi_xi ,
107+ this ->chi_pnl ,
108+ this ->chi_qnl ,
109+ this ->nkernel ,
110+ inp.of_ml_kernel ,
111+ inp.of_ml_kernel_scaling ,
112+ inp.of_ml_yukawa_alpha ,
113+ inp.of_ml_kernel_file ,
114+ this ->dV * rho_basis_in->nxyz ,
115+ rho_basis_in,
116+ ofs_running);
93117 }
94118}
95119
@@ -112,9 +136,9 @@ void ML_EXX::ml_potential(const double * const * prho, const ModulePW::PW_Basis
112136 rho_data[ir] = std::abs (prho[0 ][ir]);
113137 }
114138
115- this ->updateInput (prho_mod, pw_rho);
139+ this ->update_input (prho_mod, pw_rho);
116140
117- this ->NN_forward (prho_mod, pw_rho, true );
141+ this ->nn_forward (prho_mod, pw_rho, true );
118142
119143 torch::Tensor enhancement_cpu_tensor = this ->nn ->F .to (this ->device_CPU ).contiguous ();
120144 this ->enhancement_cpu_ptr = enhancement_cpu_tensor.data_ptr <double >();
@@ -148,13 +172,13 @@ void ML_EXX::ml_potential(const double * const * prho, const ModulePW::PW_Basis
148172 * @param pw_rho PW_Basis
149173 * @param veff effective potential
150174 */
151- void ML_EXX::generateTrainData (const double * const *prho, const ModulePW::PW_Basis *pw_rho, const double *veff)
175+ void ML_EXX::gen_training_data (const double * const *prho, const ModulePW::PW_Basis *pw_rho, const double *veff)
152176{
153177 if (PARAM .inp .of_kinetic == " ml" )
154178 {
155- this ->updateInput (prho, pw_rho);
179+ this ->update_input (prho, pw_rho);
156180
157- this ->NN_forward (prho, pw_rho, true );
181+ this ->nn_forward (prho, pw_rho, true );
158182
159183 torch::Tensor enhancement_cpu_tensor = this ->nn ->F .to (this ->device_CPU ).contiguous ();
160184 this ->enhancement_cpu_ptr = enhancement_cpu_tensor.data_ptr <double >();
@@ -166,8 +190,8 @@ void ML_EXX::generateTrainData(const double * const *prho, const ModulePW::PW_Ba
166190
167191 this ->get_potential_ (prho, pw_rho, potential);
168192
169- this ->dumpTensor (" enhancement.npy" , enhancement);
170- this ->dumpMatrix (" potential.npy" , potential);
193+ this ->dump_tensor (" enhancement.npy" , enhancement);
194+ this ->dump_matrix (" potential.npy" , potential);
171195 }
172196}
173197
@@ -177,28 +201,28 @@ void ML_EXX::generateTrainData(const double * const *prho, const ModulePW::PW_Ba
177201 * @param prho charge density
178202 * @param pw_rho PW_Basis
179203 */
180- void ML_EXX::localTest (const double * const *pprho, const ModulePW::PW_Basis *pw_rho)
204+ void ML_EXX::localTest (const double * const *pprho, const ModulePW::PW_Basis *pw_rho, std::ostream& ofs_running )
181205{
182206 // for test =====================
183207 std::vector<long unsigned int > cshape = {(long unsigned ) this ->nx };
184208 bool fortran_order = false ;
185209
186210 std::vector<double > temp_prho (this ->nx );
187- this ->loadVector (" path_to_rho_file" , temp_prho);
211+ this ->load_vector (" path_to_rho_file" , temp_prho);
188212
189213 double ** prho = new double *[1 ];
190214 prho[0 ] = new double [this ->nx ];
191215 for (int ir = 0 ; ir < this ->nx ; ++ir) prho[0 ][ir] = temp_prho[ir];
192216 for (int ir = 0 ; ir < this ->nx ; ++ir)
193217 {
194218 if (prho[0 ][ir] == 0 .){
195- std::cout << " WARNING: rho = 0" << std::endl;
219+ ofs_running << " WARNING: rho = 0" << std::endl;
196220 }
197221 };
198222 // ==============================
199- this ->updateInput (prho, pw_rho);
223+ this ->update_input (prho, pw_rho);
200224
201- this ->NN_forward (prho, pw_rho, true );
225+ this ->nn_forward (prho, pw_rho, true );
202226
203227 torch::Tensor enhancement_cpu_tensor = this ->nn ->F .to (this ->device_CPU ).contiguous ();
204228 this ->enhancement_cpu_ptr = enhancement_cpu_tensor.data_ptr <double >();
@@ -210,8 +234,8 @@ void ML_EXX::localTest(const double * const *pprho, const ModulePW::PW_Basis *pw
210234
211235 this ->get_potential_ (prho, pw_rho, potential);
212236
213- this ->dumpTensor (" enhancement-abacus.npy" , enhancement);
214- this ->dumpMatrix (" potential-abacus.npy" , potential);
237+ this ->dump_tensor (" enhancement-abacus.npy" , enhancement);
238+ this ->dump_matrix (" potential-abacus.npy" , potential);
215239 exit (0 );
216240}
217241
0 commit comments