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