void SurfaceExtension::calculateESP(Mesh *mesh) { // Calculate the ESP mapped onto the vertices of the Mesh supplied if (!m_molecule) return; // Check to see if molecule has hydrogens bool hasHydrogens = false; foreach (Atom *atom, m_molecule->atoms()) if (atom->atomicNumber() == 1) { hasHydrogens = true; break; } NeighborList *nbrList = new NeighborList(m_molecule, 7.0, false, 2); std::vector<Color3f> colors; for(unsigned int i=0; i < mesh->vertices().size(); ++i) { const Vector3f *v = mesh->vertex(i); double energy = 0.0; QList<Atom*> nbrAtoms = nbrList->nbrs(v); // Include formal charges when there are hydrogens if (hasHydrogens) { foreach(Atom *a, nbrAtoms) { Vector3f dist = a->pos()->cast<float>() - v->cast<float>(); energy += (a->formalCharge() + a->partialCharge()) / dist.squaredNorm(); } } else {
// Ridiculous O(n^2) version of neighbor list // for pedagogical purposes and simplicity void OPENMM_EXPORT computeNeighborListNaive( NeighborList& neighborList, int nAtoms, const AtomLocationList& atomLocations, const vector<set<int> >& exclusions, const RealVec& periodicBoxSize, bool usePeriodic, double maxDistance, double minDistance, bool reportSymmetricPairs ) { neighborList.clear(); double maxDistanceSquared = maxDistance * maxDistance; double minDistanceSquared = minDistance * minDistance; for (AtomIndex atomI = 0; atomI < (AtomIndex) (nAtoms - 1); ++atomI) { for (AtomIndex atomJ = atomI + 1; atomJ < (AtomIndex) nAtoms; ++atomJ) { double pairDistanceSquared = compPairDistanceSquared(atomLocations[atomI], atomLocations[atomJ], periodicBoxSize, usePeriodic); if ( (pairDistanceSquared <= maxDistanceSquared) && (pairDistanceSquared >= minDistanceSquared)) if (exclusions[atomI].find(atomJ) == exclusions[atomI].end()) { neighborList.push_back( AtomPair(atomI, atomJ) ); if (reportSymmetricPairs) neighborList.push_back( AtomPair(atomI, atomJ) ); } } } }
NeighborList* findAliveNeighbors(Grid *g, Pos *p) { NeighborList *nl = new NeighborList(); int x = p->x; int y = p->y; if (g->alive(x - 1, y)) nl->push_front( new Pos( (x - 1), y)); // left neighbor if (g->alive(x + 1, y)) nl->push_front( new Pos( (x + 1), y)); // right neighbor if (g->alive(x - 1, y - 1)) nl->push_front( new Pos( (x - 1), (y - 1))); // upper left neighbor if (g->alive(x, y - 1)) nl->push_front( new Pos( x, (y - 1))); // neighbor above if (g->alive(x + 1, y - 1)) nl->push_front( new Pos( (x + 1), (y - 1))); // upper right neighbor if (g->alive(x - 1, y + 1)) nl->push_front( new Pos( (x - 1), (y + 1))); // lower left neighbor if (g->alive(x, y + 1)) nl->push_front( new Pos(x, (y + 1))); // neighbor below if (g->alive(x + 1, y + 1)) nl->push_front( new Pos( (x + 1), (y + 1))); // lower right neighbor return nl; }
unsigned int NeighborListTest::test(int n, double r) { NeighborList *nbrList = new NeighborList(m_molecule, r, false, n); unsigned int count = 0; for (unsigned int i = 0; i < m_molecule->numAtoms(); ++i) { QList<Atom*> nbrs = nbrList->nbrs(m_molecule->atom(i)); foreach(Atom *nbr, nbrs) { Q_UNUSED(nbr); count++; } }
void aliveNeighbors(Grid *g, NeighborList *nl) { bool is_alive; NeighborList *newNl = new NeighborList(); for (NeighborList::iterator i = nl->begin(); i != nl->end(); i++) { if (!(g->alive(*i))) newNl->push_back((*i)); } delete nl; nl = newNl; }
NeighborList* findNeighbors(Pos *p) { NeighborList *nl = new NeighborList(); nl->push_back( new Pos( (p->x - 1), p->y)); // left neighbor nl->push_back( new Pos( (p->x + 1), p->y)); // right neighbor nl->push_back( new Pos( (p->x - 1), (p->y - 1))); // upper left neighbor nl->push_back( new Pos( p->x, (p->y - 1))); // neighbor above nl->push_back( new Pos( (p->x + 1), (p->y - 1))); // upper right neighbor nl->push_back( new Pos( (p->x - 1), (p->y + 1))); // lower left neighbor nl->push_back( new Pos( p->x, (p->y + 1))); // neighbor below nl->push_back( new Pos( (p->x + 1), (p->y + 1))); // lower right neighbor return nl; }
RealOpenMM MBPolReferenceDispersionForce::calculateForceAndEnergy( int numParticles, const vector<RealVec>& particlePositions, const std::vector<std::vector<int> >& allParticleIndices, const NeighborList& neighborList, vector<RealVec>& forces ) const { RealOpenMM energy = 0.; for( unsigned int ii = 0; ii < neighborList.size(); ii++ ){ OpenMM::AtomPair pair = neighborList[ii]; int siteI = pair.first; int siteJ = pair.second; energy += calculatePairIxn( siteI, siteJ, particlePositions, allParticleIndices, forces ); } return energy; }
// O(n) neighbor list method using voxel hash data structure void OPENMM_EXPORT computeNeighborListVoxelHash( NeighborList& neighborList, int nAtoms, const AtomLocationList& atomLocations, const vector<set<int> >& exclusions, const RealVec& periodicBoxSize, bool usePeriodic, double maxDistance, double minDistance, bool reportSymmetricPairs ) { neighborList.clear(); double edgeSizeX, edgeSizeY, edgeSizeZ; if (!usePeriodic) edgeSizeX = edgeSizeY = edgeSizeZ = maxDistance; // TODO - adjust this as needed else { edgeSizeX = periodicBoxSize[0]/floor(periodicBoxSize[0]/maxDistance); edgeSizeY = periodicBoxSize[1]/floor(periodicBoxSize[1]/maxDistance); edgeSizeZ = periodicBoxSize[2]/floor(periodicBoxSize[2]/maxDistance); } VoxelHash voxelHash(edgeSizeX, edgeSizeY, edgeSizeZ, periodicBoxSize, usePeriodic); for (AtomIndex atomJ = 0; atomJ < (AtomIndex) nAtoms; ++atomJ) // use "j", because j > i for pairs { // 1) Find other atoms that are close to this one const RealVec& location = atomLocations[atomJ]; voxelHash.getNeighbors( neighborList, VoxelItem(&location, atomJ), exclusions, reportSymmetricPairs, maxDistance, minDistance); // 2) Add this atom to the voxelHash voxelHash.insert(atomJ, location); } }
void Grid::update() { Pos p(0, 0); Grid *g = new Grid(m_width, m_height); NeighborList *nl = new NeighborList(); for (int i = 0; i < m_width; i++) { for (int j = 0; j < m_height; j++) { p.x = i; p.y = j; nl = findAliveNeighbors(this, &p); // rules to evolve from if (alive(&p) && (nl->size() < 2)) g->setDead(&p); else if (alive(&p) && (nl->size() > 3)) g->setDead(&p); else if (alive(&p) && ((nl->size() == 2) || (nl->size() == 3))) g->setAlive(&p); else if (dead(&p) && (nl->size() == 3)) g->setAlive(&p); delete nl; } } /* copy over the cells so we can delete the temporary grid */ for (int i = 0; i < g->m_width; i++) { for (int j = 0; j < g->m_height; j++) { m_cells[i][j] = g->m_cells[i][j]; } } delete g; }
typename md::neighbor::result_of::force_model<NeighborList const>::type force_model(NeighborList const& neighbor_list) { return neighbor_list.force_model(); }
void getNeighbors( NeighborList& neighbors, const VoxelItem& referencePoint, const vector<set<int> >& exclusions, bool reportSymmetricPairs, double maxDistance, double minDistance) const { // Loop over neighboring voxels // TODO use more clever selection of neighboring voxels assert(maxDistance > 0); assert(minDistance >= 0); assert(voxelSizeX > 0); assert(voxelSizeY > 0); assert(voxelSizeZ > 0); const AtomIndex atomI = referencePoint.second; const RealVec& locationI = *referencePoint.first; double maxDistanceSquared = maxDistance * maxDistance; double minDistanceSquared = minDistance * minDistance; int dIndexX = int(maxDistance / voxelSizeX) + 1; // How may voxels away do we have to look? int dIndexY = int(maxDistance / voxelSizeY) + 1; int dIndexZ = int(maxDistance / voxelSizeZ) + 1; VoxelIndex centerVoxelIndex = getVoxelIndex(locationI); int lastx = centerVoxelIndex.x+dIndexX; int lasty = centerVoxelIndex.y+dIndexY; int lastz = centerVoxelIndex.z+dIndexZ; if (usePeriodic) { lastx = min(lastx, centerVoxelIndex.x-dIndexX+nx-1); lasty = min(lasty, centerVoxelIndex.y-dIndexY+ny-1); lastz = min(lastz, centerVoxelIndex.z-dIndexZ+nz-1); } for (int x = centerVoxelIndex.x - dIndexX; x <= lastx; ++x) { for (int y = centerVoxelIndex.y - dIndexY; y <= lasty; ++y) { for (int z = centerVoxelIndex.z - dIndexZ; z <= lastz; ++z) { VoxelIndex voxelIndex(x, y, z); if (usePeriodic) { voxelIndex.x = (x+nx)%nx; voxelIndex.y = (y+ny)%ny; voxelIndex.z = (z+nz)%nz; } if (voxelMap.find(voxelIndex) == voxelMap.end()) continue; // no such voxel; skip const Voxel& voxel = voxelMap.find(voxelIndex)->second; for (Voxel::const_iterator itemIter = voxel.begin(); itemIter != voxel.end(); ++itemIter) { const AtomIndex atomJ = itemIter->second; const RealVec& locationJ = *itemIter->first; // Ignore self hits if (atomI == atomJ) continue; // Ignore exclusions. if (exclusions[atomI].find(atomJ) != exclusions[atomI].end()) continue; double dSquared = compPairDistanceSquared(locationI, locationJ, periodicBoxSize, usePeriodic); if (dSquared > maxDistanceSquared) continue; if (dSquared < minDistanceSquared) continue; neighbors.push_back( AtomPair(atomI, atomJ) ); if (reportSymmetricPairs) neighbors.push_back( AtomPair(atomJ, atomI) ); } } } } }
bool is_valid(NeighborList const& neighbor_list) { return neighbor_list.is_valid(); }
void OurNeighbors::LookForNeighbors () { ImageFeaturesListPtr currentImageFeatures = NULL; KKStr relativeDir; log.Level (10) << "OurNeighbors::LookForNeighbors" << endl; /* * create an image feature list from the source directory that corresponds to the * current locations of the actual image files. Where possible, the feature data * file will be used. However, if an image has been moved it's features will have * to be recalculated (which is handled by the function call) and we'll have to * look in the origImageFeatures list for the original predicted class. We must do * this since the predicted class for an image file should NEVER change between * classification runs. */ FeatureFileIOPices::Driver ()->LoadInSubDirectoryTree (PicesFVProducerFactory::Factory (&log), sourceRootDirPath, *mlClasses, false, // useDirectoryNameForClassName, DB (), cancelFlag, false, // rewiteRootFeatureFile log ); currentImageFeatures->FixSipperFileScanLineAndColFields (); lastScanLine = LastScanLine (*currentImageFeatures); { // Make sure Class name matches subdirectory that Example was found in. ImageFeaturesList::iterator idx; for (idx = currentImageFeatures->begin (); idx != currentImageFeatures->end (); idx++) { ImageFeaturesPtr image = *idx; MLClassPtr mlClass = DetermineClassFromFileName (image->ExampleFileName ()); if (mlClass) image->MLClass (mlClass); } } if (excludedClasses) { if (excludedClasses->QueueSize () > 0) RemoveExcludedClasses (currentImageFeatures); } //if (randomizeLocations) // RandomizeLocations (*currentImageFeatures); if (!fromPlanktonName.Empty ()) { fromPlankton = mlClasses->LookUpByName (fromPlanktonName); if (!fromPlankton) { log.Level (-1) << endl << endl << endl << "LookForNeighbors ****** ERROR *******" << endl << endl << "No images that are of PlanktonName[" << fromPlanktonName << "]" << endl << endl; osWaitForEnter (); exit (-1); } } // We will now build Neighbor List NeighborList neighbors (*currentImageFeatures, log); neighbors.FindNearestNeighbors (neighborType, fromPlankton); double allClassesMeanNND = 0.0f; double allClassesMeanStdDev = 0.0f; double allClassesMinDist = 0.0f; double allClassesMaxDist = 0.0f; neighbors.CalcStatistics (allClassesMeanNND, allClassesMeanStdDev, allClassesMinDist, allClassesMaxDist ); if (fromPlankton) neighbors.ReportClassRowRestricted (mlClasses, *report, fromPlankton); else neighbors.ReportClassRow (mlClasses, *report); neighbors.ReportClassNeighbor (mlClasses, *report); if (randomizeLocations) RandomReport (*currentImageFeatures); log.Level (10) << "OurNeighbors::LookForNeighbors Exiting" << endl; } /* LookForNeighbors */
void OurNeighbors::RandomReport (ImageFeaturesList& images) { double allClassesMeanNNDAnyClass = 0.0f; double allClassesMeanStdDevAnyClass = 0.0f; ClassSummaryList classSummaries (log); MLClassList::iterator classIdx; VectorKKStr zScoreSummaryLines; for (classIdx = mlClasses->begin (); classIdx != mlClasses->end (); classIdx++) { MLClassPtr mlClass = *classIdx; if (fromPlankton && (fromPlankton != mlClass)) continue; double randomMeanNND = 0.0f; double randomStdDevNND = 0.0f; double realDataU2Stat = 0.0f; double sampleMeanNND = 0.0f; double sampleStdDevNND = 0.0f; double sampleMaxDist = 0.0f; double sampleMinDist = 0.0f; ImageFeaturesListPtr imagesInClass = images.ExtractExamplesForAGivenClass (mlClass); if (imagesInClass->QueueSize () > 0) { // We are going to make a list of images that has duplicate instances of 'ImageFeatures' objects // This way when we Randomize the locations of each 'sfCentroidRow' and 'sfCentroidCol' we do not // imapct on the original data. ImageFeaturesListPtr imagesToRandomize = new ImageFeaturesList (*imagesInClass, true // 'true means we want to own the data so new instances will be created. ); LLoydsEntryListPtr lloydsEntries = DeriveAllLLoydsBins (*imagesToRandomize); { // We will now get mean and stdDev of nnd for this class NeighborList neighbors (*imagesToRandomize, log); neighbors.FindNearestNeighbors (neighborType, mlClass); neighbors.CalcStatistics (sampleMeanNND, sampleStdDevNND, sampleMaxDist, sampleMinDist); } KKStr randomReportFileName; if (reportFileName.Empty ()) randomReportFileName = "RandomDistanceReport"; else randomReportFileName = osRemoveExtension (reportFileName) + "_Random"; randomReportFileName << "_" << mlClass->Name () << ".txt"; ofstream randomReport (randomReportFileName.Str ()); randomReport << "Random Distribution Report" << endl << endl; randomReport << "Source Directory [" << sourceRootDirPath << "]" << endl; randomReport << "Class [" << mlClass->Name () << "]" << endl; RandomNND randomizeLocations (lastScanLine, *imagesToRandomize, numOfIterations, numOfBuckets, bucketSize, randomReport, log ); randomizeLocations.GenerateReport (); randomMeanNND = randomizeLocations.NND_Mean (); randomStdDevNND = randomizeLocations.NND_StdDev (); realDataU2Stat = randomizeLocations.RealDataU2Stat (); //double sampleMeanNND = 0.0f; //double sampleStdDevNND = 0.0f; double z_Score = Z_Score (sampleMeanNND, randomMeanNND, randomStdDevNND, imagesToRandomize->QueueSize ()); randomReport << endl << endl << endl << "Z-Score" << endl << "=======" << endl << endl << "SampleMeanNND " << "\t" << sampleMeanNND << endl << "SampleStdDevNND " << "\t" << sampleStdDevNND << endl << "RandomMeanNND " << "\t" << randomMeanNND << endl << "RandomStdDevNND " << "\t" << randomStdDevNND << endl << "------- Z-Score " << "\t" << z_Score << endl << endl; KKStr zScoreSummaryLine = mlClass->Name () + "\t" + StrFormatDouble (sampleMeanNND, "###,##0.00") + "\t" + StrFormatDouble (sampleStdDevNND, "###,##0.00") + "\t" + StrFormatDouble (randomMeanNND, "###,##0.00") + "\t" + StrFormatDouble (randomStdDevNND, "###,##0.00") + "\t" + StrFormatDouble (z_Score, "###,##0.000"); zScoreSummaryLines.push_back (zScoreSummaryLine); // The new instance on 'ClassSummary' that is aboiut to be created will take ownership // of lloydsBins. classSummaries.PushOnBack (new ClassSummary (mlClass, lloydsEntries, (float)realDataU2Stat, (float)z_Score)); delete imagesToRandomize; imagesToRandomize = NULL; } delete imagesInClass; imagesInClass = NULL; } if (!fromPlankton) { LLoydsEntryListPtr allClassesLLoydsEntries = DeriveAllLLoydsBins (images); // Create a report for all classes KKStr randomReportFileName; if (reportFileName.Empty ()) randomReportFileName = "RandomDistanceReport_All.txt"; else randomReportFileName = osRemoveExtension (reportFileName) + "_Random_All.txt"; ofstream randomReport (randomReportFileName.Str ()); randomReport << "Source Directory [" << sourceRootDirPath << "]" << endl; randomReport << "Class [" << "All" << "]" << endl; { // Find the mean and stddev of Nearest Neighbor regardless of class. NeighborList allClassesNeighbors (images, log); allClassesNeighbors.FindNearestNeighbors (NeighborType::AnyPlankton, fromPlankton); double allClassesMinDistAnyClass = 0.0f; double allClassesMaxDistAnyClass = 0.0f; allClassesNeighbors.CalcStatistics (allClassesMeanNNDAnyClass, allClassesMeanStdDevAnyClass, allClassesMinDistAnyClass, allClassesMaxDistAnyClass ); } RandomNND randomizeLocations (lastScanLine, images, numOfIterations, numOfBuckets, bucketSize, randomReport, log ); randomizeLocations.GenerateReport (); // All classes Z-Score double allClassesRandomMeanNND = randomizeLocations.NND_Mean (); double allClassesRandomStdDevNND = randomizeLocations.NND_StdDev (); double allClassesRealDataU2Stat = randomizeLocations.RealDataU2Stat (); double z_Score = Z_Score (allClassesMeanNNDAnyClass, allClassesRandomMeanNND, allClassesRandomStdDevNND, images.QueueSize ()); KKStr zScoreSummaryLine = KKStr ("All-Classes") + "\t" + StrFormatDouble (allClassesMeanNNDAnyClass, "###,##0.00") + "\t" + StrFormatDouble (allClassesMeanStdDevAnyClass, "###,##0.00") + "\t" + StrFormatDouble (allClassesRandomMeanNND, "###,##0.00") + "\t" + StrFormatDouble (allClassesRandomStdDevNND, "###,##0.00") + "\t" + StrFormatDouble (z_Score, "###,##0.00"); zScoreSummaryLines.push_back (zScoreSummaryLine); randomReport << endl << endl << endl << "Z-Score" << endl << "=======" << endl << endl << "SampleMeanNND " << "\t" << allClassesMeanNNDAnyClass << endl << "SampleStdDevNND " << "\t" << allClassesMeanStdDevAnyClass << endl << "RandomMeanNND " << "\t" << allClassesRandomMeanNND << endl << "RandomStdDevNND " << "\t" << allClassesRandomStdDevNND << endl << "------- Z-Score " << "\t" << z_Score << endl << endl; classSummaries.PushOnBack (new ClassSummary (MLClass::CreateNewMLClass (KKStr ("AllClasses")), allClassesLLoydsEntries, (float)allClassesRealDataU2Stat, (float)z_Score ) ); } { // Z-Score Summary Report KKB::kkuint32 x; *report << std::endl << std::endl << "Z-Score Summary By Class" << std::endl << std::endl << "ClassName" << "\t" << "SampleMean" << "\t" << "SampleStdDev" << "\t" << "RandomMean" << "\t" << "RandomStdDev" << "\t" << "Z-Score" << std::endl << "=========" << "\t" << "==========" << "\t" << "============" << "\t" << "==========" << "\t" << "============" << "\t" << "=======" << std::endl; for (x = 0; x < zScoreSummaryLines.size (); x++) *report << zScoreSummaryLines[x] << std::endl; } *report << endl << endl << endl; classSummaries.SummaryReport (*report); *report << endl << endl << endl; classSummaries.SpatialOverlapReport (*report); classSummaries.SaveLLoydsBinsData (lloydsBinsFileName, sourceRootDirPath, lastScanLine, baseLLoydsBinSize); } /* RandomReport */