| 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 |  * SaturationPotential.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Oct 11, 2012
 | 
|---|
| 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 "SaturationPotential.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include <boost/assign.hpp>
 | 
|---|
| 42 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
 | 
|---|
| 43 | #include <iostream>
 | 
|---|
| 44 | #include <string>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 47 | #include "CodePatterns/Log.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 | using namespace boost::assign;
 | 
|---|
| 57 | 
 | 
|---|
| 58 | // static definitions
 | 
|---|
| 59 | const SaturationPotential::ParameterNames_t
 | 
|---|
| 60 | SaturationPotential::ParameterNames =
 | 
|---|
| 61 |       boost::assign::list_of<std::string>
 | 
|---|
| 62 |       ("all_energy_offset")
 | 
|---|
| 63 |       ("")
 | 
|---|
| 64 |       ("")
 | 
|---|
| 65 |       ("")
 | 
|---|
| 66 |       ("")
 | 
|---|
| 67 |       ("")
 | 
|---|
| 68 |     ;
 | 
|---|
| 69 | const std::string SaturationPotential::potential_token("saturation");
 | 
|---|
| 70 | 
 | 
|---|
| 71 | SaturationPotential::SaturationPotential(
 | 
|---|
| 72 |     const ParticleTypes_t &_ParticleTypes) :
 | 
|---|
| 73 |   SerializablePotential(_ParticleTypes),
 | 
|---|
| 74 |   morse(_ParticleTypes),
 | 
|---|
| 75 |   angle(symmetrizeTypes(_ParticleTypes)),
 | 
|---|
| 76 |   energy_offset(0.)
 | 
|---|
| 77 | {
 | 
|---|
| 78 |   // have some decent defaults for parameter_derivative checking
 | 
|---|
| 79 |   // Morse and Angle have their own defaults, offset is set
 | 
|---|
| 80 |   ASSERT( _ParticleTypes.size() == (size_t)2,
 | 
|---|
| 81 |       "SaturationPotential::SaturationPotential() - exactly two types must be given.");
 | 
|---|
| 82 |   ASSERT( _ParticleTypes[1] == 1,
 | 
|---|
| 83 |       "SaturationPotential::SaturationPotential() - second type must be hydrogen.");
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | SaturationPotential::SaturationPotential(
 | 
|---|
| 87 |     const ParticleTypes_t &_ParticleTypes,
 | 
|---|
| 88 |     const double _all_energy_offset,
 | 
|---|
| 89 |     const double _morse_spring_constant,
 | 
|---|
| 90 |     const double _morse_equilibrium_distance,
 | 
|---|
| 91 |     const double _morse_dissociation_energy,
 | 
|---|
| 92 |     const double _angle_spring_constant,
 | 
|---|
| 93 |     const double _angle_equilibrium_distance) :
 | 
|---|
| 94 |   SerializablePotential(_ParticleTypes),
 | 
|---|
| 95 |   morse(_ParticleTypes),
 | 
|---|
| 96 |   angle(symmetrizeTypes(_ParticleTypes)),
 | 
|---|
| 97 |   energy_offset(_all_energy_offset)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   ASSERT( _ParticleTypes.size() == (size_t)2,
 | 
|---|
| 100 |       "SaturationPotential::SaturationPotential() - exactly two types must be given.");
 | 
|---|
| 101 |   ASSERT( _ParticleTypes[1] == 1,
 | 
|---|
| 102 |       "SaturationPotential::SaturationPotential() - second type must be hydrogen.");
 | 
|---|
| 103 |   parameters_t morse_params(morse.getParameterDimension());
 | 
|---|
| 104 |   morse_params[PairPotential_Morse::spring_constant] = _morse_spring_constant;
 | 
|---|
| 105 |   morse_params[PairPotential_Morse::equilibrium_distance] = _morse_equilibrium_distance;
 | 
|---|
| 106 |   morse_params[PairPotential_Morse::dissociation_energy] = _morse_dissociation_energy;
 | 
|---|
| 107 |   morse_params[PairPotential_Morse::energy_offset] = 0.;
 | 
|---|
| 108 |   morse.setParameters(morse_params);
 | 
|---|
| 109 |   parameters_t angle_params(angle.getParameterDimension());
 | 
|---|
| 110 |   angle_params[PairPotential_Angle::spring_constant] = _angle_spring_constant;
 | 
|---|
| 111 |   angle_params[PairPotential_Angle::equilibrium_distance] = _angle_equilibrium_distance;
 | 
|---|
| 112 |   angle_params[PairPotential_Angle::energy_offset] = 0.;
 | 
|---|
| 113 |   angle.setParameters(angle_params);
 | 
|---|
| 114 | }
 | 
|---|
| 115 | 
 | 
|---|
| 116 | void SaturationPotential::setParameters(const parameters_t &_params)
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   const size_t paramsDim = _params.size();
 | 
|---|
| 119 |   ASSERT( paramsDim <= getParameterDimension(),
 | 
|---|
| 120 |       "SaturationPotential::setParameters() - we need not more than "
 | 
|---|
| 121 |       +toString(getParameterDimension())+" parameters.");
 | 
|---|
| 122 | //    LOG(1, "INFO: Setting new SaturationPotential params: " << _params);
 | 
|---|
| 123 | 
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   // offsets
 | 
|---|
| 126 |   if (paramsDim > all_energy_offset)
 | 
|---|
| 127 |     energy_offset = _params[all_energy_offset];
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   // Morse
 | 
|---|
| 130 |   {
 | 
|---|
| 131 |     parameters_t morse_params(morse.getParameters());
 | 
|---|
| 132 |     if (paramsDim > morse_spring_constant)
 | 
|---|
| 133 |       morse_params[PairPotential_Morse::spring_constant] = _params[morse_spring_constant];
 | 
|---|
| 134 |     if (paramsDim > morse_equilibrium_distance)
 | 
|---|
| 135 |       morse_params[PairPotential_Morse::equilibrium_distance] = _params[morse_equilibrium_distance];
 | 
|---|
| 136 |     if (paramsDim > morse_dissociation_energy)
 | 
|---|
| 137 |       morse_params[PairPotential_Morse::dissociation_energy] = _params[morse_dissociation_energy];
 | 
|---|
| 138 |     morse_params[PairPotential_Morse::energy_offset] = 0.;
 | 
|---|
| 139 |     morse.setParameters(morse_params);
 | 
|---|
| 140 |   }
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   // Angle
 | 
|---|
| 143 |   {
 | 
|---|
| 144 |     parameters_t angle_params(angle.getParameters());
 | 
|---|
| 145 |     if (paramsDim > angle_spring_constant)
 | 
|---|
| 146 |       angle_params[PairPotential_Angle::spring_constant] = _params[angle_spring_constant];
 | 
|---|
| 147 |     if (paramsDim > angle_equilibrium_distance)
 | 
|---|
| 148 |       angle_params[PairPotential_Angle::equilibrium_distance] = _params[angle_equilibrium_distance];
 | 
|---|
| 149 |     angle_params[PairPotential_Angle::energy_offset] = 0.;
 | 
|---|
| 150 |     angle.setParameters(angle_params);
 | 
|---|
| 151 |   }
 | 
|---|
| 152 | #ifndef NDEBUG
 | 
|---|
| 153 |   parameters_t check_params(getParameters());
 | 
|---|
| 154 |   check_params.resize(paramsDim); // truncate to same size
 | 
|---|
| 155 |   ASSERT( check_params == _params,
 | 
|---|
| 156 |       "SaturationPotential::setParameters() - failed, mismatch in to be set "
 | 
|---|
| 157 |       +toString(_params)+" and set "+toString(check_params)+" params.");
 | 
|---|
| 158 | #endif
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | SaturationPotential::parameters_t SaturationPotential::getParameters() const
 | 
