| 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
 | 
|---|
| 231 |   for(models_t::const_iterator modeliter = models.begin();
 | 
|---|
| 232 |       modeliter != models.end(); ++modeliter) {
 | 
|---|
| 233 |     FunctionModel::filter_t filterfunction = (*modeliter)->getSpecificFilter();
 | 
|---|
| 234 |     arguments_t tempargs = filterfunction(arguments);
 | 
|---|
| 235 |     // then split up all the bunches, too.
 | 
|---|
| 236 |     arguments_t::const_iterator advanceiter = tempargs.begin();
 | 
|---|
| 237 |     for (arguments_t::const_iterator argiter = tempargs.begin();
 | 
|---|
| 238 |         argiter != tempargs.end(); argiter = advanceiter) {
 | 
|---|
| 239 |       advanceiter += (*modeliter)->getSpecificArgumentCount();
 | 
|---|
| 240 |       partial_args.push_back(
 | 
|---|
| 241 |           std::make_pair(
 | 
|---|
| 242 |               *modeliter,
 | 
|---|
| 243 |               arguments_t(argiter, advanceiter)
 | 
|---|
| 244 |             )
 | 
|---|
| 245 |           );
 | 
|---|
| 246 |     }
 | 
|---|
| 247 |   }
 | 
|---|
| 248 | 
 | 
|---|
| 249 |   return partial_args;
 | 
|---|
| 250 | }
 | 
|---|
| 251 | 
 | 
|---|
| 252 | CompoundPotential::arguments_by_model_t CompoundPotential::splitUpArgumentsByModels(
 | 
|---|
| 253 |     const arguments_t &arguments) const
 | 
|---|
| 254 | {
 | 
|---|
| 255 |   arguments_by_model_t partial_args;
 | 
|---|
| 256 |   arguments_t::const_iterator argiter = arguments.begin();
 | 
|---|
| 257 |   particletypes_per_model_t::const_iterator typesiter = particletypes_per_model.begin();
 | 
|---|
| 258 |   models_t::const_iterator modeliter = models.begin();
 | 
|---|
| 259 | 
 | 
|---|
| 260 |   // add constant model (which is always first model) with empty args if present
 | 
|---|
| 261 |   if (typesiter->empty()) {
 | 
|---|
| 262 |     partial_args.push_back(
 | 
|---|
| 263 |         std::pair<FunctionModel *, arguments_t>(*modeliter, arguments_t())
 | 
|---|
| 264 |         );
 | 
|---|
| 265 |     ++modeliter;
 | 
|---|
| 266 |     ++typesiter;
 | 
|---|
| 267 |   }
 | 
|---|
| 268 |   // then check other models
 | 
|---|
| 269 |   while (argiter != arguments.end()) {
 | 
|---|
| 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 |       arguments_t::const_iterator enditer = argiter+(types.size()*(types.size()-1)/2);
 | 
|---|
| 278 |       arguments_t::const_iterator nextenditer = argiter+(nexttypes.size()*(nexttypes.size()-1)/2);
 | 
|---|
| 279 |       arguments_t args(argiter, enditer);
 | 
|---|
| 280 |       arguments_t nextargs(argiter, nextenditer);
 | 
|---|
| 281 |       if (types.size() < nexttypes.size()) {
 | 
|---|
| 282 |         if (areValidArguments(nexttypes, nextargs)) {
 | 
|---|
| 283 |           // latter matches, increment
 | 
|---|
| 284 |           ++typesiter;
 | 
|---|
| 285 |           partial_args.push_back(
 | 
|---|
| 286 |               std::make_pair(*(++modeliter), arguments_t(argiter, nextenditer))
 | 
|---|
| 287 |               );
 | 
|---|
| 288 |           argiter = nextenditer;
 | 
|---|
| 289 |         } else if (areValidArguments(types, args)) {
 | 
|---|
| 290 |           // only former matches, don't increment
 | 
|---|
| 291 |           partial_args.push_back(
 | 
|---|
| 292 |               std::make_pair(*modeliter, arguments_t(argiter, enditer))
 | 
|---|
| 293 |               );
 | 
|---|
| 294 |           argiter = enditer;
 | 
|---|
| 295 |         } else
 | 
|---|
| 296 |           ASSERT(0,
 | 
|---|
| 297 |               "CompoundPotential::splitUpArgumentsByModels() - neither type matches its argument bunch.");
 | 
|---|
| 298 |       } else {
 | 
|---|
| 299 |         if (areValidArguments(types, args)) {
 | 
|---|
| 300 |           // only former matches, don't increment
 | 
|---|
| 301 |           partial_args.push_back(
 | 
|---|
| 302 |               std::make_pair(*modeliter, arguments_t(argiter, enditer))
 | 
|---|
| 303 |               );
 | 
|---|
| 304 |           argiter = enditer;
 | 
|---|
| 305 |         } else if (areValidArguments(nexttypes, nextargs)) {
 | 
|---|
| 306 |           // latter matches, increment
 | 
|---|
| 307 |           ++typesiter;
 | 
|---|
| 308 |           partial_args.push_back(
 | 
|---|
| 309 |               std::make_pair(*(++modeliter), arguments_t(argiter, nextenditer))
 | 
|---|
| 310 |               );
 | 
|---|
| 311 |           argiter = nextenditer;
 | 
|---|
| 312 |         }
 | 
|---|
| 313 |       }
 | 
|---|
| 314 |     } else {
 | 
|---|
| 315 |       const SerializablePotential::ParticleTypes_t &types = *typesiter;
 | 
|---|
| 316 |       // we always expect N(N-1)/2 distances for N particle types
 | 
|---|
| 317 |       arguments_t::const_iterator enditer = argiter+(types.size()*(types.size()-1)/2);
 | 
|---|
| 318 |       partial_args.push_back(
 | 
|---|
| 319 |           std::make_pair(*modeliter, arguments_t(argiter, enditer))
 | 
|---|
| 320 |           );
 | 
|---|
| 321 |       argiter = enditer;
 | 
|---|
| 322 |     }
 | 
|---|
| 323 |   }
 | 
