| 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, 24107 (2008). | 
| 39 | * [4]  Vardeman & Gezelter, in progress (2009). | 
| 40 | */ | 
| 41 | #include "parallel/ForceDecomposition.hpp" | 
| 42 | #include "parallel/Communicator.hpp" | 
| 43 | #include "math/SquareMatrix3.hpp" | 
| 44 |  | 
| 45 | using namespace std; | 
| 46 | namespace OpenMD { | 
| 47 |  | 
| 48 | void ForceDecomposition::distributeInitialData() { | 
| 49 | #ifdef IS_MPI | 
| 50 | Snapshot* snap = sman_->getCurrentSnapshot(); | 
| 51 | int nAtoms = snap->getNumberOfAtoms(); | 
| 52 | int nGroups = snap->getNumberOfCutoffGroups(); | 
| 53 |  | 
| 54 | AtomCommRealI = new Communicator<Row,RealType>(nAtoms); | 
| 55 | AtomCommVectorI = new Communicator<Row,Vector3d>(nAtoms); | 
| 56 | AtomCommMatrixI = new Communicator<Row,Mat3x3d>(nAtoms); | 
| 57 |  | 
| 58 | AtomCommRealJ = new Communicator<Column,RealType>(nAtoms); | 
| 59 | AtomCommVectorJ = new Communicator<Column,Vector3d>(nAtoms); | 
| 60 | AtomCommMatrixJ = new Communicator<Column,Mat3x3d>(nAtoms); | 
| 61 |  | 
| 62 | cgCommVectorI = new Communicator<Row,Vector3d>(nGroups); | 
| 63 | cgCommVectorJ = new Communicator<Column,Vector3d>(nGroups); | 
| 64 |  | 
| 65 | int nInRow = AtomCommRealI.getSize(); | 
| 66 | int nInCol = AtomCommRealJ.getSize(); | 
| 67 |  | 
| 68 | vector<vector<RealType> > pot_row(LR_POT_TYPES, | 
| 69 | vector<RealType> (nInRow, 0.0)); | 
| 70 | vector<vector<RealType> > pot_col(LR_POT_TYPES, | 
| 71 | vector<RealType> (nInCol, 0.0)); | 
| 72 |  | 
| 73 | vector<vector<RealType> > pot_local(LR_POT_TYPES, | 
| 74 | vector<RealType> (nAtoms, 0.0)); | 
| 75 |  | 
| 76 | #endif | 
| 77 | } | 
| 78 |  | 
| 79 |  | 
| 80 |  | 
| 81 | void ForceDecomposition::distributeData()  { | 
| 82 | #ifdef IS_MPI | 
| 83 | Snapshot* snap = sman_->getCurrentSnapshot(); | 
| 84 |  | 
| 85 | // gather up the atomic positions | 
| 86 | AtomCommVectorI->gather(snap->atomData.position, | 
| 87 | snap->atomIData.position); | 
| 88 | AtomCommVectorJ->gather(snap->atomData.position, | 
| 89 | snap->atomJData.position); | 
| 90 |  | 
| 91 | // gather up the cutoff group positions | 
| 92 | cgCommVectorI->gather(snap->cgData.position, | 
| 93 | snap->cgIData.position); | 
| 94 | cgCommVectorJ->gather(snap->cgData.position, | 
| 95 | snap->cgJData.position); | 
| 96 |  | 
| 97 | // if needed, gather the atomic rotation matrices | 
| 98 | if (snap->atomData.getStorageLayout() & DataStorage::dslAmat) { | 
| 99 | AtomCommMatrixI->gather(snap->atomData.aMat, | 
| 100 | snap->atomIData.aMat); | 
| 101 | AtomCommMatrixJ->gather(snap->atomData.aMat, | 
| 102 | snap->atomJData.aMat); | 
| 103 | } | 
| 104 |  | 
| 105 | // if needed, gather the atomic eletrostatic frames | 
| 106 | if (snap->atomData.getStorageLayout() & DataStorage::dslElectroFrame) { | 
| 107 | AtomCommMatrixI->gather(snap->atomData.electroFrame, | 
| 108 | snap->atomIData.electroFrame); | 
| 109 | AtomCommMatrixJ->gather(snap->atomData.electroFrame, | 
| 110 | snap->atomJData.electroFrame); | 
| 111 | } | 
| 112 | #endif | 
| 113 | } | 
| 114 |  | 
| 115 | void ForceDecomposition::collectIntermediateData() { | 
| 116 | #ifdef IS_MPI | 
| 117 | Snapshot* snap = sman_->getCurrentSnapshot(); | 
| 118 |  | 
| 119 | if (snap->atomData.getStorageLayout() & DataStorage::dslDensity) { | 
| 120 |  | 
| 121 | AtomCommRealI->scatter(snap->atomIData.density, | 
| 122 | snap->atomData.density); | 
| 123 |  | 
| 124 | int n = snap->atomData.density.size(); | 
| 125 | std::vector<RealType> rho_tmp(n, 0.0); | 
| 126 | AtomCommRealJ->scatter(snap->atomJData.density, rho_tmp); | 
| 127 | for (int i = 0; i < n; i++) | 
| 128 | snap->atomData.density[i] += rho_tmp[i]; | 
| 129 | } | 
| 130 | #endif | 
| 131 | } | 
| 132 |  | 
| 133 | void ForceDecomposition::distributeIntermediateData() { | 
| 134 | #ifdef IS_MPI | 
| 135 | Snapshot* snap = sman_->getCurrentSnapshot(); | 
| 136 | if (snap->atomData.getStorageLayout() & DataStorage::dslFunctional) { | 
| 137 | AtomCommRealI->gather(snap->atomData.functional, | 
| 138 | snap->atomIData.functional); | 
| 139 | AtomCommRealJ->gather(snap->atomData.functional, | 
| 140 | snap->atomJData.functional); | 
| 141 | } | 
| 142 |  | 
| 143 | if (snap->atomData.getStorageLayout() & DataStorage::dslFunctionalDerivative) { | 
| 144 | AtomCommRealI->gather(snap->atomData.functionalDerivative, | 
| 145 | snap->atomIData.functionalDerivative); | 
| 146 | AtomCommRealJ->gather(snap->atomData.functionalDerivative, | 
| 147 | snap->atomJData.functionalDerivative); | 
| 148 | } | 
| 149 | #endif | 
| 150 | } | 
| 151 |  | 
| 152 |  | 
| 153 | void ForceDecomposition::collectData() { | 
| 154 | #ifdef IS_MPI | 
| 155 | Snapshot* snap = sman_->getCurrentSnapshot(); | 
| 156 |  | 
| 157 | int n = snap->atomData.force.size(); | 
| 158 | std::vector<Vector3d> frc_tmp(n, 0.0); | 
| 159 |  | 
| 160 | AtomCommVectorI->scatter(snap->atomIData.force, frc_tmp); | 
| 161 | for (int i = 0; i < n; i++) { | 
| 162 | snap->atomData.force[i] += frc_tmp[i]; | 
| 163 | frc_tmp[i] = 0.0; | 
| 164 | } | 
| 165 |  | 
| 166 | AtomCommVectorJ->scatter(snap->atomJData.force, frc_tmp); | 
| 167 | for (int i = 0; i < n; i++) | 
| 168 | snap->atomData.force[i] += frc_tmp[i]; | 
| 169 |  | 
| 170 |  | 
| 171 | if (snap->atomData.getStorageLayout() & DataStorage::dslTorque) { | 
| 172 |  | 
| 173 | int nt = snap->atomData.force.size(); | 
| 174 | std::vector<Vector3d> trq_tmp(nt, 0.0); | 
| 175 |  | 
| 176 | AtomCommVectorI->scatter(snap->atomIData.torque, trq_tmp); | 
| 177 | for (int i = 0; i < n; i++) { | 
| 178 | snap->atomData.torque[i] += trq_tmp[i]; | 
| 179 | trq_tmp[i] = 0.0; | 
| 180 | } | 
| 181 |  | 
| 182 | AtomCommVectorJ->scatter(snap->atomJData.torque, trq_tmp); | 
| 183 | for (int i = 0; i < n; i++) | 
| 184 | snap->atomData.torque[i] += trq_tmp[i]; | 
| 185 | } | 
| 186 |  | 
| 187 |  | 
| 188 | vector<vector<RealType> > pot_temp(LR_POT_TYPES, | 
| 189 | vector<RealType> (nAtoms, 0.0)); | 
| 190 |  | 
| 191 | for (int i = 0; i < LR_POT_TYPES; i++) { | 
| 192 | AtomCommRealI->scatter(pot_row[i], pot_temp[i]); | 
| 193 | for (int ii = 0;  ii < pot_temp[i].size(); ii++ ) { | 
| 194 | pot_local[i] += pot_temp[i][ii]; | 
| 195 | } | 
| 196 | } | 
| 197 |  | 
| 198 |  | 
| 199 |  | 
| 200 | #endif | 
| 201 | } | 
| 202 |  | 
| 203 | } //end namespace OpenMD |