| 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 | * CompoundPotential.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: May 8, 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 "Potentials/CompoundPotential.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include <algorithm> | 
|---|
| 41 | #include <boost/bind.hpp> | 
|---|
| 42 | #include <boost/foreach.hpp> | 
|---|
| 43 | #include <boost/lambda/lambda.hpp> | 
|---|
| 44 | #include <iterator> | 
|---|
| 45 | #include <numeric> | 
|---|
| 46 |  | 
|---|
| 47 | #include "CodePatterns/Assert.hpp" | 
|---|
| 48 | #include "CodePatterns/Log.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | #include "Element/element.hpp" | 
|---|
| 51 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
| 52 | #include "Fragmentation/Summation/SetValues/Fragment.hpp" | 
|---|
| 53 | #include "FunctionApproximation/Extractors.hpp" | 
|---|
| 54 | #include "Potentials/EmpiricalPotential.hpp" | 
|---|
| 55 | #include "Potentials/helpers.hpp" | 
|---|
| 56 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| 57 |  | 
|---|
| 58 |  | 
|---|
| 59 | CompoundPotential::CompoundPotential(const HomologyGraph &graph) | 
|---|
| 60 | { | 
|---|
| 61 | LOG(1, "INFO: Creating CompoundPotential for graph " << graph << "."); | 
|---|
| 62 | // look though graph and place all matching FunctionModel's in | 
|---|
| 63 | // PotentialRegistry in models | 
|---|
| 64 | PotentialRegistry::const_iterator potentialiter = | 
|---|
| 65 | PotentialRegistry::getInstance().getBeginIter(); | 
|---|
| 66 | while (potentialiter != PotentialRegistry::getInstance().getEndIter()) { | 
|---|
| 67 | // get model and types | 
|---|
| 68 | EmpiricalPotential * const potential = potentialiter->second; | 
|---|
| 69 | const SerializablePotential::ParticleTypes_t &types = | 
|---|
| 70 | potential->getParticleTypes(); | 
|---|
| 71 |  | 
|---|
| 72 | // create charges | 
|---|
| 73 | Fragment::charges_t charges; | 
|---|
| 74 | charges.resize(types.size()); | 
|---|
| 75 | std::transform(types.begin(), types.end(), | 
|---|
| 76 | charges.begin(), boost::lambda::_1); | 
|---|
| 77 | // convert into count map | 
|---|
| 78 | Extractors::elementcounts_t counts_per_charge = | 
|---|
| 79 | Extractors::_detail::getElementCounts(charges); | 
|---|
| 80 | //    ASSERT( !counts_per_charge.empty(), | 
|---|
| 81 | //        "getFirstGraphwithSpecifiedElements() - charge counts are empty?"); | 
|---|
| 82 | LOG(2, "DEBUG: counts_per_charge is " << counts_per_charge << "."); | 
|---|
| 83 |  | 
|---|
| 84 | // check whether graph contains suitable types | 
|---|
| 85 | Extractors::elementcounts_t::const_iterator countiter = counts_per_charge.begin(); | 
|---|
| 86 | for (; countiter != counts_per_charge.end(); ++countiter) | 
|---|
| 87 | if (!graph.hasGreaterEqualTimesAtomicNumber( | 
|---|
| 88 | static_cast<size_t>(countiter->first), | 
|---|
| 89 | static_cast<size_t>(countiter->second)) | 
|---|
| 90 | ) | 
|---|
| 91 | break; | 
|---|
| 92 | // if we have a match for every count, store model | 
|---|
| 93 | if( countiter == counts_per_charge.end()) { | 
|---|
| 94 | LOG(1, "INFO: Potential " << potentialiter->first << " matches with fragment."); | 
|---|
| 95 | models.push_back(static_cast<FunctionModel*>(potential)); | 
|---|
| 96 | particletypes_per_model.push_back(types); | 
|---|
| 97 | } | 
|---|
| 98 | ++potentialiter; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | // check that models and particletypes_per_model match | 
|---|
| 102 | ASSERT( models.size() == particletypes_per_model.size(), | 
|---|
| 103 | "CompoundPotential::CompoundPotential() - particletypes not stored for all models?"); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | CompoundPotential::~CompoundPotential() | 
|---|
| 107 | { | 
|---|
| 108 | // clear all models and internally stored particletypes | 
|---|
| 109 | models.clear(); | 
|---|
| 110 | particletypes_per_model.clear(); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void CompoundPotential::setParameters(const parameters_t &_params) | 
|---|
| 114 | { | 
|---|
| 115 | size_t dim = _params.size(); | 
|---|
| 116 | parameters_t::const_iterator iter = _params.begin(); | 
|---|
| 117 | BOOST_FOREACH( FunctionModel* model, models) { | 
|---|
| 118 | const size_t model_dim = model->getParameterDimension(); | 
|---|
| 119 | if (dim > 0) { | 
|---|
| 120 | parameters_t subparams; | 
|---|
| 121 | if (dim < model_dim) { | 
|---|
| 122 | std::copy(iter, iter+dim, std::back_inserter(subparams)); | 
|---|
| 123 | iter += dim; | 
|---|
| 124 | dim = 0; | 
|---|
| 125 | } else { | 
|---|
| 126 | std::copy(iter, iter+model_dim, std::back_inserter(subparams)); | 
|---|
| 127 | iter += model_dim; | 
|---|
| 128 | dim -= model_dim; | 
|---|
| 129 | } | 
|---|
| 130 | model->setParameters(subparams); | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | #ifndef NDEBUG | 
|---|
| 135 | parameters_t check_params(getParameters()); | 
|---|
| 136 | check_params.resize(_params.size()); // truncate to same size | 
|---|
| 137 | ASSERT( check_params == _params, | 
|---|
| 138 | "CompoundPotential::setParameters() - failed, mismatch in to be set " | 
|---|
| 139 | +toString(_params)+" and set "+toString(check_params)+" params."); | 
|---|
| 140 | #endif | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | CompoundPotential::parameters_t CompoundPotential::getParameters() const | 
|---|
| 144 | { | 
|---|
| 145 | const size_t dimension = getParameterDimension(); | 
|---|
| 146 | CompoundPotential::parameters_t parameters(dimension); | 
|---|
| 147 | CompoundPotential::parameters_t::iterator iter = parameters.begin(); | 
|---|
| 148 | BOOST_FOREACH( const FunctionModel* model, models) { | 
|---|
| 149 | const CompoundPotential::parameters_t ¶ms = model->getParameters(); | 
|---|
| 150 | ASSERT( iter != parameters.end(), | 
|---|
| 151 | "CompoundPotential::getParameters() - iter already at end."); | 
|---|
| 152 | iter = std::copy(params.begin(), params.end(), iter); | 
|---|
| 153 | } | 
|---|
| 154 | ASSERT( iter == parameters.end(), | 
|---|
| 155 | "CompoundPotential::getParameters() - iter not at end."); | 
|---|
| 156 | return parameters; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | void CompoundPotential::setParametersToRandomInitialValues(const TrainingData &data) | 
|---|
| 160 | { | 
|---|
| 161 | std::for_each(models.begin(), models.end(), | 
|---|
| 162 | boost::bind(&FunctionModel::setParametersToRandomInitialValues, _1, boost::cref(data)) | 
|---|
| 163 | ); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | size_t CompoundPotential::getParameterDimension() const | 
|---|
| 167 | { | 
|---|
| 168 | std::vector<size_t> dimensions(models.size(), 0); | 
|---|
| 169 | std::transform(models.begin(), models.end(), dimensions.begin(), | 
|---|
| 170 | boost::bind(&FunctionModel::getParameterDimension, _1)); | 
|---|
| 171 | return std::accumulate(dimensions.begin(), dimensions.end(), 0, std::plus<size_t>()); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | void CompoundPotential::setTriplefunction(triplefunction_t &_triplefunction) | 
|---|
| 175 | { | 
|---|
| 176 | std::for_each(models.begin(), models.end(), | 
|---|
| 177 | boost::bind(&FunctionModel::setTriplefunction, _1, boost::ref(_triplefunction)) | 
|---|
| 178 | ); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | bool CompoundPotential::areValidArguments( | 
|---|
| 182 | const SerializablePotential::ParticleTypes_t &_types, | 
|---|
| 183 | const arguments_t &args) const | 
|---|
| 184 | { | 
|---|
| 185 | // /this function does much the same as Extractors::reorderArgumentsByParticleTypes() | 
|---|
| 186 | typedef std::list< argument_t > ListArguments_t; | 
|---|
| 187 | ListArguments_t availableList(args.begin(), args.end()); | 
|---|
| 188 |  | 
|---|
| 189 | /// basically, we have two choose any two pairs out of types but only those | 
|---|
| 190 | /// where the first is less than the letter. Hence, we start the second | 
|---|
| 191 | /// iterator at the current position of the first one and skip the equal case. | 
|---|
| 192 | for (SerializablePotential::ParticleTypes_t::const_iterator firstiter = _types.begin(); | 
|---|
| 193 | firstiter != _types.end(); | 
|---|
| 194 | ++firstiter) { | 
|---|
| 195 | for (SerializablePotential::ParticleTypes_t::const_iterator seconditer = firstiter; | 
|---|
| 196 | seconditer != _types.end(); | 
|---|
| 197 | ++seconditer) { | 
|---|
| 198 | if (seconditer == firstiter) | 
|---|
| 199 | continue; | 
|---|
| 200 |  | 
|---|
| 201 | // search the right one in _args (we might allow switching places of | 
|---|
| 202 | // firstiter and seconditer, as distance is symmetric). | 
|---|
| 203 | // we remove the matching argument to make sure we don't pick it twice | 
|---|
| 204 | ListArguments_t::iterator iter = availableList.begin(); | 
|---|
| 205 | for (;iter != availableList.end(); ++iter) { | 
|---|
| 206 | LOG(3, "DEBUG: Current args is " << *iter << "."); | 
|---|
| 207 | if ((iter->types.first == *firstiter) | 
|---|
| 208 | && (iter->types.second == *seconditer)) { | 
|---|
| 209 | availableList.erase(iter); | 
|---|
| 210 | break; | 
|---|
| 211 | } | 
|---|
| 212 | else if ((iter->types.first == *seconditer) | 
|---|
| 213 | && (iter->types.second == *firstiter)) { | 
|---|
| 214 | availableList.erase(iter); | 
|---|
| 215 | break; | 
|---|
| 216 | } | 
|---|
| 217 | } | 
|---|
| 218 | if ( iter == availableList.end()) | 
|---|
| 219 | return false; | 
|---|
| 220 | } | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | return true; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | CompoundPotential::arguments_by_model_t CompoundPotential::splitUpArgumentsByModelsFilter( | 
|---|
| 227 | const arguments_t &arguments) const | 
|---|
| 228 | { | 
|---|
| 229 | arguments_by_model_t partial_args; | 
|---|
| 230 | // go through each model and have it filter out its arguments, this already | 
|---|
| 231 | // returns a list of tuples associated with the specific model | 
|---|
| 232 | for(models_t::const_iterator modeliter = models.begin(); | 
|---|
| 233 | modeliter != models.end(); ++modeliter) { | 
|---|
| 234 | FunctionModel::filter_t filterfunction = (*modeliter)->getSpecificFilter(); | 
|---|
| 235 | list_of_arguments_t tempargs = filterfunction(arguments); | 
|---|
| 236 | // then split up all the bunches, too. | 
|---|
| 237 | for (list_of_arguments_t::const_iterator argiter = tempargs.begin(); | 
|---|
| 238 | argiter != tempargs.end(); ++argiter) { | 
|---|
| 239 | const arguments_t &args = *argiter; | 
|---|
| 240 | partial_args.push_back( | 
|---|
| 241 | std::make_pair( | 
|---|
| 242 | *modeliter, | 
|---|
| 243 | args | 
|---|
| 244 | ) | 
|---|
| 245 | ); | 
|---|
| 246 | } | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | return partial_args; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | CompoundPotential::arguments_by_model_t CompoundPotential::splitUpArgumentsByModels( | 
|---|
| 253 | const list_of_arguments_t &listarguments) const | 
|---|
| 254 | { | 
|---|
| 255 | arguments_by_model_t partial_args; | 
|---|
| 256 | particletypes_per_model_t::const_iterator typesiter = particletypes_per_model.begin(); | 
|---|
| 257 | models_t::const_iterator modeliter = models.begin(); | 
|---|
| 258 |  | 
|---|
| 259 | /// add constant model (which is always first model) with empty args if present | 
|---|
| 260 | if (typesiter->empty()) { | 
|---|
| 261 | partial_args.push_back( | 
|---|
| 262 | std::pair<FunctionModel *, arguments_t>(*modeliter, arguments_t()) | 
|---|
| 263 | ); | 
|---|
| 264 | ++modeliter; | 
|---|
| 265 | ++typesiter; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | // then check other models | 
|---|
| 269 | /// we only have to check whether the current model still matches or whether | 
|---|
| 270 | /// have to use the next model. | 
|---|
| 271 | for (list_of_arguments_t::const_iterator argiter = listarguments.begin(); | 
|---|
| 272 | argiter != listarguments.end(); ++argiter) { | 
|---|
| 273 | const arguments_t &arguments = *argiter; | 
|---|
| 274 | if (typesiter+1 != particletypes_per_model.end()) { | 
|---|
| 275 | // check whether next argument bunch is for same model or different one | 
|---|
| 276 | // we extract both partial_arguments, if the latter fits, we take the latter. | 
|---|
| 277 | const SerializablePotential::ParticleTypes_t &types = *typesiter; | 
|---|
| 278 | const SerializablePotential::ParticleTypes_t &nexttypes = *(typesiter+1); | 
|---|
| 279 |  | 
|---|
| 280 | // we always expect N(N-1)/2 distances for N particle types | 
|---|
| 281 | // check first from sizes alone | 
|---|
| 282 | const size_t tuplesize = types.size()*(types.size()-1)/2; | 
|---|
| 283 | const size_t nexttuplesize = nexttypes.size()*(nexttypes.size()-1)/2; | 
|---|
| 284 | if ((tuplesize != nexttuplesize)) { | 
|---|
| 285 | if ((arguments.size() == tuplesize) &&  areValidArguments(types, arguments)) { | 
|---|
| 286 | // only former still matches, don't increment | 
|---|
| 287 | partial_args.push_back( | 
|---|
| 288 | std::make_pair(*modeliter, arguments) | 
|---|
| 289 | ); | 
|---|
| 290 | } else if ((arguments.size() == nexttuplesize) &&  areValidArguments(nexttypes, arguments)) { | 
|---|
| 291 | // latter matches, increment | 
|---|
| 292 | ++typesiter; | 
|---|
| 293 | partial_args.push_back( | 
|---|
| 294 | std::make_pair(*(++modeliter), arguments) | 
|---|
| 295 | ); | 
|---|
| 296 | } else { | 
|---|
| 297 | ASSERT(0, | 
|---|
| 298 | "CompoundPotential::splitUpArgumentsByModels() - neither this model nor next model match (size) with current tuple."); | 
|---|
| 299 | } | 
|---|
| 300 | } else { // same size, now we have to check the types individually | 
|---|
| 301 | size_t encodeValidity = 0; | 
|---|
| 302 | encodeValidity += 1*areValidArguments(types, arguments); | 
|---|
| 303 | encodeValidity += 2*areValidArguments(nexttypes, arguments); | 
|---|
| 304 |  | 
|---|
| 305 | switch (encodeValidity) { | 
|---|
| 306 | case 1: | 
|---|
| 307 | // only former still matches, don't increment | 
|---|
| 308 | partial_args.push_back( | 
|---|
| 309 | std::make_pair(*modeliter, arguments) | 
|---|
| 310 | ); | 
|---|
| 311 | break; | 
|---|
| 312 | case 2: | 
|---|
| 313 | ++typesiter; | 
|---|
| 314 | partial_args.push_back( | 
|---|
| 315 | std::make_pair(*(++modeliter), arguments) | 
|---|
| 316 | ); | 
|---|
| 317 | break; | 
|---|
| 318 | case 0: | 
|---|
| 319 | case 3: | 
|---|
| 320 | default: | 
|---|
| 321 | ASSERT(0, | 
|---|
| 322 | "CompoundPotential::splitUpArgumentsByModels() - neither this model nor next model match (type) with current tuple."); | 
|---|
| 323 | break; | 
|---|
| 324 | } | 
|---|
| 325 | } | 
|---|
| 326 | } else { | 
|---|
| 327 | const SerializablePotential::ParticleTypes_t &types = *typesiter; | 
|---|
| 328 | if (areValidArguments(types, arguments)) { | 
|---|
| 329 | // only former matches, don't increment | 
|---|
| 330 | partial_args.push_back( | 
|---|
| 331 | std::make_pair(*modeliter, arguments) | 
|---|
| 332 | ); | 
|---|
| 333 | } else { | 
|---|
| 334 | ASSERT(0, | 
|---|
| 335 | "CompoundPotential::splitUpArgumentsByModels() - last model does not match with current tuple."); | 
|---|
| 336 | } | 
|---|
| 337 | } | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | return partial_args; | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | CompoundPotential::results_t CompoundPotential::operator()( | 
|---|
| 344 | const list_of_arguments_t &listarguments) const | 
|---|
| 345 | { | 
|---|
| 346 | /// first, we have to split up the given arguments | 
|---|
| 347 | arguments_by_model_t partial_args = | 
|---|
| 348 | splitUpArgumentsByModels(listarguments); | 
|---|
| 349 | // print split up argument list for debugging | 
|---|
| 350 | if (DoLog(4)) { | 
|---|
| 351 | LOG(4, "Arguments by model are: "); | 
|---|
| 352 | for(arguments_by_model_t::const_iterator iter = partial_args.begin(); | 
|---|
| 353 | iter != partial_args.end(); ++iter) { | 
|---|
| 354 | LOG(4, "\tModel with " << iter->first->getParameterDimension() | 
|---|
| 355 | << " parameters " << iter->first->getParameters() | 
|---|
| 356 | << " and arguments: " << iter->second); | 
|---|
| 357 | } | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | /// then, with each bunch of arguments, we call the specific model | 
|---|
| 361 | results_t results(1,0.); | 
|---|
| 362 | std::vector<results_t> partial_results; | 
|---|
| 363 | for(arguments_by_model_t::const_iterator iter = partial_args.begin(); | 
|---|
| 364 | iter != partial_args.end(); ++iter) { | 
|---|
| 365 | partial_results.push_back( | 
|---|
| 366 | (*iter->first)( | 
|---|
| 367 | FunctionModel::list_of_arguments_t(1, iter->second)) | 
|---|
| 368 | ); | 
|---|
| 369 | } | 
|---|
| 370 | // print partial results for debugging | 
|---|
| 371 | if (DoLog(4)) { | 
|---|
| 372 | std::stringstream output; | 
|---|
| 373 | output << "Partial results are: "; | 
|---|
| 374 | std::for_each(partial_results.begin(), partial_results.end(), | 
|---|
| 375 | output << (boost::lambda::_1)[0] << "\t"); | 
|---|
| 376 | LOG(4, output.str()); | 
|---|
| 377 | } | 
|---|
| 378 |  | 
|---|
| 379 | /// Finally, sum up all results and return | 
|---|
| 380 | std::for_each(partial_results.begin(), partial_results.end(), | 
|---|
| 381 | results[0] += (boost::lambda::_1)[0]); | 
|---|
| 382 | return results; | 
|---|
| 383 | } | 
|---|
| 384 |  | 
|---|
| 385 | CompoundPotential::results_t CompoundPotential::parameter_derivative( | 
|---|
| 386 | const list_of_arguments_t &listarguments, | 
|---|
| 387 | const size_t index) const | 
|---|
| 388 | { | 
|---|
| 389 | // first, we have to split up the given arguments | 
|---|
| 390 | arguments_by_model_t partial_args = | 
|---|
| 391 | splitUpArgumentsByModels(listarguments); | 
|---|
| 392 | // then, with each bunch of arguments, we call the specific model | 
|---|
| 393 | // get parameter dimensions per model | 
|---|
| 394 | std::vector<size_t> dimensions(models.size(), 0); | 
|---|
| 395 | std::transform(models.begin(), models.end(), dimensions.begin(), | 
|---|
| 396 | boost::bind(&FunctionModel::getParameterDimension, _1)); | 
|---|
| 397 |  | 
|---|
| 398 | // convert to index end+1 per model | 
|---|
| 399 | std::partial_sum(dimensions.begin(), dimensions.end(), dimensions.begin()); | 
|---|
| 400 |  | 
|---|
| 401 | // look for first value greater than index | 
|---|
| 402 | std::vector<size_t>::const_iterator iter = | 
|---|
| 403 | std::upper_bound(dimensions.begin(), dimensions.end(), index); | 
|---|
| 404 |  | 
|---|
| 405 | // step forward to same model | 
|---|
| 406 | models_t::const_iterator modeliter = models.begin(); | 
|---|
| 407 | std::advance(modeliter, | 
|---|
| 408 | std::distance(const_cast<const std::vector<size_t> &>(dimensions).begin(), iter) ); | 
|---|
| 409 |  | 
|---|
| 410 | CompoundPotential::results_t returnresults; | 
|---|
| 411 | for(arguments_by_model_t::const_iterator argiter = partial_args.begin(); | 
|---|
| 412 | argiter != partial_args.end(); ++argiter) { | 
|---|
| 413 | const FunctionModel *model = argiter->first; | 
|---|
| 414 |  | 
|---|
| 415 | // for every matching model evaluate | 
|---|
| 416 | if (model == *modeliter) { | 
|---|
| 417 | // evaluate with correct relative index and return | 
|---|
| 418 | const size_t indexbase = (iter == dimensions.begin()) ? 0 : *(iter-1); | 
|---|
| 419 | CompoundPotential::results_t results = | 
|---|
| 420 | model->parameter_derivative( | 
|---|
| 421 | FunctionModel::list_of_arguments_t(1, argiter->second), index-indexbase); | 
|---|
| 422 |  | 
|---|
| 423 | // either set results or add | 
|---|
| 424 | if (returnresults.empty()) | 
|---|
| 425 | returnresults = results; | 
|---|
| 426 | else | 
|---|
| 427 | std::transform( | 
|---|
| 428 | results.begin(), results.end(), | 
|---|
| 429 | returnresults.begin(), | 
|---|
| 430 | returnresults.begin(), | 
|---|
| 431 | std::plus<FunctionModel::result_t>()); | 
|---|
| 432 | } | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 | return returnresults; | 
|---|
| 436 | } | 
|---|
| 437 |  | 
|---|
| 438 | bool CompoundPotential::isBoxConstraint() const | 
|---|
| 439 | { | 
|---|
| 440 | std::vector<bool> constraints(models.size(), 0); | 
|---|
| 441 | std::transform(models.begin(), models.end(), constraints.begin(), | 
|---|
| 442 | boost::bind(&FunctionModel::getParameterDimension, _1)); | 
|---|
| 443 | return std::accumulate(constraints.begin(), constraints.end(), true, | 
|---|
| 444 | std::logical_and<bool>()); | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | CompoundPotential::parameters_t CompoundPotential::getLowerBoxConstraints() const | 
|---|
| 448 | { | 
|---|
| 449 | const size_t dimension = getParameterDimension(); | 
|---|
| 450 | CompoundPotential::parameters_t constraints(dimension); | 
|---|
| 451 | CompoundPotential::parameters_t::iterator iter = constraints.begin(); | 
|---|
| 452 | BOOST_FOREACH( FunctionModel* model, models) { | 
|---|
| 453 | const CompoundPotential::parameters_t params = model->getLowerBoxConstraints(); | 
|---|
| 454 | ASSERT( iter != constraints.end(), | 
|---|
| 455 | "CompoundPotential::getLowerBoxConstraints() - iter already at end."); | 
|---|
| 456 | iter = std::copy(params.begin(), params.end(), iter); | 
|---|
| 457 | } | 
|---|
| 458 | ASSERT( iter == constraints.end(), | 
|---|
| 459 | "CompoundPotential::getLowerBoxConstraints() - iter not at end."); | 
|---|
| 460 | return constraints; | 
|---|
| 461 | } | 
|---|
| 462 |  | 
|---|
| 463 | CompoundPotential::parameters_t CompoundPotential::getUpperBoxConstraints() const | 
|---|
| 464 | { | 
|---|
| 465 | const size_t dimension = getParameterDimension(); | 
|---|
| 466 | CompoundPotential::parameters_t constraints(dimension); | 
|---|
| 467 | CompoundPotential::parameters_t::iterator iter = constraints.begin(); | 
|---|
| 468 | BOOST_FOREACH( FunctionModel* model, models) { | 
|---|
| 469 | const CompoundPotential::parameters_t params = model->getUpperBoxConstraints(); | 
|---|
| 470 | ASSERT( iter != constraints.end(), | 
|---|
| 471 | "CompoundPotential::getUpperBoxConstraints() - iter already at end."); | 
|---|
| 472 | iter = std::copy(params.begin(), params.end(), iter); | 
|---|
| 473 | } | 
|---|
| 474 | ASSERT( iter == constraints.end(), | 
|---|
| 475 | "CompoundPotential::getUpperBoxConstraints() - iter not at end."); | 
|---|
| 476 | return constraints; | 
|---|
| 477 | } | 
|---|
| 478 |  | 
|---|
| 479 | FunctionModel::filter_t CompoundPotential::getSpecificFilter() const | 
|---|
| 480 | { | 
|---|
| 481 | // we must concatenate all filtered arguments here | 
|---|
| 482 | // create initial returnfunction | 
|---|
| 483 | FunctionModel::filter_t returnfunction = | 
|---|
| 484 | boost::bind(&Helpers::returnEmptyListArguments); | 
|---|
| 485 |  | 
|---|
| 486 | // every following fragments combines its arguments with the initial function | 
|---|
| 487 | for (models_t::const_iterator modeliter = models.begin(); | 
|---|
| 488 | modeliter != models.end(); ++modeliter) { | 
|---|
| 489 | returnfunction = | 
|---|
| 490 | boost::bind(&Extractors::concatenateListOfArguments, | 
|---|
| 491 | boost::bind(returnfunction, _1), | 
|---|
| 492 | boost::bind((*modeliter)->getSpecificFilter(), _1) | 
|---|
| 493 | ); | 
|---|
| 494 | } | 
|---|
| 495 | return returnfunction; | 
|---|
| 496 | } | 
|---|
| 497 |  | 
|---|
| 498 | size_t CompoundPotential::getSpecificArgumentCount() const | 
|---|
| 499 | { | 
|---|
| 500 | std::vector<size_t> argument_counts(models.size(), 0); | 
|---|
| 501 | std::transform(models.begin(), models.end(), argument_counts.begin(), | 
|---|
| 502 | boost::bind(&FunctionModel::getSpecificArgumentCount, _1)); | 
|---|
| 503 | return std::accumulate(argument_counts.begin(), argument_counts.end(), 0, | 
|---|
| 504 | std::plus<size_t>()); | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|