|---|
| 162 | {
 | 
|---|
| 163 |   parameters_t params(getParameterDimension());
 | 
|---|
| 164 |   const parameters_t morse_params = morse.getParameters();
 | 
|---|
| 165 |   const parameters_t angle_params = angle.getParameters();
 | 
|---|
| 166 | 
 | 
|---|
| 167 |   params[all_energy_offset] = energy_offset;
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   params[morse_spring_constant] = morse_params[PairPotential_Morse::spring_constant];
 | 
|---|
| 170 |   params[morse_equilibrium_distance] = morse_params[PairPotential_Morse::equilibrium_distance];
 | 
|---|
| 171 |   params[morse_dissociation_energy] = morse_params[PairPotential_Morse::dissociation_energy];
 | 
|---|
| 172 | 
 | 
|---|
| 173 |   params[angle_spring_constant] = angle_params[PairPotential_Angle::spring_constant];
 | 
|---|
| 174 |   params[angle_equilibrium_distance] = angle_params[PairPotential_Angle::equilibrium_distance];
 | 
|---|
| 175 |   return params;
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | std::vector<FunctionModel::arguments_t>
 | 
|---|
| 179 | triplefunction(
 | 
|---|
| 180 |     const argument_t &argument,
 | 
|---|
| 181 |     const FunctionModel::arguments_t& args)
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   const size_t firstindex = argument.indices.first;
 | 
|---|
| 184 |   const size_t secondindex = argument.indices.second;
 | 
|---|
| 185 | //  LOG(2, "DEBUG: first index is " << firstindex << ", second index is " << secondindex << ".");
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   // place all arguments that share either index into a lookup map
 | 
|---|
| 188 |   typedef std::map< size_t, FunctionModel::arguments_t::const_iterator > IndexLookup_t;
 | 
|---|
| 189 |   IndexLookup_t LookuptoFirst;
 | 
|---|
| 190 |   IndexLookup_t LookuptoSecond;
 | 
|---|
| 191 |   for (FunctionModel::arguments_t::const_iterator iter = args.begin();
 | 
|---|
| 192 |       iter != args.end();
 | 
|---|
| 193 |       ++iter) {
 | 
|---|
| 194 |     if (((*iter).indices.first == argument.indices.first)
 | 
|---|
| 195 |         && ((*iter).indices.second == argument.indices.second))
 | 
|---|
| 196 |       continue;
 | 
|---|
| 197 |     if (firstindex == (*iter).indices.first) {
 | 
|---|
| 198 |       LookuptoFirst.insert( std::make_pair( (*iter).indices.second, iter) );
 | 
|---|
| 199 |     }
 | 
|---|
| 200 |     else if (firstindex == (*iter).indices.second) {
 | 
|---|
| 201 |       LookuptoFirst.insert( std::make_pair( (*iter).indices.first, iter) );
 | 
|---|
| 202 |     }
 | 
|---|
| 203 |     if (secondindex == (*iter).indices.first) {
 | 
|---|
| 204 |       LookuptoSecond.insert( std::make_pair( (*iter).indices.second, iter) );
 | 
|---|
| 205 |     }
 | 
|---|
| 206 |     else if (secondindex == (*iter).indices.second) {
 | 
|---|
| 207 |       LookuptoSecond.insert( std::make_pair((*iter).indices.first, iter) );
 | 
|---|
| 208 |     }
 | 
|---|
| 209 |   }
 | 
|---|
| 210 | //  {
 | 
|---|
| 211 | //    std::stringstream lookupstream;
 | 
|---|
| 212 | //    for (IndexLookup_t::const_iterator iter = LookuptoFirst.begin();
 | 
|---|
| 213 | //        iter != LookuptoFirst.end();
 | 
|---|
| 214 | //        ++iter) {
 | 
|---|
| 215 | //      lookupstream << "(" << iter->first << "," << *(iter->second) << ") ";
 | 
|---|
| 216 | //    }
 | 
|---|
| 217 | //    LOG(2, "DEBUG: LookupToFirst is " << lookupstream.str() << ".");
 | 
|---|
| 218 | //  }
 | 
|---|
| 219 | //  {
 | 
|---|
| 220 | //    std::stringstream lookupstream;
 | 
|---|
| 221 | //    for (IndexLookup_t::const_iterator iter = LookuptoSecond.begin();
 | 
|---|
| 222 | //        iter != LookuptoSecond.end();
 | 
|---|
| 223 | //        ++iter) {
 | 
|---|
| 224 | //      lookupstream << "(" << iter->first << "," << *(iter->second) << ") ";
 | 
|---|
| 225 | //    }
 | 
|---|
| 226 | //    LOG(2, "DEBUG: LookuptoSecond is " << lookupstream.str() << ".");
 | 
|---|
| 227 | //  }
 | 
|---|
| 228 | 
 | 
|---|
| 229 |   // now go through the first lookup as the second argument and pick the
 | 
|---|
| 230 |   // corresponding third argument by the matching index
 | 
|---|
| 231 |   std::vector<FunctionModel::arguments_t> results;
 | 
|---|
| 232 |   for (IndexLookup_t::const_iterator iter = LookuptoFirst.begin();
 | 
|---|
| 233 |       iter != LookuptoFirst.end();
 | 
|---|
| 234 |       ++iter) {
 | 
|---|
| 235 |     IndexLookup_t::const_iterator otheriter = LookuptoSecond.find(iter->first);
 | 
|---|
| 236 |     ASSERT( otheriter != LookuptoSecond.end(),
 | 
|---|
| 237 |         "triplefunction() - cannot find index "+toString(iter->first)
 | 
|---|
| 238 |         +" in LookupToSecond");
 | 
|---|
| 239 |     FunctionModel::arguments_t result(1, argument);
 | 
|---|
| 240 |     result.reserve(3);
 | 
|---|
| 241 |     result.push_back(*(iter->second));
 | 
|---|
| 242 |     result.push_back(*(otheriter->second));
 | 
|---|
| 243 |     results.push_back(result);
 | 
|---|
| 244 |   }
 | 
|---|
| 245 | 
 | 
|---|
| 246 |   return results;
 | 
|---|
| 247 | }
 | 
