| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 6 |  * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 7 |  * 
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *   This file is part of MoleCuilder.
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 12 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 13 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 14 |  *    (at your option) any later version.
 | 
|---|
| 15 |  *
 | 
|---|
| 16 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 17 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 |  *    GNU General Public License for more details.
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 22 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. 
 | 
|---|
| 23 |  */
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /*
 | 
|---|
| 26 |  * ConstantPotential.cpp
 | 
|---|
| 27 |  *
 | 
|---|
| 28 |  *  Created on: May 09, 2013
 | 
|---|
| 29 |  *      Author: heber
 | 
|---|
| 30 |  */
 | 
|---|
| 31 | 
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // include config.h
 | 
|---|
| 34 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 35 | #include <config.h>
 | 
|---|
| 36 | #endif
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "ConstantPotential.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
 | 
|---|
| 43 | #include <boost/bind.hpp>
 | 
|---|
| 44 | #include <boost/lambda/lambda.hpp>
 | 
|---|
| 45 | #include <cmath>
 | 
|---|
| 46 | #include <string>
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #include "FunctionApproximation/Extractors.hpp"
 | 
|---|
| 51 | #include "FunctionApproximation/TrainingData.hpp"
 | 
|---|
| 52 | #include "Potentials/helpers.hpp"
 | 
|---|
| 53 | #include "Potentials/ParticleTypeCheckers.hpp"
 | 
|---|
| 54 | 
 | 
|---|
| 55 | class Fragment;
 | 
|---|
| 56 | 
 | 
|---|
| 57 | // static definitions
 | 
|---|
| 58 | const ConstantPotential::ParameterNames_t
 | 
|---|
| 59 | ConstantPotential::ParameterNames =
 | 
|---|
| 60 |       boost::assign::list_of<std::string>
 | 
|---|
| 61 |       ("energy_offset") //
 | 
|---|
| 62 |     ;
 | 
|---|
| 63 | const std::string ConstantPotential::potential_token("constant");
 | 
|---|
| 64 | 
 | 
|---|
| 65 | ConstantPotential::ConstantPotential() :
 | 
|---|
| 66 |     EmpiricalPotential(),
 | 
|---|
| 67 |     params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
| 70 |   params[energy_offset] = 0.1;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | ConstantPotential::ConstantPotential(
 | 
|---|
| 74 |     const ParticleTypes_t &_ParticleTypes
 | 
|---|
| 75 |     ) :
 | 
|---|
| 76 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| 77 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| 78 | {
 | 
|---|
| 79 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
| 80 |   params[energy_offset] = 0.1;
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | ConstantPotential::ConstantPotential(
 | 
|---|
| 84 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| 85 |     const double _energy_offset) :
 | 
|---|
| 86 |   EmpiricalPotential(_ParticleTypes),
 | 
|---|
| 87 |   params(parameters_t(MAXPARAMS, 0.))
 | 
|---|
| 88 | {
 | 
|---|
| 89 |   params[energy_offset] = _energy_offset;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void ConstantPotential::setParameters(const parameters_t &_params)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   const size_t paramsDim = _params.size();
 | 
|---|
| 95 |   ASSERT( paramsDim <= getParameterDimension(),
 | 
|---|
| 96 |       "ConstantPotential::setParameters() - we need not more than "
 | 
|---|
| 97 |       +toString(getParameterDimension())+" parameters.");
 | 
|---|
| 98 |   for(size_t i=0;i<paramsDim;++i)
 | 
|---|
| 99 |     params[i] = _params[i];
 | 
|---|
| 100 | 
 | 
|---|
| 101 | #ifndef NDEBUG
 | 
|---|
| 102 |   parameters_t check_params(getParameters());
 | 
|---|
| 103 |   check_params.resize(paramsDim); // truncate to same size
 | 
|---|
| 104 |   ASSERT( check_params == _params,
 | 
|---|
| 105 |       "ConstantPotential::setParameters() - failed, mismatch in to be set "
 | 
|---|
| 106 |       +toString(_params)+" and set "+toString(check_params)+" params.");
 | 
|---|
| 107 | #endif
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | ConstantPotential::results_t
 | 
|---|
| 111 | ConstantPotential::operator()(
 | 
|---|
| 112 |     const arguments_t &arguments
 | 
|---|
| 113 |     ) const
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   ASSERT( arguments.size() == 0,
 | 
|---|
| 116 |       "ConstantPotential::operator() - requires no argument.");
 | 
|---|
| 117 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
| 118 |       arguments, getParticleTypes()),
 | 
|---|
| 119 |       "ConstantPotential::operator() - types don't match with ones in arguments.");
 | 
|---|
| 120 |   const result_t result = params[energy_offset];
 | 
|---|
| 121 |   return std::vector<result_t>(1, result);
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | ConstantPotential::derivative_components_t
 | 
|---|
| 125 | ConstantPotential::derivative(
 | 
|---|
| 126 |     const arguments_t &arguments
 | 
|---|
| 127 |     ) const
 | 
|---|
| 128 | {
 | 
|---|
| 129 |   ASSERT( arguments.size() == 0,
 | 
|---|
| 130 |       "ConstantPotential::operator() - requires no argument.");
 | 
|---|
| 131 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
| 132 |       arguments, getParticleTypes()),
 | 
|---|
| 133 |       "ConstantPotential::operator() - types don't match with ones in arguments.");
 | 
|---|
| 134 |   derivative_components_t result(1, 0.);
 | 
|---|
| 135 |   return result;
 | 
|---|
| 136 | }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | ConstantPotential::results_t
 | 
|---|
| 139 | ConstantPotential::parameter_derivative(
 | 
|---|
| 140 |     const arguments_t &arguments,
 | 
|---|
| 141 |     const size_t index
 | 
|---|
| 142 |     ) const
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   ASSERT( arguments.size() == 0,
 | 
|---|
| 145 |       "ConstantPotential::parameter_derivative() - requires no argument.");
 | 
|---|
| 146 |   ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
 | 
|---|
| 147 |       arguments, getParticleTypes()),
 | 
|---|
| 148 |       "ConstantPotential::operator() - types don't match with ones in arguments.");
 | 
|---|
| 149 |   switch (index) {
 | 
|---|
| 150 |     case energy_offset:
 | 
|---|
| 151 |     {
 | 
|---|
| 152 |       // Maple result: 1
 | 
|---|
| 153 |       const result_t result = +1.;
 | 
|---|
| 154 |       return std::vector<result_t>(1, result);
 | 
|---|
| 155 |       break;
 | 
|---|
| 156 |     }
 | 
|---|
| 157 |     default:
 | 
|---|
| 158 |       break;
 | 
|---|
| 159 |   }
 | 
|---|
| 160 |   return std::vector<result_t>(1, 0.);
 | 
|---|
| 161 | }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | FunctionModel::extractor_t
 | 
|---|
| 164 | ConstantPotential::getFragmentSpecificExtractor() const
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   Fragment::charges_t charges;
 | 
|---|
| 167 |   charges.resize(getParticleTypes().size());
 | 
|---|
| 168 |   std::transform(getParticleTypes().begin(), getParticleTypes().end(),
 | 
|---|
| 169 |       charges.begin(), boost::lambda::_1);
 | 
|---|
| 170 |   FunctionModel::extractor_t returnfunction =
 | 
|---|
| 171 |       boost::bind(&Extractors::gatherDistancesFromFragment,
 | 
|---|
| 172 |           boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 173 |           boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 174 |           charges,
 | 
|---|
| 175 |           _2);
 | 
|---|
| 176 |   return returnfunction;
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | void
 | 
|---|
| 180 | ConstantPotential::setParametersToRandomInitialValues(
 | 
|---|
| 181 |     const TrainingData &data)
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   params[ConstantPotential::energy_offset] =
 | 
|---|
| 184 |       data.getTrainingOutputAverage()[0];// -1.;
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|