source: src/FunctionApproximation/Extractors.cpp@ 11bab4

Fix_FitPotential_needs_atomicnumbers
Last change on this file since 11bab4 was 11bab4, checked in by Frederik Heber <heber@…>, 9 years ago

Extractors now need a subgraph and the set of particle types.

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