|---|
| 248 | 
 | 
|---|
| 249 | SaturationPotential::results_t
 | 
|---|
| 250 | SaturationPotential::operator()(
 | 
|---|
| 251 |     const arguments_t &arguments
 | 
|---|
| 252 |     ) const
 | 
|---|
| 253 | {
 | 
|---|
| 254 |   double result = 0.;
 | 
|---|
| 255 |   const ParticleTypes_t &morse_types = morse.getParticleTypes();
 | 
|---|
| 256 |   for(arguments_t::const_iterator argiter = arguments.begin();
 | 
|---|
| 257 |       argiter != arguments.end();
 | 
|---|
| 258 |       ++argiter) {
 | 
|---|
| 259 |     const argument_t &r_ij = *argiter;
 | 
|---|
| 260 |     if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1]))
 | 
|---|
| 261 |         || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) {
 | 
|---|
| 262 |       arguments_t args(1, r_ij);
 | 
|---|
| 263 | 
 | 
|---|
| 264 |       // Morse contribution
 | 
|---|
| 265 |       const double tmp = morse(args)[0];
 | 
|---|
| 266 | //      LOG(2, "DEBUG: Morse yields " << tmp << " for << " << r_ij << ".");
 | 
|---|
| 267 |       result += tmp;
 | 
|---|
| 268 |       if (result != result)
 | 
|---|
| 269 |         ELOG(1, "result is NAN.");
 | 
|---|
| 270 | 
 | 
|---|
| 271 |       // Angle contribution
 | 
|---|
| 272 |       {
 | 
|---|
| 273 |         typedef std::vector<FunctionModel::arguments_t> tripleargs_t;
 | 
|---|
| 274 |         tripleargs_t tripleargs =
 | 
|---|
| 275 |             triplefunction(r_ij, arguments);
 | 
|---|
| 276 |         for (tripleargs_t::const_iterator iter = tripleargs.begin();
 | 
|---|
| 277 |             iter != tripleargs.end();
 | 
|---|
| 278 |             ++iter) {
 | 
|---|
| 279 |           FunctionModel::arguments_t tempargs =
 | 
|---|
| 280 |               Extractors::reorderArgumentsByParticleTypes(*iter, angle.getParticleTypes());
 | 
|---|
| 281 |           const double tmp = angle(tempargs)[0];  // as we have all distances we get both jk and kj
 | 
|---|
| 282 | //          LOG(2, "DEBUG: angle yields " << tmp << " for << " << arguments << ".");
 | 
|---|
| 283 |           result += tmp;
 | 
|---|
| 284 |           if (result != result)
 | 
|---|
| 285 |             ELOG(1, "result is NAN.");
 | 
|---|
| 286 |         }
 | 
|---|
| 287 |       }
 | 
|---|
| 288 |     }
 | 
