ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/devel_omp/src/brains/ForceManager.cpp
(Generate patch)

Comparing branches/devel_omp/src/brains/ForceManager.cpp (file contents):
Revision 1598 by mciznick, Wed Jul 27 14:26:53 2011 UTC vs.
Revision 1608 by mciznick, Tue Aug 9 01:58:56 2011 UTC

# Line 366 | Line 366 | void ForceManager::calcForces() {
366          preCalculation();
367          shortRangeInteractions();
368          //    longRangeInteractions();
369 <        longRangeInteractionsRapaport();
369 >        //      longRangeInteractionsRapaport();
370 >        longRangeInteractionsParallel();
371          postCalculation();
372   }
373  
# Line 528 | Line 529 | void ForceManager::shortRangeInteractions() {
529          curSnapshot->statData[Stats::BEND_POTENTIAL] = bendPotential;
530          curSnapshot->statData[Stats::DIHEDRAL_POTENTIAL] = torsionPotential;
531          curSnapshot->statData[Stats::INVERSION_POTENTIAL] = inversionPotential;
532 + }
533 +
534 + void ForceManager::longRangeInteractionsParallel() {
535 +
536 +        Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
537 +        DataStorage* config = &(curSnapshot->atomData);
538 +        DataStorage* cgConfig = &(curSnapshot->cgData);
539 +
540 +        //calculate the center of mass of cutoff group
541 +
542 +        SimInfo::MoleculeIterator mi;
543 +        Molecule* mol;
544 +        Molecule::CutoffGroupIterator ci;
545 +        CutoffGroup* cg;
546 +
547 +        if (info_->getNCutoffGroups() > 0)
548 +        {
549 +                for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi))
550 +                {
551 +                        for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci))
552 +                        {
553 +                                //                              cerr << "branch1\n";
554 +                                //                              cerr << "globind = " << cg->getGlobalIndex() << ":" << __LINE__ << "\n";
555 +                                cg->updateCOM();
556 +
557 +                                //                              cerr << "gbI: " << cg->getGlobalIndex() << " locI: " << cg->getLocalIndex() << " x: "
558 +                                //                                              << cgConfig->position[cg->getLocalIndex()].x() << " y: " << cgConfig->position[cg->getLocalIndex()].y()
559 +                                //                                              << " z: " << cgConfig->position[cg->getLocalIndex()].z() << "\n";
560 +                        }
561 +                }
562 +        } else
563 +        {
564 +                // center of mass of the group is the same as position of the atom
565 +                // if cutoff group does not exist
566 +                //              cerr << ":" << __LINE__ << "branch2\n";
567 +                cgConfig->position = config->position;
568 +        }
569 +
570 +        fDecomp_->zeroWorkArrays();
571 +        fDecomp_->distributeData();
572 +
573 +        int atom1, atom2, topoDist;
574 +        Vector3d d_grp, dag, d;
575 +        RealType rgrpsq, rgrp, r2, r;
576 +        RealType electroMult, vdwMult;
577 +        RealType vij;
578 +        Vector3d fij, fg, f1;
579 +        tuple3<RealType, RealType, RealType> cuts;
580 +        RealType rCutSq;
581 +        bool in_switching_region;
582 +        RealType sw, dswdr, swderiv;
583 +        vector<int> atomListColumn, atomListRow, atomListLocal;
584 +
585 +        InteractionDataPrv idatPrv;
586 +
587 +        SelfData sdat;
588 +        RealType mf;
589 +        RealType lrPot;
590 +        RealType vpair;
591 +        potVec longRangePotential(0.0);
592 +        potVec workPot(0.0);
593 +
594 +        int loopStart, loopEnd;
595 +        sdat.pot = fDecomp_->getEmbeddingPotential();
596 +
597 +        vector<CutoffGroup *> cgs;
598 +
599 +        for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi))
600 +        {
601 +                for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci))
602 +                {
603 +                        cgs.push_back(cg);
604 +                }
605 +        }
606 +
607 +        loopEnd = PAIR_LOOP;
608 +        if (info_->requiresPrepair())
609 +        {
610 +                loopStart = PREPAIR_LOOP;
611 +        } else
612 +        {
613 +                loopStart = PAIR_LOOP;
614 +        }
615 +
616 +        for (int iLoop = loopStart; iLoop <= loopEnd; iLoop++)
617 +        {
618 +
619 +                if (iLoop == loopStart)
620 +                {
621 +                        bool update_nlist = fDecomp_->checkNeighborList();
622 +                        if (update_nlist)
623 +                                neighborMatW = fDecomp_->buildLayerBasedNeighborList();
624 +                }
625 +
626 +                vector<CutoffGroup *>::iterator cg1;
627 +                vector<CutoffGroup *>::iterator cg2;
628 +
629 + //              int nThreads = 2;
630 +                int chunkSize = cgs.size() / (omp_get_num_threads() * 20);
631 +
632 +                //              printf("before omp loop\n");
633 + #pragma omp parallel /*num_threads(nThreads)*/ default(none) shared(curSnapshot, iLoop, cgs, chunkSize) \
634 +        private(cg1, cg2, cuts, d_grp, rgrpsq, rCutSq, idatPrv, vij, fij, in_switching_region, dswdr, rgrp, \
635 +                        atomListRow, atomListColumn, atom1, atom2, topoDist, d, r2, swderiv, fg, mf, dag)
636 +                {
637 +                        idatPrv.shiftedPot = (cutoffMethod_ == SHIFTED_POTENTIAL) ? true : false;
638 +                        idatPrv.shiftedForce = (cutoffMethod_ == SHIFTED_FORCE) ? true : false;
639 +
640 + //                      printf("Thread %d\n", omp_get_thread_num());
641 + #pragma omp for schedule(dynamic, chunkSize)
642 +                        for (cg1 = cgs.begin(); cg1 < cgs.end(); ++cg1)
643 +                        {
644 +                                for (cg2 = neighborMatW[(*cg1)->getGlobalIndex()].begin(); cg2 < neighborMatW[(*cg1)->getGlobalIndex()].end(); ++cg2)
645 +                                {
646 +
647 +                                        cuts = fDecomp_->getGroupCutoffs((*cg1)->getGlobalIndex(), (*cg2)->getGlobalIndex());
648 +
649 +                                        d_grp = fDecomp_->getIntergroupVector((*cg1), (*cg2));
650 +                                        curSnapshot->wrapVector(d_grp);
651 +                                        rgrpsq = d_grp.lengthSquare();
652 +
653 +                                        rCutSq = cuts.second;
654 +
655 + //                                      printf("Thread %d\tcg1:%d\tcg2:%d d_grp\tx:%f\ty:%f\tz:%f\trgrpsq:%f\n", omp_get_thread_num(), (*cg1)->getGlobalIndex(), (*cg2)->getGlobalIndex(), d_grp.x(), d_grp.y(), d_grp.z(), rgrpsq);
656 +
657 +                                        if (rgrpsq < rCutSq)
658 +                                        {
659 +                                                idatPrv.rcut = cuts.first;
660 +                                                if (iLoop == PAIR_LOOP)
661 +                                                {
662 +                                                        vij = 0.0;
663 +                                                        fij = V3Zero;
664 +                                                }
665 +
666 +                                                in_switching_region = switcher_->getSwitch(rgrpsq, /*sw*/idatPrv.sw, dswdr, rgrp);
667 +
668 + //                                              printf("in_switching_region:%d\trgrpsq:%f\t*idatPrv.sw:%f\tdswdr:%f\trgrp:%f\n", (in_switching_region == false ? 0 : 1), rgrpsq, idatPrv.sw, dswdr, rgrp);
669 +
670 +                                                atomListRow = fDecomp_->getAtomsInGroupRow((*cg1)->getGlobalIndex());
671 +                                                atomListColumn = fDecomp_->getAtomsInGroupColumn((*cg2)->getGlobalIndex());
672 +
673 +                                                for (vector<int>::iterator ia = atomListRow.begin(); ia != atomListRow.end(); ++ia)
674 +                                                {
675 +                                                        atom1 = (*ia);
676 +
677 +                                                        for (vector<int>::iterator jb = atomListColumn.begin(); jb != atomListColumn.end(); ++jb)
678 +                                                        {
679 +                                                                atom2 = (*jb);
680 +
681 + //                                                              printf("atom1:%d atom2:%d\n", atom1, atom2);
682 +                                                                if (!fDecomp_->skipAtomPair(atom1, atom2))
683 +                                                                {
684 +                                                                        idatPrv.vpair = 0.0;
685 +                                                                        idatPrv.pot = 0.0;
686 +                                                                        idatPrv.f1 = V3Zero;
687 +
688 +                                                                        fDecomp_->fillInteractionDataOMP(idatPrv, atom1, atom2);
689 +
690 +                                                                        topoDist = fDecomp_->getTopologicalDistance(atom1, atom2);
691 +                                                                        idatPrv.vdwMult = vdwScale_[topoDist];
692 +                                                                        idatPrv.electroMult = electrostaticScale_[topoDist];
693 +
694 + //                                                                      printf("topoDist:%d\tidatPrv.vdwMult:%f\tidatPrv.electroMult:%f\n", topoDist, idatPrv.vdwMult, idatPrv.electroMult);
695 +
696 +                                                                        if (atomListRow.size() == 1 && atomListColumn.size() == 1)
697 +                                                                        {
698 +                                                                                idatPrv.d = d_grp;
699 +                                                                                idatPrv.r2 = rgrpsq;
700 +                                                                                //                                                                              cerr << "dgrp = " << d_grp << ":" << __LINE__ << "\n";
701 +                                                                        } else
702 +                                                                        {
703 +                                                                                d = fDecomp_->getInteratomicVector(atom1, atom2);
704 +                                                                                curSnapshot->wrapVector(d);
705 +                                                                                r2 = d.lengthSquare();
706 +                                                                                //                                                                              cerr << "datm = " << d << ":" << __LINE__ << "\n";
707 +                                                                                idatPrv.d = d;
708 +                                                                                idatPrv.r2 = r2;
709 +                                                                        }
710 +
711 + //                                                                      printf("idatPrv.d x:%f\ty:%f\tz:%f\tidatPrv.r2:%f\n", (idatPrv.d).x(), (idatPrv.d).y(), (idatPrv.d).z(), idatPrv.r2);
712 +
713 +                                                                        //                                                                      cerr << "idat.d = " << *(idat.d) << ":" << __LINE__ << "\n";
714 +                                                                        idatPrv.rij = sqrt((idatPrv.r2));
715 +                                                                        //                                                                      cerr << "idat.rij = " << *(idat.rij) << "\n";
716 +
717 +                                                                        #pragma omp critical
718 +                                                                        {
719 +                                                                                interactionMan_->initializeOMP();
720 +                                                                        }
721 +
722 +                                                                        if (iLoop == PREPAIR_LOOP)
723 +                                                                        {
724 +                                                                                interactionMan_->doPrePairOMP(idatPrv);
725 +                                                                        } else
726 +                                                                        {
727 +                                                                                interactionMan_->doPairOMP(idatPrv);
728 +                                                                                fDecomp_->unpackInteractionDataOMP(idatPrv, atom1, atom2);
729 +
730 +                                                                                //                                                                              cerr << "d = " << *(idat.d) << "\tv=" << vpair << "\tf=" << f1 << ":" << __LINE__ << "\n";
731 + //                                                                              printf("d x:%f y:%f z:%f vpair:%f f1 x:%f y:%f z:%f\n", idatPrv.d.x(), idatPrv.d.y(), idatPrv.d.z(), idatPrv.vpair, idatPrv.f1.x(), idatPrv.f1.y(), idatPrv.f1.z());
732 +                                                                                #pragma omp critical
733 +                                                                                {
734 +                                                                                        vij += idatPrv.vpair;
735 +                                                                                        fij += idatPrv.f1;
736 +                                                                                        tau -= outProduct(idatPrv.d, idatPrv.f1);
737 +
738 + //                                                                                      printf("vij:%f fij x:%f y:%f z:%f\n", vij, fij.x(), fij.y(), fij.z());
739 +                                                                                }
740 +                                                                        }
741 +                                                                }
742 +                                                        }
743 +                                                }
744 +
745 +                                                if (iLoop == PAIR_LOOP)
746 +                                                {
747 +                                                        if (in_switching_region)
748 +                                                        {
749 +                                                                swderiv = vij * dswdr / rgrp;
750 +                                                                fg = swderiv * d_grp;
751 +                                                                fij += fg;
752 +
753 +                                                                if (atomListRow.size() == 1 && atomListColumn.size() == 1)
754 +                                                                {
755 +                                                                        #pragma omp critical
756 +                                                                        {
757 +                                                                                tau -= outProduct(idatPrv.d, fg);
758 +                                                                        }
759 +                                                                }
760 +
761 +                                                                for (vector<int>::iterator ia = atomListRow.begin(); ia != atomListRow.end(); ++ia)
762 +                                                                {
763 +                                                                        atom1 = (*ia);
764 +                                                                        mf = fDecomp_->getMassFactorRow(atom1);
765 +                                                                        // fg is the force on atom ia due to cutoff group's
766 +                                                                        // presence in switching region
767 +                                                                        fg = swderiv * d_grp * mf;
768 +                                                                        fDecomp_->addForceToAtomRowOMP(atom1, fg);
769 +
770 +                                                                        if (atomListRow.size() > 1)
771 +                                                                        {
772 +                                                                                if (info_->usesAtomicVirial())
773 +                                                                                {
774 +                                                                                        // find the distance between the atom
775 +                                                                                        // and the center of the cutoff group:
776 +                                                                                        dag = fDecomp_->getAtomToGroupVectorRow(atom1, (*cg1)->getGlobalIndex());
777 +                                                                                        #pragma omp critical
778 +                                                                                        {
779 +                                                                                                tau -= outProduct(dag, fg);
780 +                                                                                        }
781 +                                                                                }
782 +                                                                        }
783 +                                                                }
784 +                                                                for (vector<int>::iterator jb = atomListColumn.begin(); jb != atomListColumn.end(); ++jb)
785 +                                                                {
786 +                                                                        atom2 = (*jb);
787 +                                                                        mf = fDecomp_->getMassFactorColumn(atom2);
788 +                                                                        // fg is the force on atom jb due to cutoff group's
789 +                                                                        // presence in switching region
790 +                                                                        fg = -swderiv * d_grp * mf;
791 +                                                                        fDecomp_->addForceToAtomColumn(atom2, fg);
792 +
793 +                                                                        if (atomListColumn.size() > 1)
794 +                                                                        {
795 +                                                                                if (info_->usesAtomicVirial())
796 +                                                                                {
797 +                                                                                        // find the distance between the atom
798 +                                                                                        // and the center of the cutoff group:
799 +                                                                                        dag = fDecomp_->getAtomToGroupVectorColumn(atom2, (*cg2)->getGlobalIndex());
800 +                                                                                        #pragma omp critical
801 +                                                                                        {
802 +                                                                                                tau -= outProduct(dag, fg);
803 +                                                                                        }
804 +                                                                                }
805 +                                                                        }
806 +                                                                }
807 +                                                        }
808 +                                                        //if (!SIM_uses_AtomicVirial) {
809 +                                                        //  tau -= outProduct(d_grp, fij);
810 +                                                        //}
811 +                                                }
812 +                                        }
813 +                                }
814 +                        }// END: omp for loop
815 +                        //              printf("after omp loop\n");
816 +                }
817 +
818 +                if (iLoop == PREPAIR_LOOP)
819 +                {
820 +                        if (info_->requiresPrepair())
821 +                        {
822 +
823 +                                fDecomp_->collectIntermediateData();
824 +
825 +                                for (int atom1 = 0; atom1 < info_->getNAtoms(); atom1++)
826 +                                {
827 +                                        fDecomp_->fillSelfData(sdat, atom1);
828 +                                        interactionMan_->doPreForce(sdat);
829 +                                }
830 +
831 +                                fDecomp_->distributeIntermediateData();
832 +
833 +                        }
834 +                }
835 +        }
836 +
837 +        fDecomp_->collectData();
838 +
839 +        if (info_->requiresSelfCorrection())
840 +        {
841 +
842 +                for (int atom1 = 0; atom1 < info_->getNAtoms(); atom1++)
843 +                {
844 +                        fDecomp_->fillSelfData(sdat, atom1);
845 +                        interactionMan_->doSelfCorrection(sdat);
846 +                }
847 +
848 +        }
849 +
850 +        longRangePotential = *(fDecomp_->getEmbeddingPotential()) + *(fDecomp_->getPairwisePotential());
851 +
852 +        lrPot = longRangePotential.sum();
853 +
854 +        //store the tau and long range potential
855 +        curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL] = lrPot;
856 +        curSnapshot->statData[Stats::VANDERWAALS_POTENTIAL] = longRangePotential[VANDERWAALS_FAMILY];
857 +        curSnapshot->statData[Stats::ELECTROSTATIC_POTENTIAL] = longRangePotential[ELECTROSTATIC_FAMILY];
858   }
859  
860   void ForceManager::longRangeInteractionsRapaport() {
# Line 549 | Line 876 | void ForceManager::longRangeInteractionsRapaport() {
876                  {
877                          for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci))
878                          {
879 < //                              cerr << "branch1\n";
880 < //                              cerr << "globind = " << cg->getGlobalIndex() << ":" << __LINE__ << "\n";
879 >                                //                              cerr << "branch1\n";
880 >                                //                              cerr << "globind = " << cg->getGlobalIndex() << ":" << __LINE__ << "\n";
881                                  cg->updateCOM();
882  
883 < //                              cerr << "gbI: " << cg->getGlobalIndex() << " locI: " << cg->getLocalIndex() << " x: "
884 < //                                              << cgConfig->position[cg->getLocalIndex()].x() << " y: " << cgConfig->position[cg->getLocalIndex()].y()
885 < //                                              << " z: " << cgConfig->position[cg->getLocalIndex()].z() << "\n";
883 >                                //                              cerr << "gbI: " << cg->getGlobalIndex() << " locI: " << cg->getLocalIndex() << " x: "
884 >                                //                                              << cgConfig->position[cg->getLocalIndex()].x() << " y: " << cgConfig->position[cg->getLocalIndex()].y()
885 >                                //                                              << " z: " << cgConfig->position[cg->getLocalIndex()].z() << "\n";
886                          }
887                  }
888          } else
889          {
890                  // center of mass of the group is the same as position of the atom
891                  // if cutoff group does not exist
892 < //              cerr << ":" << __LINE__ << "branch2\n";
892 >                //              cerr << ":" << __LINE__ << "branch2\n";
893                  cgConfig->position = config->position;
894          }
895  
# Line 620 | Line 947 | void ForceManager::longRangeInteractionsRapaport() {
947                                  neighborMatW = fDecomp_->buildLayerBasedNeighborList();
948                  }
949  
623                //              printf("before omp loop\n");
624                //#pragma omp parallel for num_threads(3) default(none) shared(curSnapshot, idat, iLoop, sw, cerr) \
625        private(i, j, cg1, cg2, cuts, d_grp, rgrpsq, rCutSq, vij, fij, in_switching_region, rgrp, dswdr, atomListRow, atomListColumn, atom1, atom2, vpair, workPot, f1, topoDist, vdwMult, electroMult, d, r2, r, swderiv, fg, mf, dag)
950                  for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi))
951                  {
952                          for (cg1 = mol->beginCutoffGroup(ci); cg1 != NULL; cg1 = mol->nextCutoffGroup(ci))
953                          {
954                                  //                      printf("Thread %d executes loop iteration %d\n", omp_get_thread_num(), i);
955 <                                for (vector<CutoffGroup *>::iterator cg2 = neighborMatW[cg1->getGlobalIndex()].begin(); cg2 != neighborMatW[cg1->getGlobalIndex()].end(); ++cg2)
955 >                                for (vector<CutoffGroup *>::iterator cg2 = neighborMatW[cg1->getGlobalIndex()].begin(); cg2
956 >                                                != neighborMatW[cg1->getGlobalIndex()].end(); ++cg2)
957                                  {
958  
959                                          cuts = fDecomp_->getGroupCutoffs(cg1->getGlobalIndex(), (*cg2)->getGlobalIndex());
# Line 677 | Line 1002 | void ForceManager::longRangeInteractionsRapaport() {
1002                                                                          {
1003                                                                                  idat.d = &d_grp;
1004                                                                                  idat.r2 = &rgrpsq;
1005 < //                                                                              cerr << "dgrp = " << d_grp << ":" << __LINE__ << "\n";
1005 >                                                                                //                                                                              cerr << "dgrp = " << d_grp << ":" << __LINE__ << "\n";
1006                                                                          } else
1007                                                                          {
1008                                                                                  d = fDecomp_->getInteratomicVector(atom1, atom2);
1009                                                                                  curSnapshot->wrapVector(d);
1010                                                                                  r2 = d.lengthSquare();
1011 < //                                                                              cerr << "datm = " << d << ":" << __LINE__ << "\n";
1011 >                                                                                //                                                                              cerr << "datm = " << d << ":" << __LINE__ << "\n";
1012                                                                                  idat.d = &d;
1013                                                                                  idat.r2 = &r2;
1014                                                                          }
1015  
1016 < //                                                                      cerr << "idat.d = " << *(idat.d) << ":" << __LINE__ << "\n";
1016 >                                                                        //                                                                      cerr << "idat.d = " << *(idat.d) << ":" << __LINE__ << "\n";
1017                                                                          r = sqrt(*(idat.r2));
1018                                                                          idat.rij = &r;
1019 < //                                                                      cerr << "idat.rij = " << *(idat.rij) << "\n";
1019 >                                                                        //                                                                      cerr << "idat.rij = " << *(idat.rij) << "\n";
1020  
1021                                                                          if (iLoop == PREPAIR_LOOP)
1022                                                                          {
# Line 701 | Line 1026 | void ForceManager::longRangeInteractionsRapaport() {
1026                                                                                  interactionMan_->doPair(idat);
1027                                                                                  fDecomp_->unpackInteractionData(idat, atom1, atom2);
1028  
1029 < //                                                                              cerr << "d = " << *(idat.d) << "\tv=" << vpair << "\tf=" << f1 << ":" << __LINE__ << "\n";
1029 >                                                                                //                                                                              cerr << "d = " << *(idat.d) << "\tv=" << vpair << "\tf=" << f1 << ":" << __LINE__ << "\n";
1030                                                                                  vij += vpair;
1031                                                                                  fij += f1;
1032                                                                                  tau -= outProduct(*(idat.d), f1);
# Line 771 | Line 1096 | void ForceManager::longRangeInteractionsRapaport() {
1096                                          }
1097                                  }
1098                          }
1099 <                }// END: omp for loop
775 <                //              printf("after omp loop\n");
1099 >                }
1100  
1101                  if (iLoop == PREPAIR_LOOP)
1102                  {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines