source: src/Fragmentation/Exporters/SaturatedFragment.cpp@ 5aaa43

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 5aaa43 was 5aaa43, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Fixed new copyright line since start of 2013 in CodeChecks test.

  • we must look for either Uni Bonn or myself.
  • added second copyright line since from 1st of Jan 2013 I am not employed by University of Bonn anymore, hence changes to the code are my own copyright.
  • Property mode set to 100644
File size: 18.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2013 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * SaturatedFragment.cpp
26 *
27 * Created on: Mar 3, 2013
28 * Author: heber
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 "SaturatedFragment.hpp"
39
40#include <cmath>
41#include <iostream>
42
43#include "CodePatterns/Assert.hpp"
44#include "CodePatterns/Log.hpp"
45
46#include "LinearAlgebra/Exceptions.hpp"
47#include "LinearAlgebra/Plane.hpp"
48#include "LinearAlgebra/RealSpaceMatrix.hpp"
49#include "LinearAlgebra/Vector.hpp"
50
51#include "Atom/atom.hpp"
52#include "Bond/bond.hpp"
53#include "config.hpp"
54#include "Descriptors/AtomIdDescriptor.hpp"
55#include "Fragmentation/Exporters/HydrogenPool.hpp"
56#include "Fragmentation/HydrogenSaturation_enum.hpp"
57#include "Graph/BondGraph.hpp"
58#include "World.hpp"
59
60SaturatedFragment::SaturatedFragment(
61 const KeySet &_set,
62 KeySetsInUse_t &_container,
63 HydrogenPool &_hydrogens,
64 const enum HydrogenTreatment _treatment,
65 const enum HydrogenSaturation _saturation) :
66 container(_container),
67 set(_set),
68 hydrogens(_hydrogens),
69 FullMolecule(set),
70 treatment(_treatment),
71 saturation(_saturation)
72{
73 // add to in-use container
74 ASSERT( container.find(set) == container.end(),
75 "SaturatedFragment::SaturatedFragment() - the set "
76 +toString(set)+" is already marked as in use.");
77 container.insert(set);
78
79 // prepare saturation hydrogens
80 saturate();
81}
82
83SaturatedFragment::~SaturatedFragment()
84{
85 // release all saturation hydrogens if present
86 for (KeySet::iterator iter = SaturationHydrogens.begin();
87 !SaturationHydrogens.empty();
88 iter = SaturationHydrogens.begin()) {
89 hydrogens.releaseHydrogen(*iter);
90 SaturationHydrogens.erase(iter);
91 }
92
93 // remove ourselves from in-use container
94 KeySetsInUse_t::iterator iter = container.find(set);
95 ASSERT( container.find(set) != container.end(),
96 "SaturatedFragment::SaturatedFragment() - the set "
97 +toString(set)+" is not marked as in use.");
98 container.erase(iter);
99}
100
101void SaturatedFragment::saturate()
102{
103 // gather all atoms in a vector
104 std::vector<atom *> atoms;
105 for (KeySet::const_iterator iter = FullMolecule.begin();
106 iter != FullMolecule.end();
107 ++iter) {
108 atom * const Walker = World::getInstance().getAtom(AtomById(*iter));
109 ASSERT( Walker != NULL,
110 "SaturatedFragment::OutputConfig() - id "
111 +toString(*iter)+" is unknown to World.");
112 atoms.push_back(Walker);
113 }
114
115// bool LonelyFlag = false;
116 for (std::vector<atom *>::const_iterator iter = atoms.begin();
117 iter != atoms.end();
118 ++iter) {
119 atom * const Walker = *iter;
120
121 // go through all bonds
122 const BondList& ListOfBonds = Walker->getListOfBonds();
123 for (BondList::const_iterator BondRunner = ListOfBonds.begin();
124 BondRunner != ListOfBonds.end();
125 ++BondRunner) {
126 atom * const OtherWalker = (*BondRunner)->GetOtherAtom(Walker);
127 // if in set
128 if (set.find(OtherWalker->getId()) != set.end()) {
129 LOG(4, "DEBUG: Walker " << *Walker << " is bound to " << *OtherWalker << ".");
130// if (OtherWalker->getId() > Walker->getId()) { // add bond (Nr check is for adding only one of both variants: ab, ba)
131//// std::stringstream output;
132//// output << "ACCEPT: Adding Bond: "
133// output << Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree());
134//// LOG(3, output.str());
135// //NumBonds[(*iter)->getNr()]++;
136// } else {
137//// LOG(3, "REJECY: Not adding bond, labels in wrong order.");
138// }
139// LonelyFlag = false;
140 } else {
141 LOG(4, "DEBUG: Walker " << *Walker << " is bound to "
142 << *OtherWalker << ", who is not in this fragment molecule.");
143 if (saturation == DoSaturate) {
144// LOG(3, "ACCEPT: Adding Hydrogen to " << (*iter)->Name << " and a bond in between.");
145 if (!AddHydrogenReplacementAtom(
146 (*BondRunner),
147 Walker,
148 OtherWalker,
149 World::getInstance().getConfig()->IsAngstroem == 1))
150 exit(1);
151 }
152// } else if ((treatment == ExcludeHydrogen) && (OtherWalker->getElementNo() == (atomicNumber_t)1)) {
153// // just copy the atom if it's a hydrogen
154// atom * const OtherWalker = Leaf->AddCopyAtom(OtherWalker);
155// Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree());
156// }
157 //NumBonds[(*iter)->getNr()] += Binder->getDegree();
158 }
159 }
160 }
161}
162
163bool SaturatedFragment::OutputConfig(
164 std::ostream &out,
165 const ParserTypes _type) const
166{
167 // gather all atoms in a vector
168 std::vector<atom *> atoms;
169 for (KeySet::const_iterator iter = FullMolecule.begin();
170 iter != FullMolecule.end();
171 ++iter) {
172 atom * const Walker = World::getInstance().getAtom(AtomById(*iter));
173 ASSERT( Walker != NULL,
174 "SaturatedFragment::OutputConfig() - id "
175 +toString(*iter)+" is unknown to World.");
176 atoms.push_back(Walker);
177 }
178
179 // TODO: ScanForPeriodicCorrection() is missing so far!
180 // note however that this is not straight-forward for the following reasons:
181 // - we do not copy all atoms anymore, hence we are forced to shift the real
182 // atoms hither and back again
183 // - we use a long-range potential that supports periodic boundary conditions.
184 // Hence, there we would like the original configuration (split through the
185 // the periodic boundaries). Otherwise, we would have to shift (and probably
186 // interpolate) the potential with OBCs applying.
187
188 // list atoms in fragment for debugging
189 {
190 std::stringstream output;
191 output << "INFO: Contained atoms: ";
192 for (std::vector<atom *>::const_iterator iter = atoms.begin();
193 iter != atoms.end(); ++iter) {
194 output << (*iter)->getName() << " ";
195 }
196 LOG(3, output.str());
197 }
198
199 // store to stream via FragmentParser
200 const bool intermediateResult =
201 FormatParserStorage::getInstance().save(
202 out,
203 FormatParserStorage::getInstance().getSuffixFromType(_type),
204 atoms);
205
206 return intermediateResult;
207}
208
209atom * const SaturatedFragment::getHydrogenReplacement(atom * const replacement)
210{
211 atom * const _atom = hydrogens.leaseHydrogen(); // new atom
212 _atom->setAtomicVelocity(replacement->getAtomicVelocity()); // copy velocity
213 _atom->setFixedIon(replacement->getFixedIon());
214 // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
215 _atom->father = replacement;
216 SaturationHydrogens.insert(_atom->getId());
217 return _atom;
218}
219
220bool SaturatedFragment::AddHydrogenReplacementAtom(
221 bond::ptr TopBond,
222 atom *Origin,
223 atom *Replacement,
224 bool IsAngstroem)
225{
226// Info info(__func__);
227 bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
228 double bondlength; // bond length of the bond to be replaced/cut
229 double bondangle; // bond angle of the bond to be replaced/cut
230 double BondRescale; // rescale value for the hydrogen bond length
231 bond::ptr FirstBond;
232 bond::ptr SecondBond; // Other bonds in double bond case to determine "other" plane
233 atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
234 double b,l,d,f,g, alpha, factors[NDIM]; // hold temporary values in triple bond case for coordination determination
235 Vector Orthovector1, Orthovector2; // temporary vectors in coordination construction
236 Vector InBondvector; // vector in direction of *Bond
237 const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
238 bond::ptr Binder;
239
240 // create vector in direction of bond
241 InBondvector = Replacement->getPosition() - Origin->getPosition();
242 bondlength = InBondvector.Norm();
243
244 // is greater than typical bond distance? Then we have to correct periodically
245 // the problem is not the H being out of the box, but InBondvector have the wrong direction
246 // due to Replacement or Origin being on the wrong side!
247 const BondGraph * const BG = World::getInstance().getBondGraph();
248 const range<double> MinMaxBondDistance(
249 BG->getMinMaxDistance(Origin,Replacement));
250 if (!MinMaxBondDistance.isInRange(bondlength)) {
251// LOG(4, "InBondvector is: " << InBondvector << ".");
252 Orthovector1.Zero();
253 for (int i=NDIM;i--;) {
254 l = Replacement->at(i) - Origin->at(i);
255 if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
256 Orthovector1[i] = (l < 0) ? -1. : +1.;
257 } // (signs are correct, was tested!)
258 }
259 Orthovector1 *= matrix;
260 InBondvector -= Orthovector1; // subtract just the additional translation
261 bondlength = InBondvector.Norm();
262// LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
263 } // periodic correction finished
264
265 InBondvector.Normalize();
266 // get typical bond length and store as scale factor for later
267 ASSERT(Origin->getType() != NULL,
268 "SaturatedFragment::AddHydrogenReplacementAtom() - element of Origin is not given.");
269 BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()-1);
270 if (BondRescale == -1) {
271 ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!");
272 return false;
273 BondRescale = bondlength;
274 } else {
275 if (!IsAngstroem)
276 BondRescale /= (1.*AtomicLengthToAngstroem);
277 }
278
279 // discern single, double and triple bonds
280 switch(TopBond->getDegree()) {
281 case 1:
282 // check whether replacement has been an excluded hydrogen
283 if (Replacement->getType()->getAtomicNumber() == HydrogenPool::HYDROGEN) { // neither rescale nor replace if it's already hydrogen
284 FirstOtherAtom = Replacement;
285 BondRescale = bondlength;
286 } else {
287 FirstOtherAtom = getHydrogenReplacement(Replacement);
288 InBondvector *= BondRescale; // rescale the distance vector to Hydrogen bond length
289 FirstOtherAtom->setPosition(Origin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
290 }
291 FullMolecule.insert(FirstOtherAtom->getId());
292// LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
293 break;
294 case 2:
295 {
296 // determine two other bonds (warning if there are more than two other) plus valence sanity check
297 const BondList& ListOfBonds = Origin->getListOfBonds();
298 for (BondList::const_iterator Runner = ListOfBonds.begin();
299 Runner != ListOfBonds.end();
300 ++Runner) {
301 if ((*Runner) != TopBond) {
302 if (FirstBond == NULL) {
303 FirstBond = (*Runner);
304 FirstOtherAtom = (*Runner)->GetOtherAtom(Origin);
305 } else if (SecondBond == NULL) {
306 SecondBond = (*Runner);
307 SecondOtherAtom = (*Runner)->GetOtherAtom(Origin);
308 } else {
309 ELOG(2, "Detected more than four bonds for atom " << Origin->getName());
310 }
311 }
312 }
313 }
314 if (SecondOtherAtom == NULL) { // then we have an atom with valence four, but only 3 bonds: one to replace and one which is TopBond (third is FirstBond)
315 SecondBond = TopBond;
316 SecondOtherAtom = Replacement;
317 }
318 if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
319// LOG(3, "Regarding the double bond (" << Origin->Name << "<->" << Replacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << Origin->Name << " to determine orthogonal plane.");
320
321 // determine the plane of these two with the *origin
322 try {
323 Orthovector1 = Plane(Origin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
324 }
325 catch(LinearDependenceException &excp){
326 LOG(0, boost::diagnostic_information(excp));
327 // TODO: figure out what to do with the Orthovector in this case
328 AllWentWell = false;
329 }
330 } else {
331 Orthovector1.GetOneNormalVector(InBondvector);
332 }
333 //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
334 // orthogonal vector and bond vector between origin and replacement form the new plane
335 Orthovector1.MakeNormalTo(InBondvector);
336 Orthovector1.Normalize();
337 //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
338
339 // create the two Hydrogens ...
340 FirstOtherAtom = getHydrogenReplacement(Replacement);
341 SecondOtherAtom = getHydrogenReplacement(Replacement);
342 FullMolecule.insert(FirstOtherAtom->getId());
343 FullMolecule.insert(SecondOtherAtom->getId());
344 bondangle = Origin->getType()->getHBondAngle(1);
345 if (bondangle == -1) {
346 ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!");
347 return false;
348 bondangle = 0;
349 }
350 bondangle *= M_PI/180./2.;
351// LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << ".");
352// LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle));
353 FirstOtherAtom->Zero();
354 SecondOtherAtom->Zero();
355 for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
356 FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
357 SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
358 }
359 FirstOtherAtom->Scale(BondRescale); // rescale by correct BondDistance
360 SecondOtherAtom->Scale(BondRescale);
361 //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
362 *FirstOtherAtom += Origin->getPosition();
363 *SecondOtherAtom += Origin->getPosition();
364 // ... and add to molecule
365// LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
366// LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
367 break;
368 case 3:
369 // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
370 FirstOtherAtom = getHydrogenReplacement(Replacement);
371 SecondOtherAtom = getHydrogenReplacement(Replacement);
372 ThirdOtherAtom = getHydrogenReplacement(Replacement);
373 FullMolecule.insert(FirstOtherAtom->getId());
374 FullMolecule.insert(SecondOtherAtom->getId());
375 FullMolecule.insert(ThirdOtherAtom->getId());
376
377 // we need to vectors orthonormal the InBondvector
378 AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
379// LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
380 try{
381 Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
382 }
383 catch(LinearDependenceException &excp) {
384 LOG(0, boost::diagnostic_information(excp));
385 AllWentWell = false;
386 }
387// LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
388
389 // create correct coordination for the three atoms
390 alpha = (Origin->getType()->getHBondAngle(2))/180.*M_PI/2.; // retrieve triple bond angle from database
391 l = BondRescale; // desired bond length
392 b = 2.*l*sin(alpha); // base length of isosceles triangle
393 d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.); // length for InBondvector
394 f = b/sqrt(3.); // length for Orthvector1
395 g = b/2.; // length for Orthvector2
396// LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", ");
397// LOG(3, "The three Bond lengths: " << sqrt(d*d+f*f) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g));
398 factors[0] = d;
399 factors[1] = f;
400 factors[2] = 0.;
401 FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
402 factors[1] = -0.5*f;
403 factors[2] = g;
404 SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
405 factors[2] = -g;
406 ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
407
408 // rescale each to correct BondDistance
409// FirstOtherAtom->x.Scale(&BondRescale);
410// SecondOtherAtom->x.Scale(&BondRescale);
411// ThirdOtherAtom->x.Scale(&BondRescale);
412
413 // and relative to *origin atom
414 *FirstOtherAtom += Origin->getPosition();
415 *SecondOtherAtom += Origin->getPosition();
416 *ThirdOtherAtom += Origin->getPosition();
417
418 // ... and add to molecule
419// LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
420// LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
421// LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << ".");
422 break;
423 default:
424 ELOG(1, "BondDegree does not state single, double or triple bond!");
425 AllWentWell = false;
426 break;
427 }
428
429 return AllWentWell;
430};
Note: See TracBrowser for help on using the repository browser.