source: src/FunctionApproximation/Extractors.cpp@ 228340

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator 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_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 228340 was 228340, checked in by Frederik Heber <heber@…>, 9 years ago

Extractors::gatherAllSymmetricDistances..() additionaly gets edge set to set bonded flag.

  • Property mode set to 100644
File size: 16.5 KB
RevLine 
[8aa597]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[8aa597]6 * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
7 *
8 *
9 * This file is part of MoleCuilder.
10 *
11 * MoleCuilder is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * MoleCuilder is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/*
26 * Extractors.cpp
27 *
28 * Created on: 15.10.2012
29 * Author: heber
30 */
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "CodePatterns/MemDebug.hpp"
38
[caa00e9]39#include <sstream>
[8aa597]40#include <utility>
41#include <vector>
42#include <boost/assign.hpp>
[64bdfd]43#include <boost/bind.hpp>
[caa00e9]44#include <boost/foreach.hpp>
[8aa597]45
[301dbf]46#include "CodePatterns/Assert.hpp"
[51e0e3]47#include "CodePatterns/IteratorAdaptors.hpp"
[8aa597]48#include "CodePatterns/Log.hpp"
[51e0e3]49#include "CodePatterns/toString.hpp"
[8aa597]50
51#include "LinearAlgebra/Vector.hpp"
52
53#include "FunctionApproximation/Extractors.hpp"
54#include "FunctionApproximation/FunctionArgument.hpp"
55
[67044a]56#include "Fragmentation/Homology/HomologyGraph.hpp"
57
[8aa597]58using namespace boost::assign;
59
[49f163]60FunctionModel::arguments_t
61Extractors::gatherAllSymmetricDistanceArguments(
[691be4]62 const Fragment::positions_t& positions,
[c7aac9]63 const Fragment::atomicnumbers_t& atomicnumbers,
[228340]64 const FragmentationEdges::edges_t &edges,
[49f163]65 const size_t globalid)
66{
67 FunctionModel::arguments_t result;
68
69 // go through current configuration and gather all other distances
70 Fragment::positions_t::const_iterator firstpositer = positions.begin();
71 for (;firstpositer != positions.end(); ++firstpositer) {
72 Fragment::positions_t::const_iterator secondpositer = firstpositer;
73 for (; secondpositer != positions.end(); ++secondpositer) {
74 if (firstpositer == secondpositer)
75 continue;
76 argument_t arg;
77 const Vector firsttemp((*firstpositer)[0],(*firstpositer)[1],(*firstpositer)[2]);
78 const Vector secondtemp((*secondpositer)[0],(*secondpositer)[1],(*secondpositer)[2]);
[8aa597]79 arg.distance = firsttemp.distance(secondtemp);
[691be4]80 arg.types = std::make_pair(
[c7aac9]81 (int)atomicnumbers[ std::distance(positions.begin(), firstpositer) ],
82 (int)atomicnumbers[ std::distance(positions.begin(), secondpositer) ]
[691be4]83 );
[8aa597]84 arg.indices = std::make_pair(
85 std::distance(
86 positions.begin(), firstpositer),
87 std::distance(
88 positions.begin(), secondpositer)
89 );
90 arg.globalid = globalid;
[228340]91 arg.bonded = false;
[31a2be]92 LOG(3, "DEBUG: Created argument " << arg << ".");
[8aa597]93 result.push_back(arg);
94 }
95 }
96
97 return result;
98}
99
[bc6705]100Extractors::elementcounts_t
101Extractors::_detail::getElementCounts(
[c5e75f3]102 const Fragment::atomicnumbers_t elements
[bc6705]103 )
104{
105 elementcounts_t elementcounts;
[c5e75f3]106 for (Fragment::atomicnumbers_t::const_iterator elementiter = elements.begin();
[bc6705]107 elementiter != elements.end(); ++elementiter) {
108 // insert new element
109 std::pair< elementcounts_t::iterator, bool> inserter =
110 elementcounts.insert( std::make_pair( *elementiter, 1) );
111 // if already present, just increase its count
112 if (!inserter.second)
113 ++(inserter.first->second);
114 }
115 return elementcounts;
116}
117
[51e0e3]118struct ParticleTypesComparator {
119 bool operator()(const argument_t::types_t &a, const argument_t::types_t &b)
120 {
121 if (a.first < a.second) {
122 if (b.first < b.second) {
123 if (a.first < b.first)
124 return true;
125 else if (a.first > b.first)
126 return false;
127 else
128 return (a.second < b.second);
129 } else {
130 if (a.first < b.second)
131 return true;
132 else if (a.first > b.second)
133 return false;
134 else
135 return (a.second < b.first);
136 }
137 } else {
138 if (b.first < b.second) {
139 if (a.second < b.first)
140 return true;
141 else if (a.second > b.first)
142 return false;
143 else
144 return (a.first < b.second);
145 } else {
146 if (a.second < b.second)
147 return true;
148 else if (a.second > b.second)
149 return false;
150 else
151 return (a.first < b.first);
152 }
153 }
154 }
155};
156
157std::ostream& operator<<(std::ostream &out, const argument_t::types_t &a)
158{
159 out << "[" << a.first << "," << a.second << "]";
160 return out;
161}
162
[e1fe7e]163FunctionModel::list_of_arguments_t Extractors::reorderArgumentsByParticleTypes(
164 const FunctionModel::list_of_arguments_t &listargs,
[e60558]165 const HomologyGraph &_graph,
[67044a]166 const ParticleTypes_t &_types,
167 const HomologyGraph &_bindingmodel
[df350c]168 )
[51e0e3]169{
[e1fe7e]170 FunctionModel::list_of_arguments_t returnargs;
171 for (FunctionModel::list_of_arguments_t::const_iterator iter = listargs.begin();
172 iter != listargs.end(); ++iter) {
173 const FunctionModel::arguments_t &args = *iter;
174 /// We place all arguments into multimap according to particle type pair.
175 // here, we need a special comparator such that types in key pair are always
176 // properly ordered.
177 typedef std::multimap<
178 argument_t::types_t,
179 argument_t,
180 ParticleTypesComparator> TypePair_Argument_Map_t;
181 TypePair_Argument_Map_t argument_map;
182 for(FunctionModel::arguments_t::const_iterator iter = args.begin();
183 iter != args.end(); ++iter) {
184 argument_map.insert( std::make_pair(iter->types, *iter) );
185 }
186 LOG(4, "DEBUG: particle_type map is " << argument_map << ".");
[51e0e3]187
[e1fe7e]188 /// Then, we create the desired unique keys
189 typedef std::vector<argument_t::types_t> UniqueTypes_t;
190 UniqueTypes_t UniqueTypes;
191 for (ParticleTypes_t::const_iterator firstiter = _types.begin();
192 firstiter != _types.end();
193 ++firstiter) {
194 for (ParticleTypes_t::const_iterator seconditer = firstiter;
195 seconditer != _types.end();
196 ++seconditer) {
197 if (seconditer == firstiter)
198 continue;
199 UniqueTypes.push_back( std::make_pair(*firstiter, *seconditer) );
200 }
[51e0e3]201 }
[e1fe7e]202 LOG(4, "DEBUG: Created unique types as keys " << UniqueTypes << ".");
203
204 /// Finally, we use the unique key list to pick corresponding arguments from the map
205 FunctionModel::arguments_t sortedargs;
206 sortedargs.reserve(args.size());
207 while (!argument_map.empty()) {
208 // note that particle_types_t may be flipped, i.e. 1,8 is equal to 8,1, but we
209 // must maintain the correct order in indices in accordance with the order
210 // in _types, i.e. 1,8,1 must match with e.g. ids 1,0,2 where 1 has type 1,
211 // 0 has type 8, and 2 has type 2.
212 // In other words: We do not want to flip/modify arguments such that they match
213 // with the specific type pair we seek but then this comes at the price that we
214 // have flip indices when the types in a pair are flipped.
215
216 typedef std::vector<size_t> indices_t;
217 //!> here, we gather the indices as we discover them
218 indices_t indices;
219 indices.resize(_types.size(), (size_t)-1);
220
221 // these are two iterators that create index pairs in the same way as we have
222 // created type pairs. If a -1 is still present in indices, then the index is
223 // still arbitrary but is then set by the next found index
224 indices_t::iterator firstindex = indices.begin();
225 indices_t::iterator secondindex = firstindex+1;
226
227 //!> here, we gather the current bunch of arguments as we find them
228 FunctionModel::arguments_t argumentbunch;
229 argumentbunch.reserve(UniqueTypes.size());
230
231 for (UniqueTypes_t::const_iterator typeiter = UniqueTypes.begin();
232 typeiter != UniqueTypes.end(); ++typeiter) {
233 // have all arguments to same type pair as list within the found range
234 std::pair<
235 TypePair_Argument_Map_t::iterator,
236 TypePair_Argument_Map_t::iterator> range_t =
237 argument_map.equal_range(*typeiter);
238 LOG(4, "DEBUG: Set of arguments to current key [" << typeiter->first << ","
239 << typeiter->second << "] is " << std::list<argument_t>(
240 MapValueIterator<TypePair_Argument_Map_t::iterator>(range_t.first),
241 MapValueIterator<TypePair_Argument_Map_t::iterator>(range_t.second)
242 ) << ".");
243 // the first key is always easy and is pivot which the rest has to be associated to
244 if (typeiter == UniqueTypes.begin()) {
245 const argument_t & arg = range_t.first->second;
246 if ((typeiter->first == arg.types.first) && (typeiter->second == arg.types.second)) {
247 // store in correct order
248 *firstindex = arg.indices.first;
249 *secondindex = arg.indices.second;
250 } else {
251 // store in flipped order
252 *firstindex = arg.indices.second;
253 *secondindex = arg.indices.first;
254 }
255 argumentbunch.push_back(arg);
256 argument_map.erase(range_t.first);
257 LOG(4, "DEBUG: Gathered first argument " << arg << ".");
[51e0e3]258 } else {
[e1fe7e]259 // go through the range and pick the first argument matching the index constraints
260 for (TypePair_Argument_Map_t::iterator argiter = range_t.first;
261 argiter != range_t.second; ++argiter) {
262 // seconditer may be -1 still
263 const argument_t &arg = argiter->second;
264 if (arg.indices.first == *firstindex) {
265 if ((arg.indices.second == *secondindex) || (*secondindex == (size_t)-1)) {
266 if (*secondindex == (size_t)-1)
267 *secondindex = arg.indices.second;
268 argumentbunch.push_back(arg);
269 argument_map.erase(argiter);
270 LOG(4, "DEBUG: Gathered another argument " << arg << ".");
271 break;
272 }
273 } else if ((arg.indices.first == *secondindex) || (*secondindex == (size_t)-1)) {
274 if (arg.indices.second == *firstindex) {
275 if (*secondindex == (size_t)-1)
276 *secondindex = arg.indices.first;
277 argumentbunch.push_back(arg);
278 argument_map.erase(argiter);
279 LOG(4, "DEBUG: Gathered another (flipped) argument " << arg << ".");
280 break;
281 }
[51e0e3]282 }
283 }
284 }
[e1fe7e]285 // move along in indices and check bounds
286 ++secondindex;
287 if (secondindex == indices.end()) {
288 ++firstindex;
289 if (firstindex != indices.end()-1)
290 secondindex = firstindex+1;
291 }
[51e0e3]292 }
[e1fe7e]293 ASSERT( (firstindex == indices.end()-1) && (secondindex == indices.end()),
294 "Extractors::reorderArgumentsByParticleTypes() - we have not gathered enough arguments.");
295 ASSERT( argumentbunch.size() == UniqueTypes.size(),
296 "Extractors::reorderArgumentsByParticleTypes() - we have not gathered enough arguments.");
297 // place bunch of arguments in return args
298 LOG(3, "DEBUG: Given types " << _types << " and found indices " << indices << ".");
299 LOG(3, "DEBUG: Final bunch of arguments is " << argumentbunch << ".");
300 sortedargs.insert(sortedargs.end(), argumentbunch.begin(), argumentbunch.end());
[51e0e3]301 }
[e1fe7e]302 returnargs.push_back(sortedargs);
[51e0e3]303 }
304
305 return returnargs;
306}
307
[e1fe7e]308FunctionModel::list_of_arguments_t Extractors::filterArgumentsByParticleTypes(
[51e0e3]309 const FunctionModel::arguments_t &args,
[e60558]310 const HomologyGraph &_graph,
[67044a]311 const ParticleTypes_t &_types,
312 const HomologyGraph &_bindingmodel
[51e0e3]313 )
[df350c]314{
315 typedef std::list< argument_t > ListArguments_t;
316 ListArguments_t availableList(args.begin(), args.end());
[31a2be]317 LOG(2, "DEBUG: Initial list of args is " << args << ".");
318
[df350c]319
320 // TODO: fill a lookup map such that we don't have O(M^3) scaling, if M is number
321 // of types (and we always must have M(M-1)/2 args) but O(M^2 log(M)). However, as
322 // M is very small (<=3), this is not necessary fruitful now.
323// typedef ParticleTypes_t firsttype;
324// typedef ParticleTypes_t secondtype;
325// typedef std::map< firsttype, std::map< secondtype, boost::ref(args) > > ArgsLookup_t;
326// ArgsLookup_t ArgsLookup;
327
328 // basically, we have two choose any two pairs out of types but only those
[51e0e3]329 // where the first is less than the latter. Hence, we start the second
[df350c]330 // iterator at the current position of the first one and skip the equal case.
[e1fe7e]331 FunctionModel::arguments_t allargs;
332 allargs.reserve(args.size());
[df350c]333 for (ParticleTypes_t::const_iterator firstiter = _types.begin();
334 firstiter != _types.end();
335 ++firstiter) {
336 for (ParticleTypes_t::const_iterator seconditer = firstiter;
337 seconditer != _types.end();
338 ++seconditer) {
339 if (seconditer == firstiter)
340 continue;
[e1fe7e]341 LOG(3, "DEBUG: Looking for (" << *firstiter << "," << *seconditer << ") in all args.");
[df350c]342
343 // search the right one in _args (we might allow switching places of
344 // firstiter and seconditer, as distance is symmetric).
345 ListArguments_t::iterator iter = availableList.begin();
[31a2be]346 while (iter != availableList.end()) {
[e1fe7e]347 LOG(4, "DEBUG: Current args is " << *iter << ".");
[df350c]348 if ((iter->types.first == *firstiter)
349 && (iter->types.second == *seconditer)) {
[e1fe7e]350 allargs.push_back( *iter );
[31a2be]351 iter = availableList.erase(iter);
352 LOG(4, "DEBUG: Accepted argument.");
353 } else if ((iter->types.first == *seconditer)
[df350c]354 && (iter->types.second == *firstiter)) {
[e1fe7e]355 allargs.push_back( *iter );
[51e0e3]356 iter = availableList.erase(iter);
357 LOG(4, "DEBUG: Accepted (flipped) argument.");
358 } else {
359 ++iter;
360 LOG(4, "DEBUG: Rejected argument.");
[df350c]361 }
362 }
363 }
364 }
[e1fe7e]365 LOG(2, "DEBUG: Final list of args is " << allargs << ".");
366
367 // first, we bring together tuples of distances that belong together
368 FunctionModel::list_of_arguments_t singlelist_allargs;
369 singlelist_allargs.push_back(allargs);
370 FunctionModel::list_of_arguments_t sortedargs =
[e60558]371 reorderArgumentsByParticleTypes(singlelist_allargs, _graph, _types, _bindingmodel);
[e1fe7e]372 ASSERT( sortedargs.size() == (size_t)1,
373 "Extractors::filterArgumentsByParticleTypes() - reordering did not generate a single list.");
374 // then we split up the tuples of arguments and place each into single list
375 FunctionModel::list_of_arguments_t returnargs;
376 FunctionModel::arguments_t::const_iterator argiter = sortedargs.begin()->begin();
377 const size_t num_types = _types.size();
378 const size_t args_per_tuple = num_types * (num_types-1) / 2;
379 while (argiter != sortedargs.begin()->end()) {
380 FunctionModel::arguments_t currenttuple(args_per_tuple);
381 const FunctionModel::arguments_t::const_iterator startiter = argiter;
382 std::advance(argiter, args_per_tuple);
383#ifndef NDEBUG
384 FunctionModel::arguments_t::const_iterator endoutiter =
385#endif
386 std::copy(startiter, argiter, currenttuple.begin());
387 ASSERT( endoutiter == currenttuple.end(),
388 "Extractors::filterArgumentsByParticleTypes() - currenttuple not initialized to right size.");
389 returnargs.push_back(currenttuple);
390 }
391
392 LOG(2, "DEBUG: We have generated " << returnargs.size() << " tuples of distances.");
[df350c]393
394 return returnargs;
395}
[9897ee9]396
397
398FunctionModel::arguments_t Extractors::combineArguments(
399 const FunctionModel::arguments_t &firstargs,
400 const FunctionModel::arguments_t &secondargs)
401{
[cf4905]402 FunctionModel::arguments_t args = concatenateArguments(firstargs, secondargs);
[64bdfd]403 std::sort(args.begin(), args.end(),
404 boost::bind(&argument_t::operator<, _1, _2));
405 FunctionModel::arguments_t::iterator iter =
406 std::unique(args.begin(), args.end(),
407 boost::bind(&argument_t::operator==, _1, _2));
408 args.erase(iter, args.end());
[9897ee9]409 return args;
410}
[64bdfd]411
[cf4905]412FunctionModel::arguments_t Extractors::concatenateArguments(
413 const FunctionModel::arguments_t &firstargs,
414 const FunctionModel::arguments_t &secondargs)
415{
416 FunctionModel::arguments_t args(firstargs);
417 args.insert(args.end(), secondargs.begin(), secondargs.end());
418 return args;
419}
420
[e1fe7e]421FunctionModel::list_of_arguments_t Extractors::concatenateListOfArguments(
422 const FunctionModel::list_of_arguments_t &firstlistargs,
423 const FunctionModel::list_of_arguments_t &secondlistargs)
424{
425 FunctionModel::list_of_arguments_t listargs(firstlistargs);
426 listargs.insert(listargs.end(), secondlistargs.begin(), secondlistargs.end());
427 return listargs;
428}
Note: See TracBrowser for help on using the repository browser.