| 1 | /* | 
| 2 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 | * | 
| 4 | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | * non-exclusive, royalty free, license to use, modify and | 
| 6 | * redistribute this software in source and binary code form, provided | 
| 7 | * that the following conditions are met: | 
| 8 | * | 
| 9 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 | *    documentation and/or other materials provided with the | 
| 15 | *    distribution. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 28 | * arising out of the use of or inability to use software, even if the | 
| 29 | * University of Notre Dame has been advised of the possibility of | 
| 30 | * such damages. | 
| 31 | * | 
| 32 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | * research, please cite the appropriate papers when you publish your | 
| 34 | * work.  Good starting points are: | 
| 35 | * | 
| 36 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | #include <algorithm> | 
| 44 |  | 
| 45 | #include "RadialDistrFunc.hpp" | 
| 46 | #include "io/DumpReader.hpp" | 
| 47 | #include "primitives/Molecule.hpp" | 
| 48 |  | 
| 49 | namespace OpenMD { | 
| 50 |  | 
| 51 | RadialDistrFunc::RadialDistrFunc(SimInfo* info, | 
| 52 | const std::string& filename, | 
| 53 | const std::string& sele1, | 
| 54 | const std::string& sele2) | 
| 55 | : StaticAnalyser(info, filename), selectionScript1_(sele1), | 
| 56 | selectionScript2_(sele2), evaluator1_(info), evaluator2_(info), | 
| 57 | seleMan1_(info), seleMan2_(info), sele1_minus_common_(info), | 
| 58 | sele2_minus_common_(info), common_(info) { | 
| 59 |  | 
| 60 | evaluator1_.loadScriptString(sele1); | 
| 61 | evaluator2_.loadScriptString(sele2); | 
| 62 |  | 
| 63 | if (!evaluator1_.isDynamic()) { | 
| 64 | seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 65 | validateSelection1(seleMan1_); | 
| 66 | } | 
| 67 | if (!evaluator2_.isDynamic()) { | 
| 68 | seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 69 | validateSelection2(seleMan2_); | 
| 70 | } | 
| 71 |  | 
| 72 | if (!evaluator1_.isDynamic() && !evaluator2_.isDynamic()) { | 
| 73 | // If all selections are static, we can precompute the number | 
| 74 | // of real pairs. | 
| 75 | common_ = seleMan1_ & seleMan2_; | 
| 76 | sele1_minus_common_ = seleMan1_ - common_; | 
| 77 | sele2_minus_common_ = seleMan2_ - common_; | 
| 78 |  | 
| 79 | int nSelected1 = seleMan1_.getSelectionCount(); | 
| 80 | int nSelected2 = seleMan2_.getSelectionCount(); | 
| 81 | int nIntersect = common_.getSelectionCount(); | 
| 82 |  | 
| 83 | nPairs_ = nSelected1 * nSelected2 - (nIntersect +1) * nIntersect/2; | 
| 84 | } | 
| 85 |  | 
| 86 | } | 
| 87 |  | 
| 88 | void RadialDistrFunc::process() { | 
| 89 | Molecule* mol; | 
| 90 | RigidBody* rb; | 
| 91 | SimInfo::MoleculeIterator mi; | 
| 92 | Molecule::RigidBodyIterator rbIter; | 
| 93 |  | 
| 94 | preProcess(); | 
| 95 |  | 
| 96 | DumpReader reader(info_, dumpFilename_); | 
| 97 | int nFrames = reader.getNFrames(); | 
| 98 | nProcessed_ = nFrames / step_; | 
| 99 |  | 
| 100 | for (int i = 0; i < nFrames; i += step_) { | 
| 101 | reader.readFrame(i); | 
| 102 | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 103 |  | 
| 104 | if (evaluator1_.isDynamic()) { | 
| 105 | seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 106 | validateSelection1(seleMan1_); | 
| 107 | } | 
| 108 | if (evaluator2_.isDynamic()) { | 
| 109 | seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 110 | validateSelection2(seleMan2_); | 
| 111 | } | 
| 112 |  | 
| 113 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 114 | mol = info_->nextMolecule(mi)) { | 
| 115 |  | 
| 116 | // Change the positions of atoms which belong to the RigidBodies | 
| 117 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 118 | rb = mol->nextRigidBody(rbIter)) { | 
| 119 | rb->updateAtoms(); | 
| 120 | } | 
| 121 | } | 
| 122 |  | 
| 123 | initializeHistogram(); | 
| 124 |  | 
| 125 | // Selections may overlap, and we need a bit of logic to deal | 
| 126 | // with this. | 
| 127 | // | 
| 128 | // |     s1    | | 
| 129 | // | s1 -c | c | | 
| 130 | //         | c | s2 - c | | 
| 131 | //         |    s2      | | 
| 132 | // | 
| 133 | // s1 : Set of StuntDoubles in selection1 | 
| 134 | // s2 : Set of StuntDoubles in selection2 | 
| 135 | // c  : Intersection of selection1 and selection2 | 
| 136 | // | 
| 137 | // When we loop over the pairs, we can divide the looping into 3 | 
| 138 | // stages: | 
| 139 | // | 
| 140 | // Stage 1 :     [s1-c]      [s2] | 
| 141 | // Stage 2 :     [c]         [s2 - c] | 
| 142 | // Stage 3 :     [c]         [c] | 
| 143 | // Stages 1 and 2 are completely non-overlapping. | 
| 144 | // Stage 3 is completely overlapping. | 
| 145 |  | 
| 146 | if (evaluator1_.isDynamic() || evaluator2_.isDynamic()) { | 
| 147 | common_ = seleMan1_ & seleMan2_; | 
| 148 | sele1_minus_common_ = seleMan1_ - common_; | 
| 149 | sele2_minus_common_ = seleMan2_ - common_; | 
| 150 | int nSelected1 = seleMan1_.getSelectionCount(); | 
| 151 | int nSelected2 = seleMan2_.getSelectionCount(); | 
| 152 | int nIntersect = common_.getSelectionCount(); | 
| 153 |  | 
| 154 | nPairs_ = nSelected1 * nSelected2 - (nIntersect +1) * nIntersect/2; | 
| 155 | } | 
| 156 |  | 
| 157 | processNonOverlapping(sele1_minus_common_, seleMan2_); | 
| 158 | processNonOverlapping(common_,             sele2_minus_common_); | 
| 159 | processOverlapping(common_); | 
| 160 |  | 
| 161 | processHistogram(); | 
| 162 |  | 
| 163 | } | 
| 164 |  | 
| 165 | postProcess(); | 
| 166 |  | 
| 167 | writeRdf(); | 
| 168 | } | 
| 169 |  | 
| 170 | void RadialDistrFunc::processNonOverlapping( SelectionManager& sman1, | 
| 171 | SelectionManager& sman2) { | 
| 172 | StuntDouble* sd1; | 
| 173 | StuntDouble* sd2; | 
| 174 | int i; | 
| 175 | int j; | 
| 176 |  | 
| 177 | // This is the same as a non-overlapping pairwise loop structure: | 
| 178 | // for (int i = 0;  i < ni ; ++i ) { | 
| 179 | //   for (int j = 0; j < nj; ++j) {} | 
| 180 | // } | 
| 181 |  | 
| 182 | for (sd1 = sman1.beginSelected(i); sd1 != NULL; | 
| 183 | sd1 = sman1.nextSelected(i)) { | 
| 184 | for (sd2 = sman2.beginSelected(j); sd2 != NULL; | 
| 185 | sd2 = sman2.nextSelected(j)) { | 
| 186 | collectHistogram(sd1, sd2); | 
| 187 | } | 
| 188 | } | 
| 189 | } | 
| 190 |  | 
| 191 | void RadialDistrFunc::processOverlapping( SelectionManager& sman) { | 
| 192 | StuntDouble* sd1; | 
| 193 | StuntDouble* sd2; | 
| 194 | int i; | 
| 195 | int j; | 
| 196 |  | 
| 197 | // This is the same as a pairwise loop structure: | 
| 198 | // for (int i = 0;  i < n-1 ; ++i ) { | 
| 199 | //   for (int j = i + 1; j < n; ++j) {} | 
| 200 | // } | 
| 201 |  | 
| 202 | for (sd1 = sman.beginSelected(i); sd1 != NULL; | 
| 203 | sd1 = sman.nextSelected(i)) { | 
| 204 | for (j  = i, sd2 = sman.nextSelected(j); sd2 != NULL; | 
| 205 | sd2 = sman.nextSelected(j)) { | 
| 206 | collectHistogram(sd1, sd2); | 
| 207 | } | 
| 208 | } | 
| 209 | } | 
| 210 | } |