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