|---|
| 289 |   }
 | 
|---|
| 290 |   return std::vector<result_t>(1, energy_offset + result);
 | 
|---|
| 291 | }
 | 
|---|
| 292 | 
 | 
|---|
| 293 | SaturationPotential::derivative_components_t
 | 
|---|
| 294 | SaturationPotential::derivative(
 | 
|---|
| 295 |     const arguments_t &arguments
 | 
|---|
| 296 |     ) const
 | 
|---|
| 297 | {
 | 
|---|
| 298 |   ASSERT( 0,
 | 
|---|
| 299 |       "SaturationPotential::operator() - not implemented.");
 | 
|---|
| 300 |   derivative_components_t result;
 | 
|---|
| 301 |   return result;
 | 
|---|
| 302 | }
 | 
|---|
| 303 | 
 | 
|---|
| 304 | SaturationPotential::results_t
 | 
|---|
| 305 | SaturationPotential::parameter_derivative(
 | 
|---|
| 306 |     const arguments_t &arguments,
 | 
|---|
| 307 |     const size_t index
 | 
|---|
| 308 |     ) const
 | 
|---|
| 309 | {
 | 
|---|
| 310 |   double result = 0.;
 | 
|---|
| 311 |   if (index == all_energy_offset) {
 | 
|---|
| 312 |     result = 1.;
 | 
|---|
| 313 |   } else {
 | 
|---|
| 314 |     const ParticleTypes_t &morse_types = morse.getParticleTypes();
 | 
|---|
| 315 |     for(arguments_t::const_iterator argiter = arguments.begin();
 | 
|---|
| 316 |         argiter != arguments.end();
 | 
|---|
| 317 |         ++argiter) {
 | 
|---|
| 318 |       const argument_t &r_ij = *argiter;
 | 
|---|
| 319 |       if (((r_ij.types.first == morse_types[0]) && (r_ij.types.second == morse_types[1]))
 | 
|---|
| 320 |           || ((r_ij.types.first == morse_types[1]) && (r_ij.types.second == morse_types[0]))) {
 | 
|---|
| 321 |         arguments_t args(1, r_ij);
 | 
|---|
| 322 | 
 | 
|---|
| 323 |         switch (index) {
 | 
|---|
| 324 |           case morse_spring_constant:
 | 
|---|
| 325 |             result += morse.parameter_derivative(args, PairPotential_Morse::spring_constant)[0];
 | 
|---|
| 326 |             break;
 | 
|---|
| 327 |           case morse_equilibrium_distance:
 | 
|---|
| 328 |             result += morse.parameter_derivative(args, PairPotential_Morse::equilibrium_distance)[0];
 | 
|---|
| 329 |             break;
 | 
|---|
| 330 |           case morse_dissociation_energy:
 | 
|---|
| 331 |             result += morse.parameter_derivative(args, PairPotential_Morse::dissociation_energy)[0];
 | 
|---|
| 332 |             break;
 | 
|---|
| 333 |           case angle_spring_constant:
 | 
|---|
| 334 |           case angle_equilibrium_distance:
 | 
|---|
| 335 |           {
 | 
|---|
| 336 |             typedef std::vector<FunctionModel::arguments_t> tripleargs_t;
 | 
|---|
| 337 |             tripleargs_t tripleargs =
 | 
|---|
| 338 |                 triplefunction(r_ij, arguments);
 | 
|---|
| 339 |             for (tripleargs_t::const_iterator iter = tripleargs.begin();
 | 
|---|
| 340 |                 iter != tripleargs.end();
 | 
|---|
| 341 |                 ++iter) {
 | 
|---|
| 342 |               FunctionModel::arguments_t tempargs =
 | 
|---|
| 343 |                   Extractors::reorderArgumentsByParticleTypes(*iter, angle.getParticleTypes());
 | 
|---|
| 344 |               if (index == angle_spring_constant)
 | 
|---|
| 345 |                 result += angle.parameter_derivative(tempargs, PairPotential_Angle::spring_constant)[0];
 | 
|---|
| 346 |               else if (index == angle_equilibrium_distance)
 | 
|---|
| 347 |                 result += angle.parameter_derivative(tempargs, PairPotential_Angle::equilibrium_distance)[0];
 | 
|---|
| 348 | //              LOG(2, "DEBUG: angle yields " << tmp << " for << " << arguments << ".");
 | 
|---|
| 349 | //              result += tmp;
 | 
|---|
| 350 |               if (result != result)
 | 
|---|
| 351 |                 ELOG(1, "result is NAN.");
 | 
|---|
| 352 |             }
 | 
|---|
| 353 |           }
 | 
|---|
| 354 |             break;
 | 
|---|
| 355 |           default:
 | 
|---|
| 356 |             ASSERT( 0, "SaturationPotential::parameter_derivative() - impossible to get here.");
 | 
|---|
| 357 |             break;
 | 
|---|
| 358 |         }
 | 
|---|
| 359 |       }
 | 
|---|
| 360 |     }
 | 
|---|
| 361 |   }
 | 
|---|
| 362 |   return SaturationPotential::results_t(1, result);
 | 
|---|
| 363 | }
 | 
