Unverified Commit 06d5c1d6 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer Committed by GitHub
Browse files

Merge pull request #1279 from akohlmey/create-atoms-bounding-box

Optimize performance of create_atoms for large boxes and small regions
parents 0e1c6082 e572235e
Loading
Loading
Loading
Loading
+45 −6
Original line number Diff line number Diff line
@@ -725,6 +725,24 @@ void CreateAtoms::add_lattice()
  domain->lattice->bbox(1,bboxhi[0],bboxhi[1],bboxhi[2],
                        xmin,ymin,zmin,xmax,ymax,zmax);

  // narrow down min/max further by extent of the region, if possible

  if (domain->regions[nregion]->bboxflag) {
    const double rxmin = domain->regions[nregion]->extent_xlo;
    const double rxmax = domain->regions[nregion]->extent_xhi;
    const double rymin = domain->regions[nregion]->extent_ylo;
    const double rymax = domain->regions[nregion]->extent_yhi;
    const double rzmin = domain->regions[nregion]->extent_zlo;
    const double rzmax = domain->regions[nregion]->extent_zhi;

    if (rxmin > xmin) xmin = (rxmin > xmax) ? xmax : rxmin;
    if (rxmax < xmax) xmax = (rxmax < xmin) ? xmin : rxmax;
    if (rymin > ymin) ymin = (rymin > ymax) ? ymax : rymin;
    if (rymax < ymax) ymax = (rymax < ymin) ? ymin : rymax;
    if (rzmin > zmin) zmin = (rzmin > zmax) ? zmax : rzmin;
    if (rzmax < zmax) zmax = (rzmax < zmin) ? zmin : rzmax;
  }

  // ilo:ihi,jlo:jhi,klo:khi = loop bounds for lattice overlap of my subbox
  // overlap = any part of a unit cell (face,edge,pt) in common with my subbox
  // in lattice space, subbox is a tilted box
@@ -751,15 +769,32 @@ void CreateAtoms::add_lattice()
  // convert lattice coords to box coords
  // add atom or molecule (on each basis point) if it meets all criteria

  double **basis = domain->lattice->basis;
  double x[3],lamda[3];
  double *coord;
  const double * const * const basis = domain->lattice->basis;

  // rough estimate of total time used for create atoms.
  // one inner loop takes about 25ns on a typical desktop CPU core in 2019
  double testimate = 2.5e-8/3600.0; // convert seconds to hours
  testimate *= static_cast<double>(khi-klo+1);
  testimate *= static_cast<double>(jhi-jlo+1);
  testimate *= static_cast<double>(ihi-ilo+1);
  testimate *= static_cast<double>(nbasis);
  double maxestimate = 0.0;
  MPI_Reduce(&testimate,&maxestimate,1,MPI_DOUBLE,MPI_MAX,0,world);

  if ((comm->me == 0) && (maxestimate > 0.01)) {
    if (screen) fprintf(screen,"WARNING: create_atoms will take "
                        "approx. %.2f hours to complete\n",maxestimate);
    if (logfile) fprintf(logfile,"WARNING: create_atoms will take "
                         "approx. %.2f hours to complete\n",maxestimate);
  }

  int i,j,k,m;
  for (k = klo; k <= khi; k++)
    for (j = jlo; j <= jhi; j++)
      for (i = ilo; i <= ihi; i++)
  for (k = klo; k <= khi; k++) {
    for (j = jlo; j <= jhi; j++) {
      for (i = ilo; i <= ihi; i++) {
        for (m = 0; m < nbasis; m++) {
          double *coord;
          double x[3],lamda[3];

          x[0] = i + basis[m][0];
          x[1] = j + basis[m][1];
@@ -795,6 +830,10 @@ void CreateAtoms::add_lattice()
          else add_molecule(x);
        }
      }
    }
  }
}


/* ----------------------------------------------------------------------
   add a randomly rotated molecule with its center at center