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