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