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