Program: 37810
Project: 37810
Buggy source:
protected StructureOrg makeUnitOrg(){
  GAParameters params=GAParameters.getParams();
  Random rand=params.getRandom();
  List<Vect> latVects=RandomSOCreator.makeRandomLattice();
  ArrayList<Site> sitesList=new ArrayList<Site>();
  final int maxFails=100;
  int failCount=0;
  int totNum=0;
  for (int r=0; r < difUnits; r++) {
    totNum=totNum + numUnits[r] * numAtoms[r];
  }
  if (totNum > params.getMaxNumAtoms()) {
    GAParameters.usage("String_Node_Str",true);
  }
  int targetAtoms=RandomNumbers.getUniformIntBetween(numSites,params.getMaxNumAtoms());
  int totAtoms=0;
  double[] fracValues=new double[difUnits];
  double fracTotal=0.0;
  int[] target=new int[difUnits];
  units=new int[difUnits];
  if (numUnits[0] != 0) {
    for (int t=0; t < difUnits; t++) {
      target[t]=numUnits[t];
      units[t]=target[t];
    }
  }
 else {
    for (int y=0; y < difUnits; y++) {
      fracValues[y]=RandomNumbers.getUniformDoubleBetween(0.0,1.0);
      fracTotal=fracTotal + fracValues[y];
    }
    for (int y=0; y < difUnits; y++) {
      fracValues[y]=fracValues[y] / fracTotal;
      target[y]=(int)Math.round(fracValues[y] * targetAtoms);
      target[y]=target[y] / numAtoms[y];
      units[y]=target[y];
      totAtoms=totAtoms + target[y] * numAtoms[y];
    }
  }
  refLoc=new LinkedList<Vect>();
  for (int i=0; i < difUnits; i++) {
    for (int k=0; k < target[i]; k++) {
      int currentSite=0;
      for (int m=0; m < i; m++) {
        currentSite=currentSite + numAtoms[m];
      }
      List<Vect> basis=new LinkedList<Vect>();
      for (int n=0; n < numAtoms[i]; n++) {
        Site s=sites.get(n + currentSite);
        basis.add(s.getCoords());
      }
      List<Vect> rotatedBasis=getRandomRotation3D(basis,i);
      List<Site> newSites=new LinkedList<Site>();
      for (int p=0; p < numAtoms[i]; p++) {
        Site s=sites.get(p + currentSite);
        newSites.add(new Site(s.getElement(),rotatedBasis.get(p)));
      }
      Vect potentialLocation=new Vect(rand.nextDouble(),rand.nextDouble(),rand.nextDouble(),latVects);
      if (refLoc.size() > 0) {
        boolean regen=true;
        while (regen) {
          regen=false;
          for (          Vect v : refLoc) {
            double dist=potentialLocation.subtract(v).length();
            if (dist < params.getMinInteratomicDistance()) {
              regen=true;
            }
            potentialLocation=new Vect(rand.nextDouble(),rand.nextDouble(),rand.nextDouble(),latVects);
          }
        }
      }
      if ((new Cell(latVects,sitesList)).getAtomsInSphereSorted(potentialLocation,params.getMinInteratomicDistance()).size() == 0) {
        for (        Site r : newSites) {
          Element e=r.getElement();
          Vect relativeLoc=r.getCoords();
          Vect v=potentialLocation.plus(relativeLoc);
          sitesList.add(new Site(e,v));
          refLoc.add(potentialLocation);
        }
      }
 else       if (failCount < maxFails) {
        failCount++;
        k--;
      }
    }
  }
  if (!unitsOnly) {
    Composition comp=params.getCompSpace().getRandomIntegerCompInSpace(params.getMinNumAtoms(),(params.getMaxNumAtoms() - totAtoms));
    for (    Element e : comp.getElements())     for (int k=0; k < comp.getOrigAmount(e); k++) {
      Vect potentialLocation=new Vect(rand.nextDouble(),rand.nextDouble(),rand.nextDouble(),latVects);
      if ((new Cell(latVects,sitesList)).getAtomsInSphereSorted(potentialLocation,params.getMinInteratomicDistance()).size() == 0)       sitesList.add(new Site(e,potentialLocation));
 else       if (failCount < maxFails) {
        failCount++;
        k--;
      }
    }
  }
  Cell newStructure=new Cell(latVects,sitesList);
  Cell optimizedStructure=optimizeDensity(newStructure);
  m++;
  return new StructureOrg(optimizedStructure);
}