|---|
| 364 | 
 | 
|---|
| 365 | const SaturationPotential::ParticleTypes_t
 | 
|---|
| 366 | SaturationPotential::symmetrizeTypes(const ParticleTypes_t &_ParticleTypes)
 | 
|---|
| 367 | {
 | 
|---|
| 368 |   ASSERT( _ParticleTypes.size() == (size_t)2,
 | 
|---|
| 369 |       "SaturationPotential::symmetrizeTypes() - require initial _ParticleTypes with two elements.");
 | 
|---|
| 370 | //  // insert before couple
 | 
|---|
| 371 | //  ParticleTypes_t types(1, _ParticleTypes[1]);
 | 
|---|
| 372 | //  types.insert(types.end(), _ParticleTypes.begin(), _ParticleTypes.end());
 | 
|---|
| 373 |   // insert after the couple
 | 
|---|
| 374 |   ParticleTypes_t types(_ParticleTypes);
 | 
|---|
| 375 |   types.push_back( _ParticleTypes.back() );
 | 
|---|
| 376 |   ASSERT( types.size() == (size_t)3,
 | 
|---|
| 377 |       "SaturationPotential::symmetrizeTypes() - failed to generate three types for angle.");
 | 
|---|
| 378 |   return types;
 | 
|---|
| 379 | }
 | 
|---|
| 380 | 
 | 
|---|
| 381 | std::ostream& operator<<(std::ostream &ost, const SaturationPotential &potential)
 | 
|---|
| 382 | {
 | 
|---|
| 383 |   ost << potential.morse;
 | 
|---|
| 384 |   ost << potential.angle;
 | 
|---|
| 385 |   return ost;
 | 
|---|
| 386 | }
 | 
|---|
| 387 | 
 | 
|---|
| 388 | std::istream& operator>>(std::istream &ist, SaturationPotential &potential)
 | 
|---|
| 389 | {
 | 
|---|
| 390 |   ist >> potential.morse;
 | 
|---|
| 391 |   ist >> potential.angle;
 | 
|---|
| 392 |   return ist;
 | 
|---|
| 393 | }
 | 
