| 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 | * LevMartester.cpp
|
|---|
| 26 | *
|
|---|
| 27 | * Created on: Sep 27, 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 <boost/archive/text_iarchive.hpp>
|
|---|
| 38 |
|
|---|
| 39 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 40 |
|
|---|
| 41 | #include <boost/assign.hpp>
|
|---|
| 42 | #include <boost/bind.hpp>
|
|---|
| 43 | #include <boost/filesystem.hpp>
|
|---|
| 44 | #include <boost/function.hpp>
|
|---|
| 45 | #include <boost/program_options.hpp>
|
|---|
| 46 |
|
|---|
| 47 | #include <cstdlib>
|
|---|
| 48 | #include <ctime>
|
|---|
| 49 | #include <fstream>
|
|---|
| 50 | #include <iostream>
|
|---|
| 51 | #include <iterator>
|
|---|
| 52 | #include <list>
|
|---|
| 53 | #include <vector>
|
|---|
| 54 |
|
|---|
| 55 | #include <levmar.h>
|
|---|
| 56 |
|
|---|
| 57 | #include "CodePatterns/Assert.hpp"
|
|---|
| 58 | #include "CodePatterns/Log.hpp"
|
|---|
| 59 |
|
|---|
| 60 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 61 |
|
|---|
| 62 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
|---|
| 63 | #include "Fragmentation/SetValues/Fragment.hpp"
|
|---|
| 64 | #include "FunctionApproximation/Extractors.hpp"
|
|---|
| 65 | #include "FunctionApproximation/FunctionApproximation.hpp"
|
|---|
| 66 | #include "FunctionApproximation/FunctionModel.hpp"
|
|---|
| 67 | #include "FunctionApproximation/TrainingData.hpp"
|
|---|
| 68 | #include "Helpers/defs.hpp"
|
|---|
| 69 | #include "Potentials/Specifics/PairPotential_Morse.hpp"
|
|---|
| 70 | #include "Potentials/Specifics/PairPotential_Angle.hpp"
|
|---|
| 71 | #include "Potentials/Specifics/SaturationPotential.hpp"
|
|---|
| 72 |
|
|---|
| 73 | namespace po = boost::program_options;
|
|---|
| 74 |
|
|---|
| 75 | using namespace boost::assign;
|
|---|
| 76 |
|
|---|
| 77 | HomologyGraph getFirstGraphWithThreeCarbons(const HomologyContainer &homologies)
|
|---|
| 78 | {
|
|---|
| 79 | FragmentNode SaturatedCarbon(6,4); // carbon has atomic number 6 and should have 4 bonds for C3H8
|
|---|
| 80 | FragmentNode DanglingCarbon(6,3); // carbon has atomic number 6 and should have 3 pure bonds for C3H8
|
|---|
| 81 | for (HomologyContainer::container_t::const_iterator iter =
|
|---|
| 82 | homologies.begin(); iter != homologies.end(); ++iter) {
|
|---|
| 83 | if ((iter->first.hasNode(SaturatedCarbon,2)) && (iter->first.hasNode(DanglingCarbon,1)))
|
|---|
| 84 | return iter->first;
|
|---|
| 85 | }
|
|---|
| 86 | return HomologyGraph();
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | HomologyGraph getFirstGraphWithTwoCarbons(const HomologyContainer &homologies)
|
|---|
| 90 | {
|
|---|
| 91 | FragmentNode SaturatedCarbon(6,3); // carbon has atomic number 6 and should have 4 bonds for C2H6
|
|---|
| 92 | for (HomologyContainer::container_t::const_iterator iter =
|
|---|
| 93 | homologies.begin(); iter != homologies.end(); ++iter) {
|
|---|
| 94 | if (iter->first.hasNode(SaturatedCarbon,2))
|
|---|
| 95 | return iter->first;
|
|---|
| 96 | }
|
|---|
| 97 | return HomologyGraph();
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | HomologyGraph getFirstGraphWithOneCarbon(const HomologyContainer &homologies)
|
|---|
| 101 | {
|
|---|
| 102 | FragmentNode SaturatedCarbon(6,2); // carbon has atomic number 6 and has 3 bonds (to other Hs)
|
|---|
| 103 | for (HomologyContainer::container_t::const_iterator iter =
|
|---|
| 104 | homologies.begin(); iter != homologies.end(); ++iter) {
|
|---|
| 105 | if (iter->first.hasNode(SaturatedCarbon,1))
|
|---|
| 106 | return iter->first;
|
|---|
| 107 | }
|
|---|
| 108 | return HomologyGraph();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | /** This function returns the elements of the sum over index "k" for an
|
|---|
| 113 | * argument containing indices "i" and "j"
|
|---|
| 114 | * @param inputs vector of all configuration (containing each a vector of all arguments)
|
|---|
| 115 | * @param arg argument containing indices "i" and "j"
|
|---|
| 116 | * @param cutoff cutoff criterion for sum over k
|
|---|
| 117 | * @return vector of argument pairs (a vector) of ik and jk for at least all k
|
|---|
| 118 | * within distance of \a cutoff to i
|
|---|
| 119 | */
|
|---|
| 120 | std::vector<FunctionModel::arguments_t>
|
|---|
| 121 | getTripleFromArgument(const FunctionApproximation::inputs_t &inputs, const argument_t &arg, const double cutoff)
|
|---|
| 122 | {
|
|---|
| 123 | typedef std::list<argument_t> arg_list_t;
|
|---|
| 124 | typedef std::map<size_t, arg_list_t > k_args_map_t;
|
|---|
| 125 | k_args_map_t tempresult;
|
|---|
| 126 | ASSERT( inputs.size() > arg.globalid,
|
|---|
| 127 | "getTripleFromArgument() - globalid "+toString(arg.globalid)
|
|---|
| 128 | +" is greater than all inputs "+toString(inputs.size())+".");
|
|---|
| 129 | const FunctionModel::arguments_t &listofargs = inputs[arg.globalid];
|
|---|
| 130 | for (FunctionModel::arguments_t::const_iterator argiter = listofargs.begin();
|
|---|
| 131 | argiter != listofargs.end();
|
|---|
| 132 | ++argiter) {
|
|---|
| 133 | // first index must be either i or j but second index not
|
|---|
| 134 | if (((argiter->indices.first == arg.indices.first)
|
|---|
| 135 | || (argiter->indices.first == arg.indices.second))
|
|---|
| 136 | && ((argiter->indices.second != arg.indices.first)
|
|---|
| 137 | && (argiter->indices.second != arg.indices.second))) {
|
|---|
| 138 | // we need arguments ik and jk
|
|---|
| 139 | std::pair< k_args_map_t::iterator, bool> inserter =
|
|---|
| 140 | tempresult.insert( std::make_pair( argiter->indices.second, arg_list_t(1,*argiter)));
|
|---|
| 141 | if (!inserter.second) {
|
|---|
| 142 | // is present one ik or jk, if ik insert jk at back
|
|---|
| 143 | if (inserter.first->second.begin()->indices.first == arg.indices.first)
|
|---|
| 144 | inserter.first->second.push_back(*argiter);
|
|---|
| 145 | else // if jk, insert ik at front
|
|---|
| 146 | inserter.first->second.push_front(*argiter);
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | // // or second index must be either i or j but first index not
|
|---|
| 150 | // else if (((argiter->indices.first != arg.indices.first)
|
|---|
| 151 | // && (argiter->indices.first != arg.indices.second))
|
|---|
| 152 | // && ((argiter->indices.second == arg.indices.first)
|
|---|
| 153 | // || (argiter->indices.second == arg.indices.second))) {
|
|---|
| 154 | // // we need arguments ki and kj
|
|---|
| 155 | // std::pair< k_args_map_t::iterator, bool> inserter =
|
|---|
| 156 | // tempresult.insert( std::make_pair( argiter->indices.first, arg_list_t(1,*argiter)));
|
|---|
| 157 | // if (!inserter.second) {
|
|---|
| 158 | // // is present one ki or kj, if ki insert kj at back
|
|---|
| 159 | // if (inserter.first->second.begin()->indices.second == arg.indices.first)
|
|---|
| 160 | // inserter.first->second.push_back(*argiter);
|
|---|
| 161 | // else // if kj, insert ki at front
|
|---|
| 162 | // inserter.first->second.push_front(*argiter);
|
|---|
| 163 | // }
|
|---|
| 164 | // }
|
|---|
| 165 | }
|
|---|
| 166 | // check that i,j are NOT contained
|
|---|
| 167 | ASSERT( tempresult.count(arg.indices.first) == 0,
|
|---|
| 168 | "getTripleFromArgument() - first index of argument present in k_args_map?");
|
|---|
| 169 | ASSERT( tempresult.count(arg.indices.second) == 0,
|
|---|
| 170 | "getTripleFromArgument() - first index of argument present in k_args_map?");
|
|---|
| 171 |
|
|---|
| 172 | // convert
|
|---|
| 173 | std::vector<FunctionModel::arguments_t> result;
|
|---|
| 174 | for (k_args_map_t::const_iterator iter = tempresult.begin();
|
|---|
| 175 | iter != tempresult.end();
|
|---|
| 176 | ++iter) {
|
|---|
| 177 | ASSERT( iter->second.size() == 2,
|
|---|
| 178 | "getTripleFromArgument() - for index "+toString(iter->first)+" we did not find both ik and jk.");
|
|---|
| 179 | result.push_back( FunctionModel::arguments_t(iter->second.begin(), iter->second.end()) );
|
|---|
| 180 | }
|
|---|
| 181 | return result;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | double
|
|---|
| 185 | function_angle(
|
|---|
| 186 | const double &r_ij,
|
|---|
| 187 | const double &r_ik,
|
|---|
| 188 | const double &r_jk
|
|---|
| 189 | )
|
|---|
| 190 | {
|
|---|
| 191 | // Info info(__func__);
|
|---|
| 192 | const double angle = pow(r_ij,2.) + pow(r_ik,2.) - pow(r_jk,2.);
|
|---|
| 193 | const double divisor = 2.* r_ij * r_ik;
|
|---|
| 194 |
|
|---|
| 195 | // LOG(2, "DEBUG: cos(theta)= " << angle/divisor);
|
|---|
| 196 | if (divisor == 0.)
|
|---|
| 197 | return 0.;
|
|---|
| 198 | else
|
|---|
| 199 | return angle/divisor;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | int main(int argc, char **argv)
|
|---|
| 203 | {
|
|---|
| 204 | std::cout << "Hello to the World from LevMar!" << std::endl;
|
|---|
| 205 |
|
|---|
| 206 | // load homology file
|
|---|
| 207 | po::options_description desc("Allowed options");
|
|---|
| 208 | desc.add_options()
|
|---|
| 209 | ("help", "produce help message")
|
|---|
| 210 | ("homology-file", po::value< boost::filesystem::path >(), "homology file to parse")
|
|---|
| 211 | ;
|
|---|
| 212 |
|
|---|
| 213 | po::variables_map vm;
|
|---|
| 214 | po::store(po::parse_command_line(argc, argv, desc), vm);
|
|---|
| 215 | po::notify(vm);
|
|---|
| 216 |
|
|---|
| 217 | if (vm.count("help")) {
|
|---|
| 218 | std::cout << desc << "\n";
|
|---|
| 219 | return 1;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | boost::filesystem::path homology_file;
|
|---|
| 223 | if (vm.count("homology-file")) {
|
|---|
| 224 | homology_file = vm["homology-file"].as<boost::filesystem::path>();
|
|---|
| 225 | LOG(1, "INFO: Parsing " << homology_file.string() << ".");
|
|---|
| 226 | } else {
|
|---|
| 227 | LOG(0, "homology-file level was not set.");
|
|---|
| 228 | }
|
|---|
| 229 | HomologyContainer homologies;
|
|---|
| 230 | if (boost::filesystem::exists(homology_file)) {
|
|---|
| 231 | std::ifstream returnstream(homology_file.string().c_str());
|
|---|
| 232 | if (returnstream.good()) {
|
|---|
| 233 | boost::archive::text_iarchive ia(returnstream);
|
|---|
| 234 | ia >> homologies;
|
|---|
| 235 | } else {
|
|---|
| 236 | ELOG(2, "Failed to parse from " << homology_file.string() << ".");
|
|---|
| 237 | }
|
|---|
| 238 | returnstream.close();
|
|---|
| 239 | } else {
|
|---|
| 240 | ELOG(0, homology_file << " does not exist.");
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | // first we try to look into the HomologyContainer
|
|---|
| 244 | LOG(1, "INFO: Listing all present homologies ...");
|
|---|
| 245 | for (HomologyContainer::container_t::const_iterator iter =
|
|---|
| 246 | homologies.begin(); iter != homologies.end(); ++iter) {
|
|---|
| 247 | LOG(1, "INFO: graph " << iter->first << " has Fragment "
|
|---|
| 248 | << iter->second.first << " and associated energy " << iter->second.second << ".");
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /******************** Angle TRAINING ********************/
|
|---|
| 252 | {
|
|---|
| 253 | // then we ought to pick the right HomologyGraph ...
|
|---|
| 254 | const HomologyGraph graph = getFirstGraphWithThreeCarbons(homologies);
|
|---|
| 255 | LOG(1, "First representative graph containing three saturated carbons is " << graph << ".");
|
|---|
| 256 |
|
|---|
| 257 | // Afterwards we go through all of this type and gather the distance and the energy value
|
|---|
| 258 | typedef std::pair<
|
|---|
| 259 | FunctionApproximation::inputs_t,
|
|---|
| 260 | FunctionApproximation::outputs_t> InputOutputVector_t;
|
|---|
| 261 | InputOutputVector_t DistanceEnergyVector;
|
|---|
| 262 | std::pair<HomologyContainer::const_iterator, HomologyContainer::const_iterator> range =
|
|---|
| 263 | homologies.getHomologousGraphs(graph);
|
|---|
| 264 | for (HomologyContainer::const_iterator fragiter = range.first; fragiter != range.second; ++fragiter) {
|
|---|
| 265 | // get distance out of Fragment
|
|---|
| 266 | const double &energy = fragiter->second.second;
|
|---|
| 267 | const Fragment &fragment = fragiter->second.first;
|
|---|
| 268 | const Fragment::charges_t charges = fragment.getCharges();
|
|---|
| 269 | const Fragment::positions_t positions = fragment.getPositions();
|
|---|
| 270 | std::vector< std::pair<Vector, size_t> > DistanceVectors;
|
|---|
| 271 | for (Fragment::charges_t::const_iterator chargeiter = charges.begin();
|
|---|
| 272 | chargeiter != charges.end(); ++chargeiter) {
|
|---|
| 273 | if (*chargeiter == 6) {
|
|---|
| 274 | Fragment::positions_t::const_iterator positer = positions.begin();
|
|---|
| 275 | const size_t steps = std::distance(charges.begin(), chargeiter);
|
|---|
| 276 | std::advance(positer, steps);
|
|---|
| 277 | DistanceVectors.push_back(
|
|---|
| 278 | std::make_pair(Vector((*positer)[0], (*positer)[1], (*positer)[2]),
|
|---|
| 279 | steps));
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 | if (DistanceVectors.size() == (size_t)3) {
|
|---|
| 283 | FunctionModel::arguments_t args(3);
|
|---|
| 284 | // we require specific ordering of the carbons: ij, ik, jk
|
|---|
| 285 | typedef std::vector< std::pair<size_t, size_t> > indices_t;
|
|---|
| 286 | indices_t indices;
|
|---|
| 287 | indices += std::make_pair(0,1), std::make_pair(0,2), std::make_pair(1,2);
|
|---|
| 288 | // create the three arguments
|
|---|
| 289 | for (indices_t::const_iterator iter = indices.begin(); iter != indices.end(); ++iter) {
|
|---|
| 290 | const size_t &firstindex = iter->first;
|
|---|
| 291 | const size_t &secondindex = iter->second;
|
|---|
| 292 | argument_t &arg = args[(size_t)std::distance(const_cast<const indices_t&>(indices).begin(), iter)];
|
|---|
| 293 | arg.indices.first = DistanceVectors[firstindex].second;
|
|---|
| 294 | arg.indices.second = DistanceVectors[secondindex].second;
|
|---|
| 295 | arg.distance = DistanceVectors[firstindex].first.distance(DistanceVectors[secondindex].first);
|
|---|
| 296 | arg.globalid = DistanceEnergyVector.first.size();
|
|---|
| 297 | }
|
|---|
| 298 | // make largest distance last to create correct angle
|
|---|
| 299 | // (this would normally depend on the order of the nodes in the subgraph)
|
|---|
| 300 | std::list<argument_t> sorted_args;
|
|---|
| 301 | double greatestdistance = 0.;
|
|---|
| 302 | for(FunctionModel::arguments_t::const_iterator iter = args.begin(); iter != args.end(); ++iter)
|
|---|
| 303 | greatestdistance = std::max(greatestdistance, iter->distance);
|
|---|
| 304 | for(FunctionModel::arguments_t::const_iterator iter = args.begin(); iter != args.end(); ++iter)
|
|---|
| 305 | if (iter->distance == greatestdistance)
|
|---|
| 306 | sorted_args.push_back(*iter);
|
|---|
| 307 | else
|
|---|
| 308 | sorted_args.push_front(*iter);
|
|---|
| 309 | // and add the training pair
|
|---|
| 310 | DistanceEnergyVector.first.push_back( FunctionModel::arguments_t(sorted_args.begin(), sorted_args.end()) );
|
|---|
| 311 | DistanceEnergyVector.second.push_back( FunctionModel::results_t(1,energy) );
|
|---|
| 312 | } else {
|
|---|
| 313 | ELOG(2, "main() - found not exactly three carbon atoms in fragment "
|
|---|
| 314 | << fragment << ".");
|
|---|
| 315 | }
|
|---|
| 316 | }
|
|---|
| 317 | // print training data for debugging
|
|---|
| 318 | {
|
|---|
| 319 | LOG(1, "INFO: I gathered the following (" << DistanceEnergyVector.first.size()
|
|---|
| 320 | << "," << DistanceEnergyVector.second.size() << ") data pairs: ");
|
|---|
| 321 | FunctionApproximation::inputs_t::const_iterator initer = DistanceEnergyVector.first.begin();
|
|---|
| 322 | FunctionApproximation::outputs_t::const_iterator outiter = DistanceEnergyVector.second.begin();
|
|---|
| 323 | for (; initer != DistanceEnergyVector.first.end(); ++initer, ++outiter) {
|
|---|
| 324 | std::stringstream stream;
|
|---|
| 325 | const double cos_angle = function_angle((*initer)[0].distance,(*initer)[1].distance,(*initer)[2].distance);
|
|---|
| 326 | for (size_t index = 0; index < (*initer).size(); ++index)
|
|---|
| 327 | stream << " (" << (*initer)[index].indices.first << "," << (*initer)[index].indices.second
|
|---|
| 328 | << ") " << (*initer)[index].distance;
|
|---|
| 329 | stream << " with energy " << *outiter << " and cos(angle) " << cos_angle;
|
|---|
| 330 | LOG(1, "INFO:" << stream.str());
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
|---|
| 334 |
|
|---|
| 335 | // now perform the function approximation by optimizing the model function
|
|---|
| 336 | FunctionModel::parameters_t params(PairPotential_Angle::MAXPARAMS, 0.);
|
|---|
| 337 | params[PairPotential_Angle::energy_offset] = -1.;
|
|---|
| 338 | params[PairPotential_Angle::spring_constant] = 1.;
|
|---|
| 339 | params[PairPotential_Angle::equilibrium_distance] = 0.2;
|
|---|
| 340 | PairPotential_Angle angle;
|
|---|
| 341 | LOG(0, "INFO: Initial parameters are " << params << ".");
|
|---|
| 342 | angle.setParameters(params);
|
|---|
| 343 | FunctionModel &model = angle;
|
|---|
| 344 | FunctionApproximation approximator(
|
|---|
| 345 | DistanceEnergyVector.first.begin()->size(),
|
|---|
| 346 | DistanceEnergyVector.second.begin()->size(),
|
|---|
| 347 | model);
|
|---|
| 348 | approximator.setTrainingData(DistanceEnergyVector.first,DistanceEnergyVector.second);
|
|---|
| 349 | if (model.isBoxConstraint() && approximator.checkParameterDerivatives())
|
|---|
| 350 | approximator(FunctionApproximation::ParameterDerivative);
|
|---|
| 351 | else
|
|---|
| 352 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
|---|
| 353 | params = model.getParameters();
|
|---|
| 354 |
|
|---|
| 355 | LOG(0, "RESULT: Best parameters are " << params << ".");
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | /******************** MORSE TRAINING ********************/
|
|---|
| 359 | {
|
|---|
| 360 | // then we ought to pick the right HomologyGraph ...
|
|---|
| 361 | const HomologyGraph graph = getFirstGraphWithTwoCarbons(homologies);
|
|---|
| 362 | LOG(1, "First representative graph containing two saturated carbons is " << graph << ".");
|
|---|
| 363 |
|
|---|
| 364 | // Afterwards we go through all of this type and gather the distance and the energy value
|
|---|
| 365 | TrainingData MorseData(
|
|---|
| 366 | boost::bind(&Extractors::_detail::gatherAllDistanceArguments,
|
|---|
| 367 | boost::bind(&Extractors::gatherDistanceOfTuples,
|
|---|
| 368 | _1, Fragment::charges_t(2,6.)
|
|---|
| 369 | ), _2 // gather first carbon pair
|
|---|
| 370 | )
|
|---|
| 371 | );
|
|---|
| 372 | MorseData(homologies.getHomologousGraphs(graph));
|
|---|
| 373 | LOG(1, "INFO: I gathered the following training data: " << MorseData);
|
|---|
| 374 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
|---|
| 375 |
|
|---|
| 376 | // now perform the function approximation by optimizing the model function
|
|---|
| 377 | FunctionModel::parameters_t params(PairPotential_Morse::MAXPARAMS, 0.);
|
|---|
| 378 | params[PairPotential_Morse::dissociation_energy] = 0.5;
|
|---|
| 379 | params[PairPotential_Morse::energy_offset] = -1.;
|
|---|
| 380 | params[PairPotential_Morse::spring_constant] = 1.;
|
|---|
| 381 | params[PairPotential_Morse::equilibrium_distance] = 2.9;
|
|---|
| 382 | PairPotential_Morse morse;
|
|---|
| 383 | morse.setParameters(params);
|
|---|
| 384 | FunctionModel &model = morse;
|
|---|
| 385 | FunctionApproximation approximator(MorseData, model); // we only give CC distance, hence 1 input dim
|
|---|
| 386 | if (model.isBoxConstraint() && approximator.checkParameterDerivatives())
|
|---|
| 387 | approximator(FunctionApproximation::ParameterDerivative);
|
|---|
| 388 | else
|
|---|
| 389 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
|---|
| 390 | params = model.getParameters();
|
|---|
| 391 |
|
|---|
| 392 | LOG(0, "RESULT: Best parameters are " << params << ".");
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /******************* SATURATION TRAINING *******************/
|
|---|
| 396 | FunctionModel::parameters_t params(SaturationPotential::MAXPARAMS, 0.);
|
|---|
| 397 | {
|
|---|
| 398 | // then we ought to pick the right HomologyGraph ...
|
|---|
| 399 | const HomologyGraph graph = getFirstGraphWithOneCarbon(homologies);
|
|---|
| 400 | LOG(1, "First representative graph containing one saturated carbon is " << graph << ".");
|
|---|
| 401 |
|
|---|
| 402 | // Afterwards we go through all of this type and gather the distance and the energy value
|
|---|
| 403 | TrainingData TersoffData(
|
|---|
| 404 | TrainingData::extractor_t(&Extractors::gatherAllDistances) // gather first carbon pair
|
|---|
| 405 | );
|
|---|
| 406 | TersoffData( homologies.getHomologousGraphs(graph) );
|
|---|
| 407 | LOG(1, "INFO: I gathered the following training data: " << TersoffData);
|
|---|
| 408 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
|---|
| 409 |
|
|---|
| 410 | // now perform the function approximation by optimizing the model function
|
|---|
| 411 | boost::function< std::vector<FunctionModel::arguments_t>(const argument_t &, const double)> triplefunction =
|
|---|
| 412 | boost::bind(&getTripleFromArgument, boost::cref(TersoffData.getTrainingInputs()), _1, _2);
|
|---|
| 413 | srand((unsigned)time(0)); // seed with current time
|
|---|
| 414 | LOG(0, "INFO: Initial parameters are " << params << ".");
|
|---|
| 415 |
|
|---|
| 416 | SaturationPotential saturation(triplefunction);
|
|---|
| 417 | saturation.setParameters(params);
|
|---|
| 418 | FunctionModel &model = saturation;
|
|---|
| 419 | FunctionApproximation approximator(TersoffData, model); // CH4 has 5 atoms, hence 5*4/2 distances
|
|---|
| 420 | if (model.isBoxConstraint() && approximator.checkParameterDerivatives())
|
|---|
| 421 | approximator(FunctionApproximation::ParameterDerivative);
|
|---|
| 422 | else
|
|---|
| 423 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
|---|
| 424 |
|
|---|
| 425 | params = model.getParameters();
|
|---|
| 426 |
|
|---|
| 427 | LOG(0, "RESULT: Best parameters are " << params << ".");
|
|---|
| 428 |
|
|---|
| 429 | // std::cout << "\tsaturationparticle:";
|
|---|
| 430 | // std::cout << "\tparticle_type=C,";
|
|---|
| 431 | // std::cout << "\tA=" << params[SaturationPotential::A] << ",";
|
|---|
| 432 | // std::cout << "\tB=" << params[SaturationPotential::B] << ",";
|
|---|
| 433 | // std::cout << "\tlambda=" << params[SaturationPotential::lambda] << ",";
|
|---|
| 434 | // std::cout << "\tmu=" << params[SaturationPotential::mu] << ",";
|
|---|
| 435 | // std::cout << "\tbeta=" << params[SaturationPotential::beta] << ",";
|
|---|
| 436 | // std::cout << "\tn=" << params[SaturationPotential::n] << ",";
|
|---|
| 437 | // std::cout << "\tc=" << params[SaturationPotential::c] << ",";
|
|---|
| 438 | // std::cout << "\td=" << params[SaturationPotential::d] << ",";
|
|---|
| 439 | // std::cout << "\th=" << params[SaturationPotential::h] << ",";
|
|---|
| 440 | //// std::cout << "\toffset=" << params[SaturationPotential::offset] << ",";
|
|---|
| 441 | // std::cout << "\tR=" << saturation.R << ",";
|
|---|
| 442 | // std::cout << "\tS=" << saturation.S << ";";
|
|---|
| 443 | // std::cout << std::endl;
|
|---|
| 444 |
|
|---|
| 445 | // check L2 and Lmax error against training set
|
|---|
| 446 | LOG(1, "INFO: L2sum = " << TersoffData.getL2Error(model)
|
|---|
| 447 | << ", LMax = " << TersoffData.getLMaxError(model) << ".");
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | return 0;
|
|---|
| 451 | }
|
|---|