source: src/Dynamics/BondVectors.cpp@ 825d33

AutomationFragmentation_failures Candidate_v1.6.1 ChemicalSpaceEvaluator Exclude_Hydrogens_annealWithBondGraph ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity PythonUI_with_named_parameters StoppableMakroAction TremoloParser_IncreasedPrecision
Last change on this file since 825d33 was 825d33, checked in by Frederik Heber <frederik.heber@…>, 7 years ago

Extracted calculation of weights per atom into BondVectors.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2017 Frederik Heber. 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 * BondVectors.cpp
25 *
26 * Created on: Jun 13, 2017
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 "BondVectors.hpp"
39
40#include <algorithm>
41#include <functional>
42#include <iterator>
43#include <numeric>
44
45#include "CodePatterns/Assert.hpp"
46#include "CodePatterns/Log.hpp"
47
48#include "Atom/atom.hpp"
49#include "Bond/bond.hpp"
50#include "Helpers/defs.hpp"
51
52void BondVectors::recalculateBondVectorsAtStep(
53 const size_t &_step) const
54{
55 current_mapped_vectors.clear();
56
57 ASSERT( !container.empty(),
58 "BondVectors::getBondVectors() - container empty, not set properly?");
59 for (container_t::const_iterator iter = container.begin();
60 iter != container.end(); ++iter) {
61 const bond::ptr &current_bond = *iter;
62 Vector BondVector = current_bond->leftatom->getPositionAtStep(_step)
63 - current_bond->rightatom->getPositionAtStep(_step);
64 BondVector.Normalize();
65 current_mapped_vectors.insert( std::make_pair(current_bond, BondVector) );
66 }
67 ASSERT( current_mapped_vectors.size() == container.size(),
68 "BondVectors::getBondVectors() - not same amount of bond vectors as bonds?");
69
70 map_is_dirty = false;
71 current_step_for_map = _step;
72}
73
74size_t BondVectors::getIndexForBond(const bond::ptr &_bond) const
75{
76 std::pair<
77 container_t::const_iterator,
78 container_t::const_iterator> iters =
79 std::equal_range(container.begin(), container.end(), _bond);
80 if (iters.first != container.end())
81 return std::distance(container.begin(), iters.first);
82 else
83 return (size_t)-1;
84}
85
86std::vector<Vector> BondVectors::getAtomsBondVectorsAtStep(
87 const atom &_walker,
88 const size_t &_step) const
89{
90 if (map_is_dirty || (current_step_for_map != _step))
91 recalculateBondVectorsAtStep(_step);
92
93 std::vector<Vector> BondVectors;
94 // gather subset of BondVectors for the current atom
95 const BondList& ListOfBonds = _walker.getListOfBonds();
96 for(BondList::const_iterator bonditer = ListOfBonds.begin();
97 bonditer != ListOfBonds.end(); ++bonditer) {
98 const bond::ptr &current_bond = *bonditer;
99 const BondVectors::mapped_t::const_iterator bviter =
100 current_mapped_vectors.find(current_bond);
101 ASSERT( bviter != current_mapped_vectors.end(),
102 "ForceAnnealing() - cannot find current_bond ?");
103 ASSERT( bviter != current_mapped_vectors.end(),
104 "ForceAnnealing - cannot find current bond "+toString(*current_bond)
105 +" in bonds.");
106 BondVectors.push_back(bviter->second);
107 }
108 LOG(4, "DEBUG: BondVectors for atom #" << _walker.getId() << ": " << BondVectors);
109
110 return BondVectors;
111}
112
113BondVectors::weights_t BondVectors::getWeightsForAtomAtStep(
114 const atom &_walker,
115 const size_t &_step) const
116{
117 const std::vector<Vector> BondVectors =
118 getAtomsBondVectorsAtStep(_walker, _step);
119
120 weights_t weights;
121 for (std::vector<Vector>::const_iterator iter = BondVectors.begin();
122 iter != BondVectors.end(); ++iter) {
123 std::vector<double> scps;
124 scps.reserve(BondVectors.size());
125 std::transform(
126 BondVectors.begin(), BondVectors.end(),
127 std::back_inserter(scps),
128 boost::bind(static_cast< double (*)(double) >(&fabs),
129 boost::bind(&Vector::ScalarProduct, boost::cref(*iter), _1))
130 );
131 const double scp_sum = std::accumulate(scps.begin(), scps.end(), 0.);
132 ASSERT( (scp_sum-1.) > -MYEPSILON,
133 "ForceAnnealing() - sum of weights must be equal or larger one but is "
134 +toString(scp_sum));
135 weights.push_back( 1./scp_sum );
136 }
137 LOG(4, "DEBUG: Weights for atom #" << _walker.getId() << ": " << weights);
138
139 // for testing we check whether all weighted scalar products now come out as 1.
140#ifndef NDEBUG
141 for (std::vector<Vector>::const_iterator iter = BondVectors.begin();
142 iter != BondVectors.end(); ++iter) {
143 std::vector<double> scps;
144 scps.reserve(BondVectors.size());
145 std::transform(
146 BondVectors.begin(), BondVectors.end(),
147 weights.begin(),
148 std::back_inserter(scps),
149 boost::bind(static_cast< double (*)(double) >(&fabs),
150 boost::bind(std::multiplies<double>(),
151 boost::bind(&Vector::ScalarProduct, boost::cref(*iter), _1),
152 _2))
153 );
154 const double scp_sum = std::accumulate(scps.begin(), scps.end(), 0.);
155 ASSERT( fabs(scp_sum - 1.) < MYEPSILON,
156 "ForceAnnealing::operator() - for BondVector "+toString(*iter)
157 +" we have weighted scalar product of "+toString(scp_sum)+" != 1.");
158 }
159#endif
160 return weights;
161}
Note: See TracBrowser for help on using the repository browser.