source: src/Filling/Inserter/RandomInserter.cpp@ 6253ed

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 6253ed was 94d5ac6, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: As we use GSL internally, we are as of now required to use GPL v2 license.

  • GNU Scientific Library is used at every place in the code, especially the sub-package LinearAlgebra is based on it which in turn is used really everywhere in the remainder of MoleCuilder. Hence, we have to use the GPL license for the whole of MoleCuilder. In effect, GPL's COPYING was present all along and stated the terms of the GPL v2 license.
  • Hence, I added the default GPL v2 disclaimer to every source file and removed the note about a (actually missing) LICENSE file.
  • also, I added a help-redistribute action which again gives the disclaimer of the GPL v2.
  • also, I changed in the disclaimer that is printed at every program start in builder_init.cpp.
  • TEST: Added check on GPL statement present in every module to test CodeChecks project-disclaimer.
  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * RandomInserter.cpp
25 *
26 * Created on: Feb 21, 2012
27 * Author: heber
28 */
29
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "CodePatterns/MemDebug.hpp"
37
38#include "RandomInserter.hpp"
39
40#include <algorithm>
41
42#include "Atom/atom.hpp"
43#include "CodePatterns/Log.hpp"
44#include "LinearAlgebra/RealSpaceMatrix.hpp"
45#include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
46#include "RandomNumbers/RandomNumberGenerator.hpp"
47
48
49size_t RandomInserter::Max_Attempts = 10;
50
51/** Sets given 3x3 matrix to a random rotation matrix.
52 *
53 * @param a matrix to set
54 */
55inline void setRandomRotation(RealSpaceMatrix &a)
56{
57 double phi[NDIM];
58 RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
59 const double rng_min = random.min();
60 const double rng_max = random.max();
61
62 for (int i=0;i<NDIM;i++) {
63 phi[i] = (random()/(rng_max-rng_min))*(2.*M_PI);
64 LOG(4, "DEBUG: Random angle is " << phi[i] << ".");
65 }
66
67 a.setRotation(phi);
68}
69
70/** Constructor for class RandomInserter.
71 *
72 * @param _MaxAtomComponent maximum component for random atom translations
73 * @param _MaxMoleculeComponent maximum component for random molecule translations
74 * @param _DoRandomRotation whether to do random rotations
75 */
76RandomInserter::RandomInserter(
77 const double _MaxAtomComponent,
78 const double _MaxMoleculeComponent,
79 const bool _DoRandomRotation) :
80 random(RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator()),
81 rng_min(random.min()),
82 rng_max(random.max()),
83 MaxAtomComponent(_MaxAtomComponent),
84 MaxMoleculeComponent(_MaxMoleculeComponent),
85 DoRandomRotation(_DoRandomRotation)
86{}
87
88/** Destructor for class RandomInserter.
89 *
90 */
91RandomInserter::~RandomInserter()
92{}
93
94/** Checks whether all atoms currently are inside the cluster's shape.
95 *
96 * @param cluster cluster to check
97 * @return true - all atoms are inside cluster's shape, false - else
98 */
99bool RandomInserter::AreClustersAtomsInside(ClusterInterface::Cluster_impl cluster) const
100{
101 ClusterInterface::atomIdSet atoms = cluster->getAtomIds();
102 bool status = true;
103
104 for (ClusterInterface::atomIdSet::const_iterator iter = atoms.begin();
105 iter != atoms.end();
106 ++iter)
107 status = status && cluster->isInside(*iter);
108
109 return status;
110}
111
112/** Perform the given random translations and rotations on a cluster.
113 *
114 * @param cluster cluster to translate and rotate
115 * @param Rotations random rotation matrix
116 * @param RandomAtomTranslations vector with random translation for each atom in cluster
117 * @param RandomMoleculeTranslation vector with random translation for cluster
118 * @param offset vector with offset for cluster
119 */
120void RandomInserter::doTranslation(
121 ClusterInterface::Cluster_impl cluster,
122 const RealSpaceMatrix &Rotations,
123 const std::vector<Vector> &RandomAtomTranslations,
124 const Vector &RandomMoleculeTranslation) const
125{
126 AtomIdSet atoms = cluster->getAtoms();
127
128 ASSERT( atoms.size() <= RandomAtomTranslations.size(),
129 "RandomInserter::doTranslation() - insufficient random atom translations given.");
130
131 cluster->transform(Rotations);
132 cluster->translate(RandomMoleculeTranslation);
133 AtomIdSet::iterator miter = atoms.begin();
134 std::vector<Vector>::const_iterator aiter = RandomAtomTranslations.begin();
135 for(;miter != atoms.end(); ++miter, ++aiter)
136 (*miter)->setPosition((*miter)->getPosition() + *aiter);
137}
138
139/** Undos a given random translations and rotations on a cluster.
140 *
141 * @param cluster cluster to translate and rotate
142 * @param Rotations random rotation matrix
143 * @param RandomAtomTranslations vector with random translation for each atom in cluster
144 * @param RandomMoleculeTranslations vector with random translation for cluster
145 * @param offset vector with offset for cluster
146 */
147void RandomInserter::undoTranslation(
148 ClusterInterface::Cluster_impl cluster,
149 const RealSpaceMatrix &Rotations,
150 const std::vector<Vector> &RandomAtomTranslations,
151 const Vector &RandomMoleculeTranslation) const
152{
153 AtomIdSet atoms = cluster->getAtoms();
154 ASSERT( atoms.size() <= RandomAtomTranslations.size(),
155 "RandomInserter::doTranslation() - insufficient random atom translations given.");
156
157 // get inverse rotation
158 RealSpaceMatrix inverseRotations = Rotations.invert();
159
160 AtomIdSet::iterator miter = atoms.begin();
161 std::vector<Vector>::const_iterator aiter = RandomAtomTranslations.begin();
162 for(;miter != atoms.end(); ++miter, ++aiter) {
163 (*miter)->setPosition((*miter)->getPosition() - *aiter);
164 }
165 cluster->translate(zeroVec-RandomMoleculeTranslation);
166 cluster->transform(inverseRotations);
167}
168
169/** Creates a random vector
170 *
171 * @param range range of components, i.e. \f$ v[i] \in [0,range)\f$
172 * @param offset offset for each component
173 * @return \a range * rnd() + \a offset
174 */
175Vector RandomInserter::getRandomVector(const double range, const double offset) const
176{
177 Vector returnVector;
178 for (size_t i=0; i<NDIM; ++i)
179 returnVector[i] = (range*random()/((rng_max-rng_min)/2.)) + offset;
180 return returnVector;
181}
182
183/** Inserter operator that randomly translates and rotates the Cluster.
184 *
185 * \note we assume that clusters are always cloned at origin.
186 *
187 * @param offset
188 * @return true - random translations and rotations did not violate bounding shape, false - else
189 */
190bool RandomInserter::operator()(ClusterInterface::Cluster_impl cluster, const Vector &offset) const
191{
192 // calculate center
193 AtomIdSet atoms = cluster->getAtoms();
194 Vector center;
195 center.Zero();
196 for(AtomIdSet::iterator miter = atoms.begin();miter != atoms.end(); ++miter)
197 center += (*miter)->getPosition();
198 center *= 1./atoms.size();
199
200 // shift cluster to center
201 cluster->translate(zeroVec-center);
202
203 size_t attempt = 0;
204 for (;attempt < Max_Attempts; ++attempt) {
205 // generate random rotation matrix
206 RealSpaceMatrix Rotations;
207 if (DoRandomRotation)
208 setRandomRotation(Rotations);
209 else
210 Rotations.setIdentity();
211
212 // generate random molecule translation vector
213 Vector RandomMoleculeTranslation(getRandomVector(MaxMoleculeComponent, -MaxMoleculeComponent));
214
215 // generate random atom translation vector
216 std::vector<Vector> RandomAtomTranslations(atoms.size(), zeroVec);
217 std::generate_n(RandomAtomTranslations.begin(), atoms.size(),
218 boost::bind(&RandomInserter::getRandomVector, boost::cref(this), MaxAtomComponent, -MaxAtomComponent));
219
220 // apply!
221 doTranslation(cluster, Rotations, RandomAtomTranslations, RandomMoleculeTranslation);
222
223 // ... and check
224 if (!AreClustersAtomsInside(cluster)) {
225 undoTranslation(cluster, Rotations, RandomAtomTranslations, RandomMoleculeTranslation);
226 } else {
227 break;
228 }
229 }
230
231 // and move to final position
232 cluster->translate(offset+center);
233
234 return attempt != Max_Attempts;
235}
Note: See TracBrowser for help on using the repository browser.