2929#include < vector>
3030#include " module_base/timer.h"
3131#include " module_base/tool_threading.h"
32+ #include " module_base/libm/libm.h"
3233#include " module_io/rho_io.h"
3334
3435Charge::Charge ()
@@ -368,13 +369,13 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
368369 }();
369370
370371 assert (GlobalC::ucell.meshx >0 );
371- std::vector<double > rho1d (GlobalC::ucell.meshx );
372372 // ----------------------------------------------------------
373373 // Here we compute the G=0 term
374374 // ----------------------------------------------------------
375375 int gstart = 0 ;
376376 if (rho_basis->gg_uniq [0 ] < 1e-8 )
377377 {
378+ std::vector<double > rho1d (GlobalC::ucell.meshx );
378379 for (int ir = 0 ;ir < mesh;ir++)
379380 {
380381 // rho1d [ir] = atom->rho_at[ir];
@@ -390,6 +391,15 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
390391 // G=0 term only belong to 1 cpu.
391392 // Other processors start from '0'
392393 // ----------------------------------------------------------
394+ #ifdef _OPENMP
395+ #pragma omp parallel
396+ {
397+ #endif
398+ std::vector<double > rho1d (GlobalC::ucell.meshx );
399+
400+ #ifdef _OPENMP
401+ #pragma omp for
402+ #endif
393403 for (int igg = gstart; igg < rho_basis->ngg ;++igg)
394404 {
395405 const double gx = sqrt (rho_basis->gg_uniq [igg]) * GlobalC::ucell.tpiba ;
@@ -403,27 +413,37 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
403413 else
404414 {
405415 const double gxx = gx * atom->ncpp .r [ir];
406- rho1d[ir] = rhoatm[ir] * sin (gxx) / gxx;
407- rho1d[ir] = rhoatm[ir] * sin (gxx) / gxx;
416+ rho1d[ir] = rhoatm[ir] * ModuleBase::libm::sin (gxx) / gxx;
408417 }
409418 }
410419 ModuleBase::Integral::Simpson_Integral (mesh, rho1d.data (), atom->ncpp .rab , rho_lgl[igg]);
411420 }
412-
413- if (GlobalV::test_charge>0 ) std::cout<<" |G|>0 term done." <<std::endl;
421+ #ifdef _OPENMP
422+ #pragma omp single
423+ #endif
424+ { if (GlobalV::test_charge>0 ) std::cout<<" |G|>0 term done." <<std::endl; }
414425 // ----------------------------------------------------------
415426 // EXPLAIN : Complete the transfer of rho from real space to
416427 // reciprocal space
417428 // ----------------------------------------------------------
429+ #ifdef _OPENMP
430+ #pragma omp for
431+ #endif
418432 for (int igg=0 ; igg< rho_basis->ngg ; igg++)
419433 rho_lgl[igg] /= GlobalC::ucell.omega ;
434+ #ifdef _OPENMP
435+ }
436+ #endif
420437 return rho_lgl;
421438 }();
422439 // ----------------------------------------------------------
423440 // EXPLAIN : compute the 3D atomic charge in reciprocal space
424441 // ----------------------------------------------------------
425442 if (spin_number_need==1 )
426443 {
444+ #ifdef _OPENMP
445+ #pragma omp parallel for
446+ #endif
427447 for (int ig=0 ; ig< rho_basis->npw ;ig++)
428448 {
429449 rho_g3d (0 , ig) += GlobalC::sf.strucFac (it, ig) * rho_lgl[ rho_basis->ig2igg [ig] ];
@@ -434,6 +454,9 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
434454 {
435455 if (startmag_type==1 )
436456 {
457+ #ifdef _OPENMP
458+ #pragma omp parallel for
459+ #endif
437460 for (int ig = 0 ; ig < rho_basis->npw ; ig++)
438461 {
439462 const std::complex <double > swap = GlobalC::sf.strucFac (it, ig)* rho_lgl[rho_basis->ig2igg [ig]];
@@ -446,7 +469,6 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
446469 // mohan add 2011-06-14
447470 else if (startmag_type==2 )
448471 {
449- std::complex <double > swap = ModuleBase::ZERO;
450472 std::complex <double > ci_tpi = ModuleBase::NEG_IMAG_UNIT * ModuleBase::TWO_PI;
451473 for (int ia = 0 ; ia < atom->na ; ia++)
452474 {
@@ -455,15 +477,17 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
455477 const double up = 0.5 * ( 1 + atom->mag [ia] / atom->ncpp .zv );
456478 const double dw = 0.5 * ( 1 - atom->mag [ia] / atom->ncpp .zv );
457479 // std::cout << " atom " << ia << " up=" << up << " dw=" << dw << std::endl;
458-
480+ #ifdef _OPENMP
481+ #pragma omp parallel for
482+ #endif
459483 for (int ig = 0 ; ig < rho_basis->npw ; ig++)
460484 {
461485 const double Gtau =
462486 rho_basis->gcar [ig][0 ] * atom->tau [ia].x +
463487 rho_basis->gcar [ig][1 ] * atom->tau [ia].y +
464488 rho_basis->gcar [ig][2 ] * atom->tau [ia].z ;
465489
466- swap = exp (ci_tpi * Gtau) * rho_lgl[rho_basis->ig2igg [ig]];
490+ std:: complex < double > swap = ModuleBase::libm:: exp (ci_tpi * Gtau) * rho_lgl[rho_basis->ig2igg [ig]];
467491
468492 rho_g3d (0 , ig) += swap * up;
469493 rho_g3d (1 , ig) += swap * dw;
@@ -476,18 +500,27 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
476500 // noncolinear case
477501 if (startmag_type == 1 )
478502 {
503+ double sin_a1, sin_a2, cos_a1, cos_a2;
504+ if (GlobalV::DOMAG)
505+ {
506+ ModuleBase::libm::sincos (atom->angle1 [0 ], &sin_a1, &cos_a1);
507+ ModuleBase::libm::sincos (atom->angle2 [0 ], &sin_a2, &cos_a2);
508+ }
509+ #ifdef _OPENMP
510+ #pragma omp parallel for
511+ #endif
479512 for (int ig = 0 ; ig < rho_basis->npw ; ig++)
480513 {
481514 const std::complex <double > swap = GlobalC::sf.strucFac (it, ig)* rho_lgl[rho_basis->ig2igg [ig]];
482515 rho_g3d (0 , ig) += swap ;
483516 if (GlobalV::DOMAG)
484517 {
485518 rho_g3d (1 , ig) += swap * (GlobalC::ucell.magnet .start_magnetization [it] / atom->ncpp .zv )
486- * sin (atom-> angle1 [ 0 ]) * cos (atom-> angle2 [ 0 ]) ;
519+ * sin_a1 * cos_a2 ;
487520 rho_g3d (2 , ig) += swap * (GlobalC::ucell.magnet .start_magnetization [it] / atom->ncpp .zv )
488- * sin (atom-> angle1 [ 0 ]) * sin (atom-> angle2 [ 0 ]) ;
521+ * sin_a1 * sin_a2 ;
489522 rho_g3d (3 , ig) += swap * (GlobalC::ucell.magnet .start_magnetization [it] / atom->ncpp .zv )
490- * cos (atom-> angle1 [ 0 ]) ;
523+ * cos_a1 ;
491524 }
492525 else if (GlobalV::DOMAG_Z)
493526 {
@@ -498,28 +531,36 @@ void Charge::atomic_rho(const int spin_number_need, double** rho_in, ModulePW::P
498531 }
499532 else if (startmag_type == 2 )
500533 {// zdy-warning-not-available
501- std::complex <double > swap = ModuleBase::ZERO;
502534 std::complex <double > ci_tpi = ModuleBase::NEG_IMAG_UNIT * ModuleBase::TWO_PI;
503535 for (int ia = 0 ;ia<atom->na ;ia++)
504536 {
537+ double sin_a1, sin_a2, cos_a1, cos_a2;
538+ if (GlobalV::DOMAG)
539+ {
540+ ModuleBase::libm::sincos (atom->angle1 [ia], &sin_a1, &cos_a1);
541+ ModuleBase::libm::sincos (atom->angle2 [ia], &sin_a2, &cos_a2);
542+ }
543+ #ifdef _OPENMP
544+ #pragma omp parallel for
545+ #endif
505546 for (int ig = 0 ; ig < rho_basis->npw ; ig++)
506547 {
507548 const double Gtau =
508549 rho_basis->gcar [ig][0 ] * atom->tau [ia].x +
509550 rho_basis->gcar [ig][1 ] * atom->tau [ia].y +
510551 rho_basis->gcar [ig][2 ] * atom->tau [ia].z ;
511552
512- swap = exp (ci_tpi * Gtau) * rho_lgl[rho_basis->ig2igg [ig]];
553+ std:: complex < double > swap = exp (ci_tpi * Gtau) * rho_lgl[rho_basis->ig2igg [ig]];
513554
514555 rho_g3d (0 , ig) += swap;
515556 if (GlobalV::DOMAG)
516557 {
517558 rho_g3d (1 , ig) += swap * (atom->mag [ia] / atom->ncpp .zv )
518- * sin (atom-> angle1 [ia]) * cos (atom-> angle2 [ia]) ;
559+ * sin_a1 * cos_a2 ;
519560 rho_g3d (2 , ig) += swap * (atom->mag [ia] / atom->ncpp .zv )
520- * sin (atom-> angle1 [ia]) * sin (atom-> angle2 [ia]) ;
561+ * sin_a1 * sin_a2 ;
521562 rho_g3d (3 , ig) += swap * (atom->mag [ia] / atom->ncpp .zv )
522- * cos (atom-> angle1 [ia]) ;
563+ * cos_a1 ;
523564 }
524565 else if (GlobalV::DOMAG_Z)
525566 {
0 commit comments