Fixed version (ground truth):
protected StructureOrg makeUnitOrg(){
  GAParameters params=GAParameters.getParams();
  Random rand=params.getRandom();
  List<Vect> latVects=RandomSOCreator.makeRandomLattice();
  ArrayList<Site> sitesList=new ArrayList<Site>();
  final int maxFails=100;
  int failCount=0;
  int totNum=0;
  for (int r=0; r < difUnits; r++) {
    totNum=totNum + numUnits[r] * numAtoms[r];
  }
  if (totNum > params.getMaxNumAtoms()) {
    GAParameters.usage("String_Node_Str",true);
  }
  int targetAtoms=RandomNumbers.getUniformIntBetweenInclusive(numSites,params.getMaxNumAtoms());
  int totAtoms=0;
  double[] fracValues=new double[difUnits];
  double fracTotal=0.0;
  int[] target=new int[difUnits];
  units=new int[difUnits];
  if (numUnits[0] != 0) {
    for (int t=0; t < difUnits; t++) {
      target[t]=numUnits[t];
      units[t]=target[t];
    }
  }
 else {
    for (int y=0; y < difUnits; y++) {
      fracValues[y]=RandomNumbers.getUniformDoubleBetween(0.0,1.0);
      fracTotal=fracTotal + fracValues[y];
    }
    for (int y=0; y < difUnits; y++) {
      fracValues[y]=fracValues[y] / fracTotal;
      target[y]=(int)Math.round(fracValues[y] * targetAtoms);
      target[y]=target[y] / numAtoms[y];
      units[y]=target[y];
      totAtoms=totAtoms + target[y] * numAtoms[y];
    }
  }
  refLoc=new LinkedList<Vect>();
  for (int i=0; i < difUnits; i++) {
    for (int k=0; k < target[i]; k++) {
      int currentSite=0;
      for (int m=0; m < i; m++) {
        currentSite=currentSite + numAtoms[m];
      }
      List<Vect> basis=new LinkedList<Vect>();
      for (int n=0; n < numAtoms[i]; n++) {
        Site s=sites.get(n + currentSite);
        basis.add(s.getCoords());
      }
      List<Vect> rotatedBasis=getRandomRotation3D(basis,i);
      List<Site> newSites=new LinkedList<Site>();
      for (int p=0; p < numAtoms[i]; p++) {
        Site s=sites.get(p + currentSite);
        newSites.add(new Site(s.getElement(),rotatedBasis.get(p)));
      }
      Vect potentialLocation=new Vect(rand.nextDouble(),rand.nextDouble(),rand.nextDouble(),latVects);
      if (refLoc.size() > 0) {
        boolean regen=true;
        while (regen) {
          regen=false;
          for (          Vect v : refLoc) {
            double dist=potentialLocation.subtract(v).length();
            if (dist < params.getMinInteratomicDistance()) {
              regen=true;
            }
            potentialLocation=new Vect(rand.nextDouble(),rand.nextDouble(),rand.nextDouble(),latVects);
          }
        }
      }
      if ((new Cell(latVects,sitesList)).getAtomsInSphereSorted(potentialLocation,params.getMinInteratomicDistance()).size() == 0) {
        for (        Site r : newSites) {
          Element e=r.getElement();
          Vect relativeLoc=r.getCoords();
          Vect v=potentialLocation.plus(relativeLoc);
          sitesList.add(new Site(e,v));
          refLoc.add(potentialLocation);
        }
      }
 else       if (failCount < maxFails) {
        failCount++;
        k--;
      }
    }
  }
  if (!unitsOnly) {
    Composition comp=params.getCompSpace().getRandomIntegerCompInSpace(params.getMinNumAtoms(),(params.getMaxNumAtoms() - totAtoms));
    for (    Element e : comp.getElements())     for (int k=0; k < comp.getOrigAmount(e); k++) {
      Vect potentialLocation=new Vect(rand.nextDouble(),rand.nextDouble(),rand.nextDouble(),latVects);
      if ((new Cell(latVects,sitesList)).getAtomsInSphereSorted(potentialLocation,params.getMinInteratomicDistance()).size() == 0)       sitesList.add(new Site(e,potentialLocation));
 else       if (failCount < maxFails) {
        failCount++;
        k--;
      }
    }
  }
  Cell newStructure=new Cell(latVects,sitesList);
  Cell optimizedStructure=optimizeDensity(newStructure);
  m++;
  return new StructureOrg(optimizedStructure);
}