[8203ce8] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
[acc9b1] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[8203ce8] | 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"
|
---|
[94453f1] | 54 | #include "Potentials/InternalCoordinates/OneBody_Constant.hpp"
|
---|
[8203ce8] | 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");
|
---|
[7d320c] | 65 | Coordinator::ptr ConstantPotential::coordinator(Memory::ignore(new OneBody_Constant()));
|
---|
[8203ce8] | 66 |
|
---|
[9c793c] | 67 | static BindingModel generateBindingModel(const EmpiricalPotential::ParticleTypes_t &_ParticleTypes)
|
---|
[d5ca1a] | 68 | {
|
---|
| 69 | // fill nodes
|
---|
| 70 | ASSERT( _ParticleTypes.size() <= (size_t)1,
|
---|
| 71 | "generateBindingModel() - ConstantPotential needs zero or one type.");
|
---|
[9c793c] | 72 | BindingModel::vector_nodes_t nodes;
|
---|
[d5ca1a] | 73 | if (_ParticleTypes.size() == (size_t)1)
|
---|
[9c793c] | 74 | nodes.push_back( FragmentNode(_ParticleTypes.front(), 0) );
|
---|
[d5ca1a] | 75 |
|
---|
| 76 | // there are no edges
|
---|
| 77 | HomologyGraph::edges_t edges;
|
---|
| 78 |
|
---|
[9c793c] | 79 | return BindingModel(nodes, edges);
|
---|
[d5ca1a] | 80 | }
|
---|
| 81 |
|
---|
[a82d33] | 82 | ConstantPotential::ConstantPotential() :
|
---|
| 83 | EmpiricalPotential(),
|
---|
[d5ca1a] | 84 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
[9c793c] | 85 | bindingmodel(BindingModel())
|
---|
[a82d33] | 86 | {
|
---|
| 87 | // have some decent defaults for parameter_derivative checking
|
---|
| 88 | params[energy_offset] = 0.1;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[8203ce8] | 91 | ConstantPotential::ConstantPotential(
|
---|
| 92 | const ParticleTypes_t &_ParticleTypes
|
---|
| 93 | ) :
|
---|
| 94 | EmpiricalPotential(_ParticleTypes),
|
---|
[d5ca1a] | 95 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 96 | bindingmodel(generateBindingModel(_ParticleTypes))
|
---|
[8203ce8] | 97 | {
|
---|
| 98 | // have some decent defaults for parameter_derivative checking
|
---|
| 99 | params[energy_offset] = 0.1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | ConstantPotential::ConstantPotential(
|
---|
| 103 | const ParticleTypes_t &_ParticleTypes,
|
---|
| 104 | const double _energy_offset) :
|
---|
| 105 | EmpiricalPotential(_ParticleTypes),
|
---|
[d5ca1a] | 106 | params(parameters_t(MAXPARAMS, 0.)),
|
---|
| 107 | bindingmodel(generateBindingModel(_ParticleTypes))
|
---|
[8203ce8] | 108 | {
|
---|
| 109 | params[energy_offset] = _energy_offset;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void ConstantPotential::setParameters(const parameters_t &_params)
|
---|
| 113 | {
|
---|
| 114 | const size_t paramsDim = _params.size();
|
---|
| 115 | ASSERT( paramsDim <= getParameterDimension(),
|
---|
| 116 | "ConstantPotential::setParameters() - we need not more than "
|
---|
| 117 | +toString(getParameterDimension())+" parameters.");
|
---|
| 118 | for(size_t i=0;i<paramsDim;++i)
|
---|
| 119 | params[i] = _params[i];
|
---|
| 120 |
|
---|
| 121 | #ifndef NDEBUG
|
---|
| 122 | parameters_t check_params(getParameters());
|
---|
| 123 | check_params.resize(paramsDim); // truncate to same size
|
---|
| 124 | ASSERT( check_params == _params,
|
---|
| 125 | "ConstantPotential::setParameters() - failed, mismatch in to be set "
|
---|
| 126 | +toString(_params)+" and set "+toString(check_params)+" params.");
|
---|
| 127 | #endif
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | ConstantPotential::results_t
|
---|
| 131 | ConstantPotential::operator()(
|
---|
[e1fe7e] | 132 | const list_of_arguments_t &listarguments
|
---|
[8203ce8] | 133 | ) const
|
---|
| 134 | {
|
---|
[e1fe7e] | 135 | // directly set result as energy constant as independent of arg list
|
---|
| 136 | result_t result = params[energy_offset];
|
---|
| 137 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 138 | iter != listarguments.end(); ++iter) {
|
---|
| 139 | const arguments_t &arguments = *iter;
|
---|
| 140 | ASSERT( arguments.size() == 0,
|
---|
| 141 | "ConstantPotential::operator() - requires no argument.");
|
---|
| 142 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 143 | arguments, getParticleTypes()),
|
---|
| 144 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
---|
| 145 | result += 0.;
|
---|
| 146 | }
|
---|
| 147 | return results_t(1, result);
|
---|
[8203ce8] | 148 | }
|
---|
| 149 |
|
---|
| 150 | ConstantPotential::derivative_components_t
|
---|
| 151 | ConstantPotential::derivative(
|
---|
[e1fe7e] | 152 | const list_of_arguments_t &listarguments
|
---|
[8203ce8] | 153 | ) const
|
---|
| 154 | {
|
---|
[e1fe7e] | 155 | result_t result = 0.;
|
---|
| 156 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 157 | iter != listarguments.end(); ++iter) {
|
---|
| 158 | const arguments_t &arguments = *iter;
|
---|
| 159 | ASSERT( arguments.size() == 0,
|
---|
| 160 | "ConstantPotential::operator() - requires no argument.");
|
---|
| 161 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 162 | arguments, getParticleTypes()),
|
---|
| 163 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
---|
| 164 | result += 0.;
|
---|
| 165 | }
|
---|
| 166 | return derivative_components_t(1, result);
|
---|
[8203ce8] | 167 | }
|
---|
| 168 |
|
---|
| 169 | ConstantPotential::results_t
|
---|
| 170 | ConstantPotential::parameter_derivative(
|
---|
[e1fe7e] | 171 | const list_of_arguments_t &listarguments,
|
---|
[8203ce8] | 172 | const size_t index
|
---|
| 173 | ) const
|
---|
| 174 | {
|
---|
[e1fe7e] | 175 | // Maple result: 1
|
---|
| 176 | result_t result = 1.; // energy_offset
|
---|
| 177 | for(list_of_arguments_t::const_iterator iter = listarguments.begin();
|
---|
| 178 | iter != listarguments.end(); ++iter) {
|
---|
| 179 | const arguments_t &arguments = *iter;
|
---|
| 180 | ASSERT( arguments.size() == 0,
|
---|
| 181 | "ConstantPotential::parameter_derivative() - requires no argument.");
|
---|
| 182 | ASSERT( ParticleTypeChecker::checkArgumentsAgainstParticleTypes(
|
---|
| 183 | arguments, getParticleTypes()),
|
---|
| 184 | "ConstantPotential::operator() - types don't match with ones in arguments.");
|
---|
| 185 | // switch (index) {
|
---|
| 186 | // case energy_offset:
|
---|
| 187 | // {
|
---|
| 188 | // // Maple result: 1
|
---|
| 189 | // result = +1.;
|
---|
| 190 | // break;
|
---|
| 191 | // }
|
---|
| 192 | // default:
|
---|
| 193 | // break;
|
---|
| 194 | // }
|
---|
[8203ce8] | 195 | }
|
---|
[e1fe7e] | 196 | return results_t(1, result);
|
---|
[8203ce8] | 197 | }
|
---|
| 198 |
|
---|
[0f5d38] | 199 | FunctionModel::filter_t ConstantPotential::getSpecificFilter() const
|
---|
| 200 | {
|
---|
| 201 | FunctionModel::filter_t returnfunction =
|
---|
[51e0e3] | 202 | boost::bind(&Extractors::filterArgumentsByParticleTypes,
|
---|
[e60558] | 203 | _2, _1,
|
---|
[67044a] | 204 | boost::cref(getParticleTypes()), boost::cref(getBindingModel()));
|
---|
[0f5d38] | 205 | return returnfunction;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[8203ce8] | 208 | void
|
---|
| 209 | ConstantPotential::setParametersToRandomInitialValues(
|
---|
| 210 | const TrainingData &data)
|
---|
| 211 | {
|
---|
| 212 | params[ConstantPotential::energy_offset] =
|
---|
| 213 | data.getTrainingOutputAverage()[0];// -1.;
|
---|
| 214 | }
|
---|
| 215 |
|
---|