|---|
| 394 | 
 | 
|---|
| 395 | FunctionModel::extractor_t
 | 
|---|
| 396 | SaturationPotential::getFragmentSpecificExtractor(
 | 
|---|
| 397 |     const charges_t &charges) const
 | 
|---|
| 398 | {
 | 
|---|
| 399 |   ASSERT(charges.size() == (size_t)2,
 | 
|---|
| 400 |       "SaturationPotential::getFragmentSpecificExtractor() - requires 2 charges.");
 | 
|---|
| 401 |   FunctionModel::extractor_t returnfunction;
 | 
|---|
| 402 |   if (charges[0] == charges[1]) {
 | 
|---|
| 403 |     // In case both types are equal there is only a single pair of possible
 | 
|---|
| 404 |     // type combinations.
 | 
|---|
| 405 |      returnfunction =
 | 
|---|
| 406 |         boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 407 |             boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 408 |             boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 409 |             boost::cref(charges),
 | 
|---|
| 410 |             _2);
 | 
|---|
| 411 |   } else {
 | 
|---|
| 412 |     // we have to chain here a rather complex "tree" of functions
 | 
|---|
| 413 |     // as we only have a couple of ParticleTypes but need to get
 | 
|---|
| 414 |     // all possible three pairs of the set of the two types.
 | 
|---|
| 415 |     // Finally, we also need to arrange them in correct order
 | 
|---|
| 416 |     // (for PairPotentiale_Angle).
 | 
|---|
| 417 |     //charges_t firstpair(2, boost::cref(charges[0]));
 | 
|---|
| 418 |     // only that saturation potential never has its middle element twice!
 | 
|---|
| 419 |     // hence, we skip the firstpair but keep the code for later generalization
 | 
|---|
| 420 |     charges_t secondpair(2, boost::cref(charges[1]));
 | 
|---|
| 421 |     const charges_t &thirdpair = charges;
 | 
|---|
| 422 |     returnfunction =
 | 
|---|
| 423 | //        boost::bind(&Extractors::reorderArgumentsByParticleTypes,
 | 
|---|
| 424 |           boost::bind(&Extractors::combineArguments,
 | 
|---|
| 425 | //            boost::bind(&Extractors::combineArguments,
 | 
|---|
| 426 | //              boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 427 | //                  boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 428 | //                  boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 429 | //                  firstpair,  // no crefs here as are temporaries!
 | 
|---|
| 430 | //                  _2),
 | 
|---|
| 431 |               boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 432 |                   boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 433 |                   boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 434 |                   secondpair,  // no crefs here as are temporaries!
 | 
|---|
| 435 |                   _2),
 | 
|---|
| 436 | //            ),
 | 
|---|
| 437 |             boost::bind(&Extractors::gatherAllDistancesFromFragment,
 | 
|---|
| 438 |                 boost::bind(&Fragment::getPositions, _1),
 | 
|---|
| 439 |                 boost::bind(&Fragment::getCharges, _1),
 | 
|---|
| 440 |                 boost::cref(thirdpair), // only the last one is no temporary
 | 
|---|
| 441 |                 _2)
 | 
|---|
| 442 |           );
 | 
|---|
| 443 | //          boost::cref(angle.getParticleTypes())
 | 
|---|
| 444 | //        );
 | 
|---|
| 445 | }
 | 
|---|
| 446 |   return returnfunction;
 | 
|---|
| 447 | }
 | 
|---|
| 448 | 
 | 
|---|
| 449 | void
 | 
|---|
| 450 | SaturationPotential::setParametersToRandomInitialValues(
 | 
|---|
| 451 |     const TrainingData &data)
 | 
|---|
| 452 | {
 | 
|---|
| 453 |   energy_offset = data.getTrainingOutputAverage()[0];
 | 
|---|
| 454 |   morse.setParametersToRandomInitialValues(data);
 | 
|---|
| 455 |   angle.setParametersToRandomInitialValues(data);
 | 
|---|
| 456 | }
 | 
|---|