|---|
| 324 | 
 | 
|---|
| 325 |   return partial_args;
 | 
|---|
| 326 | }
 | 
|---|
| 327 | 
 | 
|---|
| 328 | CompoundPotential::results_t CompoundPotential::operator()(const arguments_t &arguments) const
 | 
|---|
| 329 | {
 | 
|---|
| 330 |   /// first, we have to split up the given arguments
 | 
|---|
| 331 |   arguments_by_model_t partial_args =
 | 
|---|
| 332 |       splitUpArgumentsByModels(arguments);
 | 
|---|
| 333 |   // print split up argument list for debugging
 | 
|---|
| 334 |   if (DoLog(4)) {
 | 
|---|
| 335 |     LOG(4, "Arguments by model are: ");
 | 
|---|
| 336 |     for(arguments_by_model_t::const_iterator iter = partial_args.begin();
 | 
|---|
| 337 |         iter != partial_args.end(); ++iter) {
 | 
|---|
| 338 |       LOG(4, "\tModel with " << iter->first->getParameterDimension()
 | 
|---|
| 339 |           << " parameters " << iter->first->getParameters()
 | 
|---|
| 340 |           << " and arguments: " << iter->second);
 | 
|---|
| 341 |     }
 | 
|---|
| 342 |   }
 | 
|---|
| 343 | 
 | 
|---|
| 344 |   /// then, with each bunch of arguments, we call the specific model
 | 
|---|
| 345 |   results_t results(1,0.);
 | 
|---|
| 346 |   std::vector<results_t> partial_results;
 | 
|---|
| 347 |   for(arguments_by_model_t::const_iterator iter = partial_args.begin();
 | 
|---|
| 348 |       iter != partial_args.end(); ++iter) {
 | 
|---|
| 349 |     partial_results.push_back(
 | 
|---|
| 350 |         (*iter->first)(iter->second)
 | 
|---|
| 351 |     );
 | 
|---|
| 352 |   }
 | 
|---|
| 353 |   // print partial results for debugging
 | 
|---|
| 354 |   if (DoLog(4)) {
 | 
|---|
| 355 |     std::stringstream output;
 | 
|---|
| 356 |     output << "Partial results are: ";
 | 
|---|
| 357 |     std::for_each(partial_results.begin(), partial_results.end(),
 | 
|---|
| 358 |         output << (boost::lambda::_1)[0] << "\t");
 | 
|---|
| 359 |     LOG(4, output.str());
 | 
|---|
| 360 |   }
 | 
|---|
| 361 | 
 | 
|---|
| 362 |   /// Finally, sum up all results and return
 | 
|---|
| 363 |   std::for_each(partial_results.begin(), partial_results.end(),
 | 
|---|
| 364 |       results[0] += (boost::lambda::_1)[0]);
 | 
|---|
| 365 |   return results;
 | 
|---|
| 366 | }
 | 
|---|
| 367 | 
 | 
|---|
| 368 | CompoundPotential::results_t CompoundPotential::parameter_derivative(const arguments_t &arguments, const size_t index) const
 | 
|---|
| 369 | {
 | 
|---|
| 370 |   // first, we have to split up the given arguments
 | 
|---|
| 371 |   arguments_by_model_t partial_args =
 | 
|---|
| 372 |       splitUpArgumentsByModels(arguments);
 | 
|---|
| 373 |   // then, with each bunch of arguments, we call the specific model
 | 
|---|
| 374 |   // get parameter dimensions per model
 | 
|---|
| 375 |   std::vector<size_t> dimensions(models.size(), 0);
 | 
|---|
| 376 |   std::transform(models.begin(), models.end(), dimensions.begin(),
 | 
|---|
| 377 |       boost::bind(&FunctionModel::getParameterDimension, _1));
 | 
|---|
| 378 | 
 | 
|---|
| 379 |   // convert to index end+1 per model
 | 
|---|
| 380 |   std::partial_sum(dimensions.begin(), dimensions.end(), dimensions.begin());
 | 
|---|
| 381 | 
 | 
|---|
| 382 |   // look for first value greater than index
 | 
|---|
| 383 |   std::vector<size_t>::const_iterator iter =
 | 
|---|
| 384 |       std::upper_bound(dimensions.begin(), dimensions.end(), index);
 | 
|---|
| 385 | 
 | 
|---|
| 386 |   // step forward to same model
 | 
|---|
| 387 |   models_t::const_iterator modeliter = models.begin();
 | 
|---|
| 388 |   std::advance(modeliter,
 | 
|---|
| 389 |       std::distance(const_cast<const std::vector<size_t> &>(dimensions).begin(), iter) );
 | 
|---|
| 390 | 
 | 
|---|
| 391 |   CompoundPotential::results_t returnresults;
 | 
|---|
| 392 |   for(arguments_by_model_t::const_iterator argiter = partial_args.begin();
 | 
|---|
| 393 |       argiter != partial_args.end(); ++argiter) {
 | 
|---|
| 394 |     const FunctionModel *model = argiter->first;
 | 
|---|
| 395 | 
 | 
|---|
| 396 |     // for every matching model evaluate
 | 
|---|
| 397 |     if (model == *modeliter) {
 | 
|---|
| 398 |       // evaluate with correct relative index and return
 | 
|---|
| 399 |       const size_t indexbase = (iter == dimensions.begin()) ? 0 : *(iter-1);
 | 
|---|
| 400 |       CompoundPotential::results_t results =
 | 
|---|
| 401 |           model->parameter_derivative(argiter->second, index-indexbase);
 | 
|---|
| 402 | 
 | 
|---|
| 403 |       // either set results or add
 | 
|---|
| 404 |       if (returnresults.empty())
 | 
|---|
| 405 |         returnresults = results;
 | 
|---|
| 406 |       else
 | 
|---|
| 407 |         std::transform(
 | 
|---|
| 408 |             results.begin(), results.end(),
 | 
|---|
| 409 |             returnresults.begin(),
 | 
|---|
| 410 |             returnresults.begin(),
 | 
|---|
| 411 |             std::plus<FunctionModel::result_t>());
 | 
|---|
| 412 |     }
 | 
|---|
| 413 |   }
 | 
|---|
| 414 | 
 | 
|---|
| 415 |   return returnresults;
 | 
|---|
| 416 | }
 | 
|---|
| 417 | 
 | 
|---|
| 418 | bool CompoundPotential::isBoxConstraint() const
 | 
|---|
| 419 | {
 | 
|---|
| 420 |   std::vector<bool> constraints(models.size(), 0);
 | 
|---|
| 421 |   std::transform(models.begin(), models.end(), constraints.begin(),
 | 
|---|
| 422 |       boost::bind(&FunctionModel::getParameterDimension, _1));
 | 
|---|
| 423 |   return std::accumulate(constraints.begin(), constraints.end(), true,
 | 
|---|
| 424 |       std::logical_and<bool>());
 | 
|---|
| 425 | }
 | 
|---|
| 426 | 
 | 
|---|
| 427 | CompoundPotential::parameters_t CompoundPotential::getLowerBoxConstraints() const
 | 
|---|
| 428 | {
 | 
|---|
| 429 |   const size_t dimension = getParameterDimension();
 | 
|---|
| 430 |   CompoundPotential::parameters_t constraints(dimension);
 | 
|---|
| 431 |   CompoundPotential::parameters_t::iterator iter = constraints.begin();
 | 
|---|
| 432 |   BOOST_FOREACH( FunctionModel* model, models) {
 | 
|---|
| 433 |     const CompoundPotential::parameters_t params = model->getLowerBoxConstraints();
 | 
|---|
| 434 |     ASSERT( iter != constraints.end(),
 | 
|---|
| 435 |         "CompoundPotential::getLowerBoxConstraints() - iter already at end.");
 | 
|---|
| 436 |     iter = std::copy(params.begin(), params.end(), iter);
 | 
|---|
| 437 |   }
 | 
|---|
| 438 |   ASSERT( iter == constraints.end(),
 | 
|---|
| 439 |       "CompoundPotential::getLowerBoxConstraints() - iter not at end.");
 | 
|---|
| 440 |   return constraints;
 | 
|---|
| 441 | }
 | 
|---|
| 442 | 
 | 
|---|
| 443 | CompoundPotential::parameters_t CompoundPotential::getUpperBoxConstraints() 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->getUpperBoxConstraints();
 | 
|---|
| 450 |     ASSERT( iter != constraints.end(),
 | 
|---|
| 451 |         "CompoundPotential::getUpperBoxConstraints() - iter already at end.");
 | 
|---|
| 452 |     iter = std::copy(params.begin(), params.end(), iter);
 | 
|---|
| 453 |   }
 | 
|---|
| 454 |   ASSERT( iter == constraints.end(),
 | 
|---|
| 455 |       "CompoundPotential::getUpperBoxConstraints() - iter not at end.");
 | 
|---|
| 456 |   return constraints;
 | 
|---|
| 457 | }
 | 
|---|
| 458 | 
 | 
|---|
| 459 | FunctionModel::extractor_t CompoundPotential::getSpecificExtractor() const
 | 
|---|
| 460 | {
 | 
|---|
| 461 |   // create initial returnfunction
 | 
|---|
| 462 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
| 463 |       boost::bind(&Helpers::returnEmptyArguments);
 | 
|---|
| 464 | 
 | 
|---|
| 465 |   // every following fragments combines its arguments with the initial function
 | 
|---|
| 466 |   for (particletypes_per_model_t::const_iterator typesiter = particletypes_per_model.begin();
 | 
|---|
| 467 |       typesiter != particletypes_per_model.end(); ++typesiter) {
 | 
|---|
| 468 |     Fragment::charges_t charges;
 | 
|---|
| 469 |     {
 | 
|---|
| 470 |       charges.resize((*typesiter).size());
 | 
|---|
| 471 |       std::transform((*typesiter).begin(), (*typesiter).end(),
 | 
|---|
| 472 |           charges.begin(), boost::lambda::_1);
 | 
|---|
| 473 |     }
 | 
|---|
| 474 | 
 | 
|---|
| 475 |     returnfunction =
 | 
|---|
| 476 |           boost::bind(&Extractors::concatenateArguments,
 | 
|---|
| 477 |               boost::bind(returnfunction, _1, _2),
 | 
|---|
| 478 |               boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 479 |                   boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 480 |                   boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 481 |                   charges,  // no crefs here as are temporaries!
 | 
|---|
| 482 |                   _2)
 | 
|---|
| 483 |         );
 | 
|---|
| 484 |   }
 | 
|---|
| 485 |   return returnfunction;
 | 
|---|
| 486 | }
 | 
|---|
| 487 | 
 | 
|---|
| 488 | FunctionModel::filter_t CompoundPotential::getSpecificFilter() const
 | 
|---|
| 489 | {
 | 
|---|
| 490 |   // we must concatenate all filtered arguments here
 | 
|---|
| 491 |   // create initial returnfunction
 | 
|---|
| 492 |   FunctionModel::filter_t returnfunction =
 | 
|---|
| 493 |       boost::bind(&Helpers::returnEmptyArguments);
 | 
|---|
| 494 | 
 | 
|---|
| 495 |   // every following fragments combines its arguments with the initial function
 | 
|---|
| 496 |   for (models_t::const_iterator modeliter = models.begin();
 | 
|---|
| 497 |       modeliter != models.end(); ++modeliter) {
 | 
|---|
| 498 |     returnfunction =
 | 
|---|
| 499 |           boost::bind(&Extractors::concatenateArguments,
 | 
|---|
| 500 |               boost::bind(returnfunction, _1),
 | 
|---|
| 501 |               boost::bind((*modeliter)->getSpecificFilter(), _1)
 | 
|---|
| 502 |         );
 | 
|---|
| 503 |   }
 | 
|---|
| 504 |   return returnfunction;
 | 
|---|
| 505 | }
 | 
|---|
| 506 | 
 | 
|---|
| 507 | size_t CompoundPotential::getSpecificArgumentCount() const
 | 
|---|
| 508 | {
 | 
|---|
| 509 |   std::vector<size_t> argument_counts(models.size(), 0);
 | 
|---|
| 510 |   std::transform(models.begin(), models.end(), argument_counts.begin(),
 | 
|---|
| 511 |       boost::bind(&FunctionModel::getSpecificArgumentCount, _1));
 | 
|---|
| 512 |   return std::accumulate(argument_counts.begin(), argument_counts.end(), 0,
 | 
|---|
| 513 |       std::plus<size_t>());
 | 
|---|
| 514 | }
 | 
|---|
| 515 | 
 | 
|---|