[8203ce8] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
|
---|
| 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 | * ConstantPotential.cpp
|
---|
| 26 | *
|
---|
| 27 | * Created on: May 09, 2013
|
---|
| 28 | * Author: heber
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | // include config.h
|
---|
| 33 | #ifdef HAVE_CONFIG_H
|
---|
| 34 | #include <config.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 38 |
|
---|
| 39 | #include "ConstantPotential.hpp"
|
---|
| 40 |
|
---|
| 41 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
|
---|
| 42 | #include <boost/bind.hpp>
|
---|
| 43 | #include <boost/lambda/lambda.hpp>
|
---|
| 44 | #include <cmath>
|
---|
| 45 | #include <string>
|
---|
| 46 |
|
---|
| 47 | #include "CodePatterns/Assert.hpp"
|
---|
| 48 |
|
---|
| 49 | #include "FunctionApproximation/Extractors.hpp"
|
---|
| 50 | #include "FunctionApproximation/TrainingData.hpp"
|
---|
| 51 | #include "Potentials/helpers.hpp"
|
---|
| 52 | #include "Potentials/ParticleTypeCheckers.hpp"
|
---|
| 53 |
|
---|
| 54 | class Fragment;
|
---|
| 55 |
|
---|
| 56 | // static definitions
|
---|
| 57 | const ConstantPotential::ParameterNames_t
|
---|
| 58 | ConstantPotential::ParameterNames =
|
---|
| 59 | boost::assign::list_of<std::string>
|
---|
| 60 | ("energy_offset") //
|
---|
| 61 | ;
|
---|
| 62 | const std::string ConstantPotential::potential_token("constant");
|
---|
| 63 |
|
---|
| 64 | ConstantPotential::ConstantPotential(
|
---|
| 65 | const ParticleTypes_t &_ParticleTypes
|
---|
| 66 | ) :
|
---|
| 67 | EmpiricalPotential(_ParticleTypes),
|
---|
| 68 | params(parameters_t(MAXPARAMS, 0.))
|
---|
| 69 | {
|
---|
| 70 | // have some decent defaults for parameter_derivative checking
|
---|
| 71 | params[energy_offset] = 0.1;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | ConstantPotential::ConstantPotential(
|
---|
| 75 | const ParticleTypes_t &_ParticleTypes,
|
---|
| 76 | const double _energy_offset) :
|
---|
| 77 | EmpiricalPotential(_ParticleTypes),
|
---|
| 78 | params(parameters_t(MAXPARAMS, 0.))
|
---|
| 79 | {
|
---|
| 80 | params[energy_offset] = _energy_offset;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void ConstantPotential::setParameters(const parameters_t &_params)
|
---|
| 84 | {
|
---|
| 85 | const size_t paramsDim = _params.size();
|
---|
| 86 | ASSERT( paramsDim <= getParameterDimension(),
|
---|
| 87 | "ConstantPotential::setParameters() - we need not more than "
|
---|
| 88 | +toString(getParameterDimension())+" parameters.");
|
---|
| 89 | for(size_t i=0;i<paramsDim;++i)
|
---|
| 90 | params[i] = _params[i];
|
---|
| 91 |
|
---|
| 92 | #ifndef NDEBUG
|
---|
| 93 | parameters_t check_params(getParameters());
|
---|
| 94 | check_params.resize(paramsDim); // truncate to same size
|
---|
| 95 | ASSERT( check_params == _params,
|
---|
| 96 | "ConstantPotential::setParameters() - failed, mismatch in to be set "
|
---|
| 97 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
---|
| 98 | #endif
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | ConstantPotential::results_t
|
---|
| 102 | ConstantPotential::operator()(
|
---|
| 103 | const arguments_t &arguments
|
---|
| 104 | ) const
|
---|
| 105 | {
|
---|
| 106 | ASSERT( arguments.size() == 0,
|
---|
| 107 | "ConstantPotential::operator() - requires no argument.");
|
---|
| 108 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 109 | arguments, getParticleTypes()),
|
---|
| 110 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
---|
| 111 | const result_t result = params[energy_offset];
|
---|
| 112 | return std::vector<result_t>(1, result);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | ConstantPotential::derivative_components_t
|
---|
| 116 | ConstantPotential::derivative(
|
---|
| 117 | const arguments_t &arguments
|
---|
| 118 | ) const
|
---|
| 119 | {
|
---|
| 120 | ASSERT( arguments.size() == 0,
|
---|
| 121 | "ConstantPotential::operator() - requires no argument.");
|
---|
| 122 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 123 | arguments, getParticleTypes()),
|
---|
| 124 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
---|
| 125 | derivative_components_t result(1, 0.);
|
---|
| 126 | return result;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | ConstantPotential::results_t
|
---|
| 130 | ConstantPotential::parameter_derivative(
|
---|
| 131 | const arguments_t &arguments,
|
---|
| 132 | const size_t index
|
---|
| 133 | ) const
|
---|
| 134 | {
|
---|
| 135 | ASSERT( arguments.size() == 0,
|
---|
| 136 | "ConstantPotential::parameter_derivative() - requires no argument.");
|
---|
| 137 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 138 | arguments, getParticleTypes()),
|
---|
| 139 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
---|
| 140 | switch (index) {
|
---|
| 141 | case energy_offset:
|
---|
| 142 | {
|
---|
| 143 | // Maple result: 1
|
---|
| 144 | const result_t result = +1.;
|
---|
| 145 | return std::vector<result_t>(1, result);
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
| 148 | default:
|
---|
| 149 | break;
|
---|
| 150 | }
|
---|
| 151 | return std::vector<result_t>(1, 0.);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | FunctionModel::extractor_t
|
---|
| 155 | ConstantPotential::getFragmentSpecificExtractor() const
|
---|
| 156 | {
|
---|
| 157 | Fragment::charges_t charges;
|
---|
| 158 | charges.resize(getParticleTypes().size());
|
---|
| 159 | std::transform(getParticleTypes().begin(), getParticleTypes().end(),
|
---|
| 160 | charges.begin(), boost::lambda::_1);
|
---|
| 161 | FunctionModel::extractor_t returnfunction =
|
---|
| 162 | boost::bind(&Extractors::gatherDistancesFromFragment,
|
---|
| 163 | boost::bind(&Fragment::getPositions, _1),
|
---|
| 164 | boost::bind(&Fragment::getCharges, _1),
|
---|
| 165 | charges,
|
---|
| 166 | _2);
|
---|
| 167 | return returnfunction;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void
|
---|
| 171 | ConstantPotential::setParametersToRandomInitialValues(
|
---|
| 172 | const TrainingData &data)
|
---|
| 173 | {
|
---|
| 174 | params[ConstantPotential::energy_offset] =
|
---|
| 175 | data.getTrainingOutputAverage()[0];// -1.;
|
---|
| 176 | }
|
---|
| 177 |
|
---|