| 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 | #include "Potentials/InternalCoordinates/OneBody_Constant.hpp"
|
|---|
| 55 |
|
|---|
| 56 | class Fragment;
|
|---|
| 57 |
|
|---|
| 58 | // static definitions
|
|---|
| 59 | const ConstantPotential::ParameterNames_t
|
|---|
| 60 | ConstantPotential::ParameterNames =
|
|---|
| 61 | boost::assign::list_of<std::string>
|
|---|
| 62 | ("energy_offset") //
|
|---|
| 63 | ;
|
|---|
| 64 | const std::string ConstantPotential::potential_token("constant");
|
|---|
| 65 | Coordinator::ptr ConstantPotential::coordinator(Memory::ignore(new OneBody_Constant()));
|
|---|
| 66 |
|
|---|
| 67 | static PotentialGraph generateSubgraph()
|
|---|
| 68 | {
|
|---|
| 69 | return PotentialGraph();
|
|---|
| 70 | }
|
|---|
| 71 | const PotentialGraph ConstantPotential::subgraph(generateSubgraph());
|
|---|
| 72 |
|
|---|
| 73 | ConstantPotential::ConstantPotential() :
|
|---|
| 74 | EmpiricalPotential(),
|
|---|
| 75 | params(parameters_t(MAXPARAMS, 0.))
|
|---|
| 76 | {
|
|---|
| 77 | // have some decent defaults for parameter_derivative checking
|
|---|
| 78 | params[energy_offset] = 0.1;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | ConstantPotential::ConstantPotential(
|
|---|
| 82 | const ParticleTypes_t &_ParticleTypes
|
|---|
| 83 | ) :
|
|---|
| 84 | EmpiricalPotential(_ParticleTypes),
|
|---|
| 85 | params(parameters_t(MAXPARAMS, 0.))
|
|---|
| 86 | {
|
|---|
| 87 | // have some decent defaults for parameter_derivative checking
|
|---|
| 88 | params[energy_offset] = 0.1;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | ConstantPotential::ConstantPotential(
|
|---|
| 92 | const ParticleTypes_t &_ParticleTypes,
|
|---|
| 93 | const double _energy_offset) :
|
|---|
| 94 | EmpiricalPotential(_ParticleTypes),
|
|---|
| 95 | params(parameters_t(MAXPARAMS, 0.))
|
|---|
| 96 | {
|
|---|
| 97 | params[energy_offset] = _energy_offset;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void ConstantPotential::setParameters(const parameters_t &_params)
|
|---|
| 101 | {
|
|---|
| 102 | const size_t paramsDim = _params.size();
|
|---|
| 103 | ASSERT( paramsDim <= getParameterDimension(),
|
|---|
| 104 | "ConstantPotential::setParameters() - we need not more than "
|
|---|
| 105 | +toString(getParameterDimension())+" parameters.");
|
|---|
| 106 | for(size_t i=0;i<paramsDim;++i)
|
|---|
| 107 | params[i] = _params[i];
|
|---|
| 108 |
|
|---|
| 109 | #ifndef NDEBUG
|
|---|
| 110 | parameters_t check_params(getParameters());
|
|---|
| 111 | check_params.resize(paramsDim); // truncate to same size
|
|---|
| 112 | ASSERT( check_params == _params,
|
|---|
| 113 | "ConstantPotential::setParameters() - failed, mismatch in to be set "
|
|---|
| 114 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
|---|
| 115 | #endif
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | ConstantPotential::results_t
|
|---|
| 119 | ConstantPotential::operator()(
|
|---|
| 120 | const list_of_arguments_t &listarguments
|
|---|
| 121 | ) const
|
|---|
| 122 | {
|
|---|
| 123 | // directly set result as energy constant as independent of arg list
|
|---|
| 124 | result_t result = params[energy_offset];
|
|---|
| 125 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
|---|
| 126 | iter != listarguments.end(); ++iter) {
|
|---|
| 127 | const arguments_t &arguments = *iter;
|
|---|
| 128 | ASSERT( arguments.size() == 0,
|
|---|
| 129 | "ConstantPotential::operator() - requires no argument.");
|
|---|
| 130 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
|---|
| 131 | arguments, getParticleTypes()),
|
|---|
| 132 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
|---|
| 133 | result += 0.;
|
|---|
| 134 | }
|
|---|
| 135 | return results_t(1, result);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | ConstantPotential::derivative_components_t
|
|---|
| 139 | ConstantPotential::derivative(
|
|---|
| 140 | const list_of_arguments_t &listarguments
|
|---|
| 141 | ) const
|
|---|
| 142 | {
|
|---|
| 143 | result_t result = 0.;
|
|---|
| 144 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
|---|
| 145 | iter != listarguments.end(); ++iter) {
|
|---|
| 146 | const arguments_t &arguments = *iter;
|
|---|
| 147 | ASSERT( arguments.size() == 0,
|
|---|
| 148 | "ConstantPotential::operator() - requires no argument.");
|
|---|
| 149 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
|---|
| 150 | arguments, getParticleTypes()),
|
|---|
| 151 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
|---|
| 152 | result += 0.;
|
|---|
| 153 | }
|
|---|
| 154 | return derivative_components_t(1, result);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | ConstantPotential::results_t
|
|---|
| 158 | ConstantPotential::parameter_derivative(
|
|---|
| 159 | const list_of_arguments_t &listarguments,
|
|---|
| 160 | const size_t index
|
|---|
| 161 | ) const
|
|---|
| 162 | {
|
|---|
| 163 | // Maple result: 1
|
|---|
| 164 | result_t result = 1.; // energy_offset
|
|---|
| 165 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
|---|
| 166 | iter != listarguments.end(); ++iter) {
|
|---|
| 167 | const arguments_t &arguments = *iter;
|
|---|
| 168 | ASSERT( arguments.size() == 0,
|
|---|
| 169 | "ConstantPotential::parameter_derivative() - requires no argument.");
|
|---|
| 170 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
|---|
| 171 | arguments, getParticleTypes()),
|
|---|
| 172 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
|---|
| 173 | // switch (index) {
|
|---|
| 174 | // case energy_offset:
|
|---|
| 175 | // {
|
|---|
| 176 | // // Maple result: 1
|
|---|
| 177 | // result = +1.;
|
|---|
| 178 | // break;
|
|---|
| 179 | // }
|
|---|
| 180 | // default:
|
|---|
| 181 | // break;
|
|---|
| 182 | // }
|
|---|
| 183 | }
|
|---|
| 184 | return results_t(1, result);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | FunctionModel::filter_t ConstantPotential::getSpecificFilter() const
|
|---|
| 188 | {
|
|---|
| 189 | FunctionModel::filter_t returnfunction =
|
|---|
| 190 | boost::bind(&Extractors::filterArgumentsByParticleTypes,
|
|---|
| 191 | _1,
|
|---|
| 192 | getParticleTypes(), getSubgraph());
|
|---|
| 193 | return returnfunction;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | void
|
|---|
| 197 | ConstantPotential::setParametersToRandomInitialValues(
|
|---|
| 198 | const TrainingData &data)
|
|---|
| 199 | {
|
|---|
| 200 | params[ConstantPotential::energy_offset] =
|
|---|
| 201 | data.getTrainingOutputAverage()[0];// -1.;
|
|---|
| 202 | }
|
|---|