2828
2929using namespace ALM_NS ;
3030
31- Constraint::Constraint (ALM *alm) : Pointers(alm) {}
31+ Constraint::Constraint (ALM *alm) : Pointers(alm)
32+ {
33+ }
3234
3335Constraint::~Constraint ()
3436{
@@ -556,25 +558,26 @@ void Constraint::constraint_from_symmetry(std::vector<ConstraintClass> *const_ou
556558 int **xyzcomponent;
557559 int nparams;
558560
561+ double *arr_constraint;
559562 bool has_constraint_from_symm = false ;
560-
561563 std::set<FcProperty> list_found;
564+ std::vector<std::vector<double >> const_mat;
562565
563566 for (isym = 0 ; isym < symmetry->nsym ; ++isym) {
564567 if (symmetry->sym_available [isym]) continue ;
565568 has_constraint_from_symm = true ;
566569 }
567570
568- for (order = 0 ; order < maxorder; ++order) {
569- const_out[order].clear ();
570- }
571+ for (order = 0 ; order < maxorder; ++order) const_out[order].clear ();
571572
572573 if (has_constraint_from_symm) {
573574 std::cout << " Generating constraints from crystal symmetry ..." << std::endl;
574575 }
575576
576577 memory->allocate (index_tmp, maxorder + 1 );
577578
579+ const_mat.clear ();
580+
578581 for (order = 0 ; order < maxorder; ++order) {
579582
580583 nparams = fcs->ndup [order].size ();
@@ -589,8 +592,7 @@ void Constraint::constraint_from_symmetry(std::vector<ConstraintClass> *const_ou
589592
590593 // Generate temporary list of parameters
591594 list_found.clear ();
592- for (std::vector<FcProperty>::iterator p = fcs->fc_set [order].begin ();
593- p != fcs->fc_set [order].end (); ++p) {
595+ for (auto p = fcs->fc_set [order].begin (); p != fcs->fc_set [order].end (); ++p) {
594596 for (i = 0 ; i < order + 2 ; ++i) index_tmp[i] = (*p).elems [i];
595597 list_found.insert (FcProperty (order + 2 , (*p).coef ,
596598 index_tmp, (*p).mother ));
@@ -602,25 +604,33 @@ void Constraint::constraint_from_symmetry(std::vector<ConstraintClass> *const_ou
602604
603605 int nfcs = fcs->fc_set [order].size ();
604606
605- #pragma omp parallel
607+ #ifdef _OPENMP
608+ #pragma omp parallel
609+ #endif
606610 {
611+ int j;
607612 int i_prim;
608-
613+ int loc_nonzero;
609614 int *ind;
610615 int *atm_index, *atm_index_symm;
611616 int *xyz_index;
612617 double c_tmp;
613- double *arr_constraint;
614618
615619 std::set<FcProperty>::iterator iter_found;
620+ std::vector<double > const_now_omp;
621+ std::vector<std::vector<double >> const_omp;
616622
617- memory->allocate (arr_constraint, nparams);
618623 memory->allocate (ind, order + 2 );
619624 memory->allocate (atm_index, order + 2 );
620625 memory->allocate (atm_index_symm, order + 2 );
621626 memory->allocate (xyz_index, order + 2 );
622627
623- #pragma omp for private(i, isym, ixyz)
628+ const_omp.clear ();
629+ const_now_omp.resize (nparams);
630+
631+ #ifdef _OPENMP
632+ #pragma omp for private(i, isym, ixyz)
633+ #endif
624634 for (int ii = 0 ; ii < nfcs; ++ii) {
625635 FcProperty list_tmp = fcs->fc_set [order][ii];
626636
@@ -637,9 +647,9 @@ void Constraint::constraint_from_symmetry(std::vector<ConstraintClass> *const_ou
637647 atm_index_symm[i] = symmetry->map_sym [atm_index[i]][isym];
638648 if (!fcs->is_inprim (order + 2 , atm_index_symm)) continue ;
639649
640- for (i = 0 ; i < nparams; ++i) arr_constraint [i] = 0.0 ;
650+ for (i = 0 ; i < nparams; ++i) const_now_omp [i] = 0.0 ;
641651
642- arr_constraint [list_tmp.mother ] = -list_tmp.coef ;
652+ const_now_omp [list_tmp.mother ] = -list_tmp.coef ;
643653
644654 for (ixyz = 0 ; ixyz < nxyz; ++ixyz) {
645655 for (i = 0 ; i < order + 2 ; ++i)
@@ -652,32 +662,64 @@ void Constraint::constraint_from_symmetry(std::vector<ConstraintClass> *const_ou
652662 iter_found = list_found.find (FcProperty (order + 2 , 1.0 , ind, 1 ));
653663 if (iter_found != list_found.end ()) {
654664 c_tmp = fcs->coef_sym (order + 2 , isym, xyz_index, xyzcomponent[ixyz]);
655- arr_constraint [(*iter_found).mother ] += (*iter_found).coef * c_tmp;
665+ const_now_omp [(*iter_found).mother ] += (*iter_found).coef * c_tmp;
656666 }
657667 }
658668
659- if (!is_allzero (nparams, arr_constraint)) {
669+ if (!is_allzero (const_now_omp, loc_nonzero)) {
670+ if (const_now_omp[loc_nonzero] < 0.0 ) {
671+ for (j = 0 ; j < nparams; ++j) const_now_omp[j] *= -1.0 ;
672+ }
673+ const_omp.push_back (const_now_omp);
674+ }
675+
676+ } // close isym loop
677+
678+
679+ // sort-->uniq the array
680+ std::sort (const_omp.begin (), const_omp.end ());
681+ const_omp.erase (std::unique (const_omp.begin (), const_omp.end ()),
682+ const_omp.end ());
683+
684+ // Merge vectors
660685#pragma omp critical
661- const_out[order].push_back (ConstraintClass (nparams,
662- arr_constraint));
686+ {
687+ for (std::vector<std::vector<double >>::iterator it = const_omp.begin ();
688+ it != const_omp.end (); ++it) {
689+ const_mat.push_back (*it);
663690 }
664691 }
665- }
692+ const_omp.clear ();
693+
694+ } // close ii loop
666695
667- memory->deallocate (arr_constraint);
668696 memory->deallocate (ind);
669697 memory->deallocate (atm_index);
670698 memory->deallocate (atm_index_symm);
671699 memory->deallocate (xyz_index);
700+
701+ } // close openmp region
702+
703+ memory->allocate (arr_constraint, nparams);
704+ for (std::vector<std::vector<double >>::reverse_iterator it = const_mat.rbegin ();
705+ it != const_mat.rend (); ++it) {
706+ for (i = 0 ; i < (*it).size (); ++i) {
707+ arr_constraint[i] = (*it)[i];
708+ }
709+ const_out[order].push_back (ConstraintClass (nparams,
710+ arr_constraint));
672711 }
712+ const_mat.clear ();
673713
674714 memory->deallocate (xyzcomponent);
715+ memory->deallocate (arr_constraint);
716+
675717 remove_redundant_rows (nparams, const_out[order], eps8);
676718
677719 if (has_constraint_from_symm) {
678720 std::cout << " done." << std::endl;
679721 }
680- }
722+ } // close loop order
681723
682724 memory->deallocate (index_tmp);
683725
@@ -712,11 +754,11 @@ void Constraint::translational_invariance()
712754 std::vector<int > intlist, data;
713755 std::set<FcProperty> list_found;
714756 std::set<FcProperty>::iterator iter_found;
715- std::vector<std::vector<int > > data_vec;
757+ std::vector<std::vector<int >> data_vec;
716758 std::vector<FcProperty> list_vec;
717759 std::vector<FcProperty>::iterator iter_vec;
718760 std::vector<int > const_now;
719- std::vector<std::vector<int > > const_mat;
761+ std::vector<std::vector<int >> const_mat;
720762
721763 std::cout << " Generating constraints for translational invariance ..." << std::endl;
722764
@@ -848,7 +890,7 @@ void Constraint::translational_invariance()
848890 memory->allocate (intarr_omp, order + 2 );
849891 memory->allocate (intarr_copy_omp, order + 2 );
850892
851- std::vector<std::vector<int > > const_omp;
893+ std::vector<std::vector<int >> const_omp;
852894 std::vector<int > data_omp;
853895 std::vector<int > const_now_omp;
854896
@@ -913,7 +955,7 @@ void Constraint::translational_invariance()
913955 // Merge vectors
914956#pragma omp critical
915957 {
916- for (std::vector<std::vector<int > >::iterator it = const_omp.begin ();
958+ for (std::vector<std::vector<int >>::iterator it = const_omp.begin ();
917959 it != const_omp.end (); ++it) {
918960 const_mat.push_back (*it);
919961 }
@@ -943,7 +985,7 @@ void Constraint::translational_invariance()
943985 // Copy to constraint class
944986
945987 const_translation[order].clear ();
946- for (std::vector<std::vector<int > >::reverse_iterator it = const_mat.rbegin ();
988+ for (std::vector<std::vector<int >>::reverse_iterator it = const_mat.rbegin ();
947989 it != const_mat.rend (); ++it) {
948990 for (i = 0 ; i < (*it).size (); ++i) {
949991 arr_constraint[i] = static_cast <double >((*it)[i]);
@@ -1007,7 +1049,7 @@ void Constraint::rotational_invariance()
10071049 CombinationWithRepetition<int > g;
10081050
10091051 std::vector<int > atom_tmp;
1010- std::vector<std::vector<int > > cell_dummy;
1052+ std::vector<std::vector<int >> cell_dummy;
10111053 std::set<MinimumDistanceCluster>::iterator iter_cluster;
10121054
10131055 setup_rotation_axis (valid_rotation_axis);
@@ -1614,7 +1656,6 @@ bool Constraint::is_allzero(const std::vector<int> vec, int &loc)
16141656{
16151657 loc = -1 ;
16161658 for (int i = 0 ; i < vec.size (); ++i) {
1617- // for(std::vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it){
16181659 if (std::abs (vec[i]) > 0 ) {
16191660 loc = i;
16201661 return false ;
@@ -1623,6 +1664,18 @@ bool Constraint::is_allzero(const std::vector<int> vec, int &loc)
16231664 return true ;
16241665}
16251666
1667+ bool Constraint::is_allzero (const std::vector<double > vec, int &loc)
1668+ {
1669+ loc = -1 ;
1670+ for (int i = 0 ; i < vec.size (); ++i) {
1671+ if (std::abs (vec[i]) > eps) {
1672+ loc = i;
1673+ return false ;
1674+ }
1675+ }
1676+ return true ;
1677+ }
1678+
16261679void Constraint::setup_rotation_axis (bool flag[3 ][3 ])
16271680{
16281681 unsigned int mu, nu;
0 commit comments