Ignore:
Timestamp:
May 23, 2008, 9:17:19 AM (17 years ago)
Author:
Frederik Heber <heber@…>
Children:
6145aa7
Parents:
6c96f4
Message:

Lots of for loops now count in reverse order where it does not matter, some 3 -> NDIM

for(i=0;i<var;i++) is slower than for (i=var;i--;) if the order of the i's is not important (note: i-- is also a value and it stops when on i == 0 automatically)
in builder.cpp there were some remnant 3 actually meant to be NDIM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified molecuilder/src/molecules.cpp

    r6c96f4 rf75030  
    2121  int num = par->num;
    2222
    23   for (int i=0;i<num;i++) {
    24     for(int j=0;j<NDIM;j++)
     23  for (int i=num;i--;) {
     24    for(int j=NDIM;j--;)
    2525      sum += (gsl_vector_get(x,j) - (vectors[i])->x[j])*(gsl_vector_get(x,j) - (vectors[i])->x[j]);
    2626  }
     
    5757  NumberOfBondsPerAtom = NULL;
    5858  ElementCount = 0;
    59   for(int i=0;i<MAX_ELEMENTS;i++)
     59  for(int i=MAX_ELEMENTS;i--;)
    6060    ElementsInMolecule[i] = 0;
    6161  cell_size[0] = cell_size[2] = cell_size[5]= 20.;
     
    6969{
    7070  if (ListOfBondsPerAtom != NULL)
    71     for(int i=0;i<AtomCount;i++)
     71    for(int i=AtomCount;i--;)
    7272      Free((void **)&ListOfBondsPerAtom[i], "molecule::~molecule: ListOfBondsPerAtom[i]");
    7373  Free((void **)&ListOfBondsPerAtom, "molecule::~molecule: ListOfBondsPerAtom");
     
    200200//    *out << endl;
    201201    OrthoVector1.Zero();
    202     for (int i=0;i<NDIM;i++) {
     202    for (int i=NDIM;i--;) {
    203203      l = TopReplacement->x.x[i] - TopOrigin->x.x[i];
    204204      if (fabs(l) > BondDistance) { // is component greater than bond distance
     
    312312      FirstOtherAtom->x.Zero();
    313313      SecondOtherAtom->x.Zero();
    314       for(i=0;i<NDIM;i++) { // rotate by half the bond angle in both directions (InBondVector is bondangle = 0 direction)
     314      for(i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondVector is bondangle = 0 direction)
    315315        FirstOtherAtom->x.x[i] = InBondVector.x[i] * cos(bondangle) + OrthoVector1.x[i] * (sin(bondangle));
    316316        SecondOtherAtom->x.x[i] = InBondVector.x[i] * cos(bondangle) + OrthoVector1.x[i] * (-sin(bondangle));
     
    319319      SecondOtherAtom->x.Scale(&BondRescale);
    320320      //*out << Verbose(3) << "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << "." << endl;
    321       for(i=0;i<NDIM;i++) { // and make relative to origin atom
     321      for(i=NDIM;i--;) { // and make relative to origin atom
    322322        FirstOtherAtom->x.x[i] += TopOrigin->x.x[i];
    323323        SecondOtherAtom->x.x[i] += TopOrigin->x.x[i];
     
    471471      first->type = elemente->FindElement(1);
    472472    }
    473     for(j=0;j<NDIM;j++)
     473    for(j=NDIM;j--;)
    474474      first->x.x[j] = x[j];
    475475     AddAtom(first);  // add to molecule
     
    612612  ptr = start->next;  // start at first in list
    613613  if (ptr != end) {   //list not empty?
    614     for (int i=0;i<NDIM;i++) {
     614    for (int i=NDIM;i--;) {
    615615      max->x[i] = ptr->x.x[i];
    616616      min->x[i] = ptr->x.x[i];
     
    619619      ptr = ptr->next;
    620620      //ptr->Output(1,1,out);
    621       for (int i=0;i<NDIM;i++) {
     621      for (int i=NDIM;i--;) {
    622622        max->x[i] = (max->x[i] < ptr->x.x[i]) ? ptr->x.x[i] : max->x[i];
    623623        min->x[i] = (min->x[i] > ptr->x.x[i]) ? ptr->x.x[i] : min->x[i];
     
    626626  }
    627627  // sanity check
    628   for(int i=0;i<NDIM;i++) {
     628  for(int i=NDIM;i--;) {
    629629    if (max->x[i] - min->x[i] > BoxLengths->x[i])
    630630      status = false;
     
    637637    while (ptr->next != end) {
    638638      ptr = ptr->next;
    639       for (int i=0;i<NDIM;i++)
     639      for (int i=NDIM;i--;)
    640640        ptr->x.x[i] += -(max->x[i] + min->x[i])/2. + BoxLengths->x[i]/2.; // first term centers molecule at (0,0,0), second shifts to center of new box
    641641    }
     
    659659  atom *ptr = start->next;  // start at first in list
    660660  if (ptr != end) {   //list not empty?
    661     for (int i=0;i<NDIM;i++) {
     661    for (int i=NDIM;i--;) {
    662662      max->x[i] = ptr->x.x[i];
    663663      min->x[i] = ptr->x.x[i];
     
    666666      ptr = ptr->next;
    667667      //ptr->Output(1,1,out);
    668       for (int i=0;i<NDIM;i++) {
     668      for (int i=NDIM;i--;) {
    669669        max->x[i] = (max->x[i] < ptr->x.x[i]) ? ptr->x.x[i] : max->x[i];
    670670        min->x[i] = (min->x[i] > ptr->x.x[i]) ? ptr->x.x[i] : min->x[i];
     
    677677//    *out << endl;
    678678   
    679     for (int i=0;i<NDIM;i++) {
     679    for (int i=NDIM;i--;) {
    680680      min->x[i] *= -1.;
    681681      max->x[i] += min->x[i];
     
    696696  atom *ptr = start->next;  // start at first in list
    697697 
    698   for(int i=0;i<NDIM;i++) // zero center vector
     698  for(int i=NDIM;i--;) // zero center vector
    699699    center->x[i] = 0.;
    700700   
     
    720720  vector tmp;
    721721 
    722   for(int i=0;i<NDIM;i++) // zero center vector
     722  for(int i=NDIM;i--;) // zero center vector
    723723    center->x[i] = 0.;
    724724   
     
    957957  bool result = true;
    958958  int j =-1;
    959   for (int i=0;i<3;i++) {
     959  for (int i=0;i<NDIM;i++) {
    960960    j += i+1;
    961961    result = result && ((x->x[i] >= 0) && (x->x[i] < cell_size[j]));
     
    12071207{
    12081208        int i = 0;
    1209   for(i=0;i<MAX_ELEMENTS;i++)
     1209  for(i=MAX_ELEMENTS;i--;)
    12101210        ElementsInMolecule[i] = 0;
    12111211        ElementCount = 0;
     
    12171217    i++;
    12181218  }
    1219   for(i=0;i<MAX_ELEMENTS;i++)
     1219  for(i=MAX_ELEMENTS;i--;)
    12201220        ElementCount += (ElementsInMolecule[i] != 0 ? 1 : 0);
    12211221};
     
    12821282{
    12831283  configuration.MaxPsiDouble = configuration.PsiMaxNoDown = configuration.PsiMaxNoUp = configuration.PsiType = 0;
    1284   for(int i=0;i<MAX_ELEMENTS;i++) {
     1284  for(int i=MAX_ELEMENTS;i--;) {
    12851285    if (ElementsInMolecule[i] != 0) {
    12861286      //cout << "CalculateOrbitals: " << elemente->FindElement(i)->name << " has a valence of " << (int)elemente->FindElement(i)->Valence << " and there are " << ElementsInMolecule[i] << " of it." << endl;
     
    13521352    *out << Verbose(1) << "Allocating " << NumberCells << " cells." << endl;
    13531353    CellList = (molecule **) Malloc(sizeof(molecule *)*NumberCells, "molecule::CreateAdjacencyList - ** CellList");
    1354     for (int i=0;i<NumberCells;i++)
     1354    for (int i=NumberCells;i--;)
    13551355      CellList[i] = NULL;
    13561356 
     
    13821382     
    13831383    // 3a. go through every cell
    1384     for (N[0]=0;N[0]<divisor[0];N[0]++)
    1385       for (N[1]=0;N[1]<divisor[1];N[1]++)
    1386         for (N[2]=0;N[2]<divisor[2];N[2]++) {
     1384    for (N[0]=divisor[0];N[0]--;)
     1385      for (N[1]=divisor[1];N[1]--;)
     1386        for (N[2]=divisor[2];N[2]--;) {
    13871387          Index = N[2] + (N[1] + N[0] * divisor[1]) * divisor[2];
    13881388          if (CellList[Index] != NULL) { // if there atoms in this cell
     
    14241424        }
    14251425    // 4. free the cell again
    1426     for (int i=0;i<NumberCells;i++)
     1426    for (int i=NumberCells;i--;)
    14271427      if (CellList[i] != NULL) {
    14281428        delete(CellList[i]);
     
    16961696
    16971697  // initialise each vertex as white with no predecessor, empty queue, color Root lightgray
    1698   for (int i=0;i<AtomCount;i++) {
     1698  for (int i=AtomCount;i--;) {
    16991699    PredecessorList[i] = NULL;
    17001700    ShortestPathList[i] = -1;
     
    17021702  }
    17031703  MinimumRingSize = new int[AtomCount];
    1704   for(int i=0;i<AtomCount;i++)
     1704  for(int i=AtomCount;i--;)
    17051705    MinimumRingSize[i] = AtomCount;
    17061706
     
    18911891      Free((void **)&Walker->ComponentNr, "molecule::InitComponentNumbers: **Walker->ComponentNr");
    18921892    Walker->ComponentNr = (int *) Malloc(sizeof(int)*NumberOfBondsPerAtom[Walker->nr], "molecule::InitComponentNumbers: *Walker->ComponentNr");
    1893     for (int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++)
     1893    for (int i=NumberOfBondsPerAtom[Walker->nr];i--;)
    18941894      Walker->ComponentNr[i] = -1;
    18951895  }
     
    22612261
    22622262  // initialize mask list
    2263   for(int i=0;i<AtomCount;i++)
     2263  for(int i=AtomCount;i--;)
    22642264    AtomMask[i] = false;
    22652265 
     
    24142414  }
    24152415  SortIndex = (int *) Malloc(sizeof(int)*AtomCount, "molecule::FragmentMolecule: *SortIndex");
    2416   for(int i=0;i<AtomCount;i++)
     2416  for(int i=AtomCount;i--;)
    24172417    SortIndex[i] = -1;
    24182418  while (runner->next != elemente->end) { // go through every element
     
    25332533 
    25342534  // free the index lookup list
    2535   for (int i=0;i<FragmentCounter;i++)
     2535  for (int i=FragmentCounter;i--;)
    25362536    Free((void **)&ListOfLocalAtoms[i], "molecule::FragmentMolecule - *ListOfLocalAtoms[]");
    25372537  Free((void **)&ListOfLocalAtoms, "molecule::FragmentMolecule - **ListOfLocalAtoms");
     
    26492649
    26502650  *out << Verbose(1) << "Begin of ParseOrderAtSiteFromFile" << endl;
    2651   for(int i=0;i<AtomCount;i++)
     2651  for(int i=AtomCount;i--;)
    26522652    OrderArray[i] = 0;
    26532653  line << path << "/" << FRAGMENTPREFIX << ORDERATSITEFILE;
    26542654  file.open(line.str().c_str());
    26552655  if (file != NULL) {
    2656     for (int i=0;i<AtomCount;i++) // initialise with 0
     2656    for (int i=AtomCount;i--;) // initialise with 0
    26572657      OrderArray[i] = 0;
    26582658    while (!file.eof()) { // parse from file
     
    26972697  *out << Verbose(2) << "(Re-)Allocating memory." << endl;
    26982698  if (ListOfBondsPerAtom != NULL) {
    2699     for(int i=0;i<AtomCount;i++)
     2699    for(int i=AtomCount;i--;)
    27002700      Free((void **)&ListOfBondsPerAtom[i], "molecule::CreateListOfBondsPerAtom: ListOfBondsPerAtom[i]");
    27012701    Free((void **)&ListOfBondsPerAtom, "molecule::CreateListOfBondsPerAtom: ListOfBondsPerAtom");
     
    27072707
    27082708  // reset bond counts per atom
    2709   for(int i=0;i<AtomCount;i++)
     2709  for(int i=AtomCount;i--;)
    27102710    NumberOfBondsPerAtom[i] = 0;
    27112711  // count bonds per atom
     
    27162716    NumberOfBondsPerAtom[Binder->rightatom->nr]++;
    27172717  }
    2718   // allocate list of bonds per atom
    2719   for(int i=0;i<AtomCount;i++)
     2718  for(int i=AtomCount;i--;) {
     2719    // allocate list of bonds per atom
    27202720    ListOfBondsPerAtom[i] = (bond **) Malloc(sizeof(bond *)*NumberOfBondsPerAtom[i], "molecule::CreateListOfBondsPerAtom: **ListOfBondsPerAtom[]");
    2721   // clear the list again, now each NumberOfBondsPerAtom marks current free field
    2722   for(int i=0;i<AtomCount;i++)
     2721    // clear the list again, now each NumberOfBondsPerAtom marks current free field
    27232722    NumberOfBondsPerAtom[i] = 0;
     2723  }
    27242724  // fill the list
    27252725  Binder = first;
     
    27742774
    27752775  // initialise each vertex as white with no predecessor, empty queue, color Root lightgray
    2776   for (int i=0;i<AtomCount;i++) {
     2776  for (int i=AtomCount;i--;) {
    27772777    PredecessorList[i] = NULL;
    27782778    ShortestPathList[i] = -1;
     
    28902890  // reset parent list
    28912891  *out << Verbose(3) << "Resetting ParentList." << endl;
    2892   for (int i=0;i<Father->AtomCount;i++)
     2892  for (int i=Father->AtomCount;i--;)
    28932893    ParentList[i] = NULL;
    28942894 
     
    29722972 
    29732973  Leaf->BondDistance = BondDistance;
    2974   for(int i=0;i<NDIM*2;i++)
     2974  for(int i=NDIM*2;i--;)
    29752975    Leaf->cell_size[i] = cell_size[i];
    29762976
    29772977  // initialise SonList (indicates when we need to replace a bond with hydrogen instead)
    2978   for(int i=0;i<AtomCount;i++)
     2978  for(int i=AtomCount;i--;)
    29792979    SonList[i] = NULL;
    29802980
     
    32983298  // initialised touched list (stores added atoms on this level)
    32993299  *out << Verbose(1+verbosity) << "Clearing touched list." << endl;
    3300   for (TouchedIndex=0;TouchedIndex<=SubOrder;TouchedIndex++)  // empty touched list
     3300  for (TouchedIndex=SubOrder+1;TouchedIndex--;)  // empty touched list
    33013301    TouchedList[TouchedIndex] = -1;
    33023302  TouchedIndex = 0;
     
    33073307    // count the set bit of i
    33083308    bits = 0;
    3309     for (int j=0;j<SetDimension;j++)
     3309    for (int j=SetDimension;j--;)
    33103310      bits += (i & (1 << j)) >> j;
    33113311     
     
    33393339        while (Binder->next != FragmentSearch->BondsPerSPList[2*SP+1]) {                // compare to end node of this level
    33403340          Binder = Binder->next;
    3341           for (int k=0;k<TouchedIndex;k++) {
     3341          for (int k=TouchedIndex;k--;) {
    33423342            if (Binder->Contains(TouchedList[k]))   // if we added this very endpiece
    33433343              SubSetDimension++;
     
    34193419  int Counter = FragmentSearch.FragmentCounter;
    34203420
    3421   for (int i=0;i<AtomCount;i++) {
     3421  for (int i=AtomCount;i--;) {
    34223422    PredecessorList[i] = NULL;
    34233423  }
     
    34313431  FragmentSearch.ShortestPathList[RootKeyNr] = 0;
    34323432  // prepare the atom stack counters (number of atoms with certain SP on stack)
    3433   for (int i=0;i<Order;i++)
     3433  for (int i=Order;i--;)
    34343434    NumberOfAtomsSPLevel[i] = 0;
    34353435  NumberOfAtomsSPLevel[0] = 1;  // for root
     
    35283528  }
    35293529 
    3530   // creating fragments with the found edge sets
     3530  // creating fragments with the found edge sets  (may be done in reverse order, faster)
    35313531  SP = 0;
    3532   for(int i=0;i<Order;i++) { // sum up all found edges
     3532  for(int i=Order;i--;) { // sum up all found edges
    35333533    Binder = FragmentSearch.BondsPerSPList[2*i];
    35343534    while (Binder->next != FragmentSearch.BondsPerSPList[2*i+1]) {
     
    35763576  // free'ing the bonds lists
    35773577  *out << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl;
    3578   for(int i=0;i<Order;i++) {
     3578  for(int i=Order;i--;) {
    35793579    *out << Verbose(1) << "Current SP level is " << i << ": ";
    35803580    Binder = FragmentSearch.BondsPerSPList[2*i];
     
    36253625  while (flag) {
    36263626    // remove bonds that are beyond bonddistance
    3627     for(int i=0;i<NDIM;i++)
     3627    for(int i=NDIM;i--;)
    36283628      TranslationVector.x[i] = 0.;
    36293629    // scan all bonds
     
    36323632    while ((!flag) && (Binder->next != last)) {
    36333633      Binder = Binder->next;
    3634       for (int i=0;i<NDIM;i++) {
     3634      for (int i=NDIM;i--;) {
    36353635        tmp = fabs(Binder->leftatom->x.x[i] - Binder->rightatom->x.x[i]);
    36363636        //*out << Verbose(3) << "Checking " << i << "th distance of " << *Binder->leftatom << " to " << *Binder->rightatom << ": " << tmp << "." << endl;
     
    36463646    if (flag) {
    36473647      // create translation vector from their periodically modified distance
    3648       for (int i=0;i<NDIM;i++) {
     3648      for (int i=NDIM;i--;) {
    36493649        tmp = Binder->leftatom->x.x[i] - Binder->rightatom->x.x[i];
    36503650        if (fabs(tmp) > BondDistance)
     
    36563656      //*out << endl;
    36573657      // apply to all atoms of first component via BFS
    3658       for (int i=0;i<AtomCount;i++)
     3658      for (int i=AtomCount;i--;)
    36593659        ColorList[i] = white;
    36603660      AtomStack->Push(Binder->leftatom);
     
    38323832  FragmentSearch.FragmentSet = new KeySet;
    38333833  FragmentSearch.Root = FindAtom(RootKeyNr);
    3834   for (int i=0;i<AtomCount;i++) {
     3834  for (int i=AtomCount;i--;) {
    38353835    FragmentSearch.Labels[i] = -1;
    38363836    FragmentSearch.ShortestPathList[i] = -1;
     
    38653865      FragmentSearch.BondsPerSPList = (bond **) Malloc(sizeof(bond *)*Order*2, "molecule::PowerSetGenerator: ***BondsPerSPList");
    38663866      FragmentSearch.BondsPerSPCount = (int *) Malloc(sizeof(int)*Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
    3867       for (int i=0;i<Order;i++) {
     3867      for (int i=Order;i--;) {
    38683868        FragmentSearch.BondsPerSPList[2*i] = new bond();    // start node
    38693869        FragmentSearch.BondsPerSPList[2*i+1] = new bond();  // end node
     
    39333933      // free Order-dependent entries of UniqueFragments structure for next loop cycle
    39343934      Free((void **)&FragmentSearch.BondsPerSPCount, "molecule::PowerSetGenerator: *BondsPerSPCount");
    3935       for (int i=0;i<Order;i++) {
     3935      for (int i=Order;i--;) {
    39363936        delete(FragmentSearch.BondsPerSPList[2*i]);
    39373937        delete(FragmentSearch.BondsPerSPList[2*i+1]);
     
    39893989 * \return <0, \a *a less than \a *b, ==0 if equal, >0 \a *a greater than \a *b
    39903990 */
    3991 int CompareDoubles (const void * a, const void * b)
     3991inline int CompareDoubles (const void * a, const void * b)
    39923992{
    39933993  if (*(double *)a > *(double *)b)
     
    40404040  /// -# ElementsInMolecule
    40414041  if (result) {
    4042     for (flag=0;flag<MAX_ELEMENTS;flag++) {
     4042    for (flag=MAX_ELEMENTS;flag--;) {
    40434043      //*out << Verbose(5) << "Element " <<  flag << ": " << ElementsInMolecule[flag] << " <-> " << OtherMolecule->ElementsInMolecule[flag] << "." << endl;
    40444044      if (ElementsInMolecule[flag] != OtherMolecule->ElementsInMolecule[flag])
     
    40744074    while (Walker->next != end) {
    40754075      Walker = Walker->next;
    4076     //for (i=0;i<AtomCount;i++) {
    40774076      Distances[Walker->nr] = CenterOfGravity.Distance(&Walker->x);
    40784077    }
     
    40914090    PermutationMap = (int *) Malloc(sizeof(int)*AtomCount, "molecule::IsEqualToWithinThreshold: *PermutationMap");
    40924091    *out << Verbose(5) << "Combining Permutation Maps" << endl;
    4093     for(int i=0;i<AtomCount;i++)
     4092    for(int i=AtomCount;i--;)
    40944093      PermutationMap[PermMap[i]] = (int) OtherPermMap[i];
    40954094   
     
    41364135  *out << Verbose(3) << "Begin of GetFatherAtomicMap." << endl;
    41374136  int *AtomicMap = (int *) Malloc(sizeof(int)*AtomCount, "molecule::GetAtomicMap: *AtomicMap");  //Calloc
    4138   for (int i=0;i<AtomCount;i++)
     4137  for (int i=AtomCount;i--;)
    41394138    AtomicMap[i] = -1;
    41404139  if (OtherMolecule == this) {  // same molecule
    4141     for (int i=0;i<AtomCount;i++) // no need as -1 means already that there is trivial correspondence
     4140    for (int i=AtomCount;i--;) // no need as -1 means already that there is trivial correspondence
    41424141      AtomicMap[i] = i;
    41434142    *out << Verbose(4) << "Map is trivial." << endl;
Note: See TracChangeset for help on using the changeset viewer.