| 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/PotentialRegistry.hpp"
 | 
|---|
| 56 | 
 | 
|---|
| 57 | 
 | 
|---|
| 58 | CompoundPotential::CompoundPotential(const HomologyGraph &graph)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   LOG(1, "INFO: Creating CompoundPotential for graph " << graph << ".");
 | 
|---|
| 61 |   // look though graph and place all matching FunctionModel's in
 | 
|---|
| 62 |   // PotentialRegistry in models
 | 
|---|
| 63 |   PotentialRegistry::const_iterator potentialiter =
 | 
|---|
| 64 |       PotentialRegistry::getInstance().getBeginIter();
 | 
|---|
| 65 |   while (potentialiter != PotentialRegistry::getInstance().getEndIter()) {
 | 
|---|
| 66 |     // get model and types
 | 
|---|
| 67 |     EmpiricalPotential * const potential = potentialiter->second;
 | 
|---|
| 68 |     const SerializablePotential::ParticleTypes_t &types =
 | 
|---|
| 69 |         potential->getParticleTypes();
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     // create charges
 | 
|---|
| 72 |     Fragment::charges_t charges;
 | 
|---|
| 73 |     charges.resize(types.size());
 | 
|---|
| 74 |     std::transform(types.begin(), types.end(),
 | 
|---|
| 75 |         charges.begin(), boost::lambda::_1);
 | 
|---|
| 76 |     // convert into count map
 | 
|---|
| 77 |     Extractors::elementcounts_t counts_per_charge =
 | 
|---|
| 78 |         Extractors::_detail::getElementCounts(charges);
 | 
|---|
| 79 | //    ASSERT( !counts_per_charge.empty(),
 | 
|---|
| 80 | //        "getFirstGraphwithSpecifiedElements() - charge counts are empty?");
 | 
|---|
| 81 |     LOG(2, "DEBUG: counts_per_charge is " << counts_per_charge << ".");
 | 
|---|
| 82 | 
 | 
|---|
| 83 |     // check whether graph contains suitable types
 | 
|---|
| 84 |     Extractors::elementcounts_t::const_iterator countiter = counts_per_charge.begin();
 | 
|---|
| 85 |     for (; countiter != counts_per_charge.end(); ++countiter)
 | 
|---|
| 86 |       if (!graph.hasTimesAtomicNumber(
 | 
|---|
| 87 |           static_cast<size_t>(countiter->first),
 | 
|---|
| 88 |           static_cast<size_t>(countiter->second))
 | 
|---|
| 89 |           )
 | 
|---|
| 90 |         break;
 | 
|---|
| 91 |     // if we have a match for every count, store model
 | 
|---|
| 92 |     if( countiter == counts_per_charge.end()) {
 | 
|---|
| 93 |       LOG(1, "INFO: Potential " << potentialiter->first << " matches with fragment.");
 | 
|---|
| 94 |       models.push_back(static_cast<FunctionModel*>(potential));
 | 
|---|
| 95 |       particletypes_per_model.push_back(types);
 | 
|---|
| 96 |     }
 | 
|---|
| 97 |     ++potentialiter;
 | 
|---|
| 98 |   }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   // check that models and particletypes_per_model match
 | 
|---|
| 101 |   ASSERT( models.size() == particletypes_per_model.size(),
 | 
|---|
| 102 |       "CompoundPotential::CompoundPotential() - particletypes not stored for all models?");
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | CompoundPotential::~CompoundPotential()
 | 
|---|
| 106 | {
 | 
|---|
| 107 |   // clear all models and internally stored particletypes
 | 
|---|
| 108 |   models.clear();
 | 
|---|
| 109 |   particletypes_per_model.clear();
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | void CompoundPotential::setParameters(const parameters_t &_params)
 | 
|---|
| 113 | {
 | 
|---|
| 114 |   size_t dim = _params.size();
 | 
|---|
| 115 |   parameters_t::const_iterator iter = _params.begin();
 | 
|---|
| 116 |   BOOST_FOREACH( FunctionModel* model, models) {
 | 
|---|
| 117 |     const parameters_t &model_params = model->getParameters();
 | 
|---|
| 118 |     const size_t model_dim = model_params.size();
 | 
|---|
| 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 | 
 | 
|---|
| 135 | CompoundPotential::parameters_t CompoundPotential::getParameters() const
 | 
|---|
| 136 | {
 | 
|---|
| 137 |   const size_t dimension = getParameterDimension();
 | 
|---|
| 138 |   CompoundPotential::parameters_t parameters(dimension);
 | 
|---|
| 139 |   CompoundPotential::parameters_t::iterator iter = parameters.begin();
 | 
|---|
| 140 |   BOOST_FOREACH( const FunctionModel* model, models) {
 | 
|---|
| 141 |     const CompoundPotential::parameters_t ¶ms = model->getParameters();
 | 
|---|
| 142 |     std::copy(params.begin(), params.end(), iter);
 | 
|---|
| 143 |     ASSERT( iter != parameters.end(),
 | 
|---|
| 144 |         "CompoundPotential::getParameters() - iter already at end.");
 | 
|---|
| 145 |   }
 | 
|---|
| 146 |   return parameters;
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | void CompoundPotential::setParametersToRandomInitialValues(const TrainingData &data)
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   std::for_each(models.begin(), models.end(),
 | 
|---|
| 152 |       boost::bind(&FunctionModel::setParametersToRandomInitialValues, _1, boost::cref(data))
 | 
|---|
| 153 |   );
 | 
|---|
| 154 | }
 | 
|---|
| 155 | 
 | 
|---|
| 156 | size_t CompoundPotential::getParameterDimension() const
 | 
|---|
| 157 | {
 | 
|---|
| 158 |   std::vector<size_t> dimensions(models.size(), 0);
 | 
|---|
| 159 |   std::transform(models.begin(), models.end(), dimensions.begin(),
 | 
|---|
| 160 |       boost::bind(&FunctionModel::getParameterDimension, _1));
 | 
|---|
| 161 |   return std::accumulate(dimensions.begin(), dimensions.end(), 0, std::plus<size_t>());
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | void CompoundPotential::setTriplefunction(triplefunction_t &_triplefunction)
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   std::for_each(models.begin(), models.end(),
 | 
|---|
| 167 |       boost::bind(&FunctionModel::setTriplefunction, _1, boost::ref(_triplefunction))
 | 
|---|
| 168 |   );
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | std::vector<CompoundPotential::arguments_t> CompoundPotential::splitUpArgumentsByModels(
 | 
|---|
| 172 |     const arguments_t &arguments) const
 | 
|---|
| 173 | {
 | 
|---|
| 174 |   std::vector<arguments_t> partial_args(models.size());
 | 
|---|
| 175 |   std::vector<arguments_t>::iterator partialiter = partial_args.begin();
 | 
|---|
| 176 |   arguments_t::const_iterator argiter = arguments.begin();
 | 
|---|
| 177 |   BOOST_FOREACH( const SerializablePotential::ParticleTypes_t &types, particletypes_per_model) {
 | 
|---|
| 178 |     // we always expect N(N-1)/2 distances for N particle types
 | 
|---|
| 179 |     arguments_t::const_iterator enditer = argiter+(types.size()*(types.size()-1)/2);
 | 
|---|
| 180 |     ASSERT( argiter != arguments.end(),
 | 
|---|
| 181 |         "CompoundPotential::splitUpArgumentsByModels() - incorrect number of arguments.");
 | 
|---|
| 182 |     std::copy(argiter, enditer, std::back_inserter(*partialiter++));
 | 
|---|
| 183 |     argiter = enditer;
 | 
|---|
| 184 |   }
 | 
|---|
| 185 |   ASSERT( argiter == arguments.end(),
 | 
|---|
| 186 |       "CompoundPotential::splitUpArgumentsByModels() - incorrect number of arguments.");
 | 
|---|
| 187 |   return partial_args;
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | CompoundPotential::results_t CompoundPotential::operator()(const arguments_t &arguments) const
 | 
|---|
| 191 | {
 | 
|---|
| 192 |   // first, we have to split up the given arguments
 | 
|---|
| 193 |   std::vector<arguments_t> partial_args =
 | 
|---|
| 194 |       splitUpArgumentsByModels(arguments);
 | 
|---|
| 195 |   // then, with each bunch of arguments, we call the specific model
 | 
|---|
| 196 |   results_t results(1,0.);
 | 
|---|
| 197 |   std::vector<results_t> partial_results(models.size());
 | 
|---|
| 198 |   std::transform(
 | 
|---|
| 199 |       models.begin(), models.end(),
 | 
|---|
| 200 |       partial_args.begin(),
 | 
|---|
| 201 |       partial_results.begin(),
 | 
|---|
| 202 |       boost::bind(&FunctionModel::operator(), _1, _2)
 | 
|---|
| 203 |   );
 | 
|---|
| 204 |   std::for_each(partial_results.begin(), partial_results.end(),
 | 
|---|
| 205 |       std::cout << (boost::lambda::_1)[0] << "\t");
 | 
|---|
| 206 |   std::for_each(partial_results.begin(), partial_results.end(),
 | 
|---|
| 207 |       results[0] += (boost::lambda::_1)[0]);
 | 
|---|
| 208 |   return results;
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | CompoundPotential::results_t CompoundPotential::parameter_derivative(const arguments_t &arguments, const size_t index) const
 | 
|---|
| 212 | {
 | 
|---|
| 213 |   // first, we have to split up the given arguments
 | 
|---|
| 214 |   std::vector<arguments_t> partial_args =
 | 
|---|
| 215 |       splitUpArgumentsByModels(arguments);
 | 
|---|
| 216 |   // then, with each bunch of arguments, we call the specific model
 | 
|---|
| 217 |   // get parameter dimensions per model
 | 
|---|
| 218 |   std::vector<size_t> dimensions(models.size(), 0);
 | 
|---|
| 219 |   std::transform(models.begin(), models.end(), dimensions.begin(),
 | 
|---|
| 220 |       boost::bind(&FunctionModel::getParameterDimension, _1));
 | 
|---|
| 221 | 
 | 
|---|
| 222 |   // convert to index end+1 per model
 | 
|---|
| 223 |   std::partial_sum(dimensions.begin(), dimensions.end(), dimensions.begin());
 | 
|---|
| 224 | 
 | 
|---|
| 225 |   // look for value not less than index
 | 
|---|
| 226 |   std::vector<size_t>::const_iterator iter =
 | 
|---|
| 227 |       std::lower_bound(dimensions.begin(), dimensions.end(), index);
 | 
|---|
| 228 | 
 | 
|---|
| 229 |   // step forward to same model
 | 
|---|
| 230 |   models_t::const_iterator modeliter = models.begin();
 | 
|---|
| 231 |   std::advance(modeliter,
 | 
|---|
| 232 |       std::distance(const_cast<const std::vector<size_t> &>(dimensions).begin(), iter) );
 | 
|---|
| 233 |   std::vector<arguments_t>::const_iterator argiter = partial_args.begin();
 | 
|---|
| 234 |   std::advance(argiter,
 | 
|---|
| 235 |       std::distance(const_cast<const std::vector<size_t> &>(dimensions).begin(), iter) );
 | 
|---|
| 236 | 
 | 
|---|
| 237 |   // evaluate with correct relative index and return
 | 
|---|
| 238 |   const size_t indexbase = (iter == dimensions.begin()) ? 0 : *(--iter);
 | 
|---|
| 239 |   CompoundPotential::results_t results =
 | 
|---|
| 240 |       (*modeliter)->parameter_derivative(*argiter, index-indexbase);
 | 
|---|
| 241 |   return results;
 | 
|---|
| 242 | }
 | 
|---|
| 243 | 
 | 
|---|
| 244 | bool CompoundPotential::isBoxConstraint() const
 | 
|---|
| 245 | {
 | 
|---|
| 246 |   std::vector<bool> constraints(models.size(), 0);
 | 
|---|
| 247 |   std::transform(models.begin(), models.end(), constraints.begin(),
 | 
|---|
| 248 |       boost::bind(&FunctionModel::getParameterDimension, _1));
 | 
|---|
| 249 |   return std::accumulate(constraints.begin(), constraints.end(), true,
 | 
|---|
| 250 |       std::logical_and<bool>());
 | 
|---|
| 251 | }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | CompoundPotential::parameters_t CompoundPotential::getLowerBoxConstraints() const
 | 
|---|
| 254 | {
 | 
|---|
| 255 |   const size_t dimension = getParameterDimension();
 | 
|---|
| 256 |   CompoundPotential::parameters_t constraints(dimension);
 | 
|---|
| 257 |   CompoundPotential::parameters_t::iterator iter = constraints.begin();
 | 
|---|
| 258 |   BOOST_FOREACH( FunctionModel* model, models) {
 | 
|---|
| 259 |     const CompoundPotential::parameters_t params = model->getLowerBoxConstraints();
 | 
|---|
| 260 |     std::copy(params.begin(), params.end(), iter);
 | 
|---|
| 261 |     ASSERT( iter != constraints.end(),
 | 
|---|
| 262 |         "CompoundPotential::getParameters() - iter already at end.");
 | 
|---|
| 263 |   }
 | 
|---|
| 264 |   return constraints;
 | 
|---|
| 265 | }
 | 
|---|
| 266 | 
 | 
|---|
| 267 | CompoundPotential::parameters_t CompoundPotential::getUpperBoxConstraints() const
 | 
|---|
| 268 | {
 | 
|---|
| 269 |   const size_t dimension = getParameterDimension();
 | 
|---|
| 270 |   CompoundPotential::parameters_t constraints(dimension);
 | 
|---|
| 271 |   CompoundPotential::parameters_t::iterator iter = constraints.begin();
 | 
|---|
| 272 |   BOOST_FOREACH( FunctionModel* model, models) {
 | 
|---|
| 273 |     const CompoundPotential::parameters_t params = model->getUpperBoxConstraints();
 | 
|---|
| 274 |     std::copy(params.begin(), params.end(), iter);
 | 
|---|
| 275 |     ASSERT( iter != constraints.end(),
 | 
|---|
| 276 |         "CompoundPotential::getParameters() - iter already at end.");
 | 
|---|
| 277 |   }
 | 
|---|
| 278 |   return constraints;
 | 
|---|
| 279 | }
 | 
|---|
| 280 | 
 | 
|---|
| 281 | FunctionModel::extractor_t CompoundPotential::getFragmentSpecificExtractor() const
 | 
|---|
| 282 | {
 | 
|---|
| 283 |   // create initial returnfunction
 | 
|---|
| 284 |   particletypes_per_model_t::const_iterator typesiter = particletypes_per_model.begin();
 | 
|---|
| 285 |   Fragment::charges_t charges;
 | 
|---|
| 286 |   {
 | 
|---|
| 287 |     charges.resize((*typesiter).size());
 | 
|---|
| 288 |     std::transform((*typesiter).begin(), (*typesiter).end(),
 | 
|---|
| 289 |         charges.begin(), boost::lambda::_1);
 | 
|---|
| 290 |   }
 | 
|---|
| 291 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
| 292 |     boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 293 |         boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 294 |         boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 295 |         charges,  // no crefs here as are temporaries!
 | 
|---|
| 296 |         _2);
 | 
|---|
| 297 | 
 | 
|---|
| 298 |   // every following fragments combines its arguments with the initial function
 | 
|---|
| 299 |   for (; typesiter != particletypes_per_model.end(); ++typesiter) {
 | 
|---|
| 300 |     Fragment::charges_t charges;
 | 
|---|
| 301 |     {
 | 
|---|
| 302 |       charges.resize((*typesiter).size());
 | 
|---|
| 303 |       std::transform((*typesiter).begin(), (*typesiter).end(),
 | 
|---|
| 304 |           charges.begin(), boost::lambda::_1);
 | 
|---|
| 305 |     }
 | 
|---|
| 306 | 
 | 
|---|
| 307 |     returnfunction =
 | 
|---|
| 308 |           boost::bind(&Extractors::concatenateArguments,
 | 
|---|
| 309 |               boost::bind(returnfunction, _1, _2),
 | 
|---|
| 310 |               boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 311 |                   boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 312 |                   boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 313 |                   charges,  // no crefs here as are temporaries!
 | 
|---|
| 314 |                   _2)
 | 
|---|
| 315 |         );
 | 
|---|
| 316 |   }
 | 
|---|
| 317 |   return returnfunction;
 | 
|---|
| 318 | }
 | 
|---|