[f06d52] | 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 <fstream>
|
---|
| 44 | #include <iostream>
|
---|
| 45 | #include <iterator>
|
---|
| 46 | #include <vector>
|
---|
| 47 |
|
---|
| 48 | #include <levmar.h>
|
---|
| 49 |
|
---|
| 50 | #include "CodePatterns/Assert.hpp"
|
---|
| 51 | #include "CodePatterns/Log.hpp"
|
---|
| 52 |
|
---|
| 53 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 54 |
|
---|
| 55 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
---|
| 56 | #include "Fragmentation/SetValues/Fragment.hpp"
|
---|
[c62f96] | 57 | #include "FunctionApproximation/FunctionApproximation.hpp"
|
---|
| 58 | #include "FunctionApproximation/FunctionModel.hpp"
|
---|
[155cc2] | 59 | #include "Potentials/Specifics/PairPotential_Morse.hpp"
|
---|
[f06d52] | 60 |
|
---|
| 61 | namespace po = boost::program_options;
|
---|
| 62 |
|
---|
| 63 | HomologyGraph getFirstGraphWithTwoCarbons(const HomologyContainer &homologies)
|
---|
| 64 | {
|
---|
| 65 | FragmentNode SaturatedCarbon(6,4); // carbon has atomic number 6 and should have 4 bonds for C2H6
|
---|
| 66 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
| 67 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
| 68 | if (iter->first.hasNode(SaturatedCarbon,2))
|
---|
| 69 | return iter->first;
|
---|
| 70 | }
|
---|
| 71 | return HomologyGraph();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | int main(int argc, char **argv)
|
---|
| 76 | {
|
---|
| 77 | std::cout << "Hello to the World from LevMar!" << std::endl;
|
---|
| 78 |
|
---|
| 79 | // load homology file
|
---|
| 80 | po::options_description desc("Allowed options");
|
---|
| 81 | desc.add_options()
|
---|
| 82 | ("help", "produce help message")
|
---|
| 83 | ("homology-file", po::value< boost::filesystem::path >(), "homology file to parse")
|
---|
| 84 | ;
|
---|
| 85 |
|
---|
| 86 | po::variables_map vm;
|
---|
| 87 | po::store(po::parse_command_line(argc, argv, desc), vm);
|
---|
| 88 | po::notify(vm);
|
---|
| 89 |
|
---|
| 90 | if (vm.count("help")) {
|
---|
| 91 | std::cout << desc << "\n";
|
---|
| 92 | return 1;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | boost::filesystem::path homology_file;
|
---|
| 96 | if (vm.count("homology-file")) {
|
---|
| 97 | homology_file = vm["homology-file"].as<boost::filesystem::path>();
|
---|
| 98 | LOG(1, "INFO: Parsing " << homology_file.string() << ".");
|
---|
| 99 | } else {
|
---|
| 100 | LOG(0, "homology-file level was not set.");
|
---|
| 101 | }
|
---|
| 102 | HomologyContainer homologies;
|
---|
| 103 | if (boost::filesystem::exists(homology_file)) {
|
---|
| 104 | std::ifstream returnstream(homology_file.string().c_str());
|
---|
| 105 | if (returnstream.good()) {
|
---|
| 106 | boost::archive::text_iarchive ia(returnstream);
|
---|
| 107 | ia >> homologies;
|
---|
| 108 | } else {
|
---|
| 109 | ELOG(2, "Failed to parse from " << homology_file.string() << ".");
|
---|
| 110 | }
|
---|
| 111 | returnstream.close();
|
---|
| 112 | } else {
|
---|
| 113 | ELOG(0, homology_file << " does not exist.");
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | // first we try to look into the HomologyContainer
|
---|
| 117 | LOG(1, "INFO: Listing all present homologies ...");
|
---|
| 118 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
| 119 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
| 120 | LOG(1, "INFO: graph " << iter->first << " has Fragment "
|
---|
| 121 | << iter->second.first << " and associated energy " << iter->second.second << ".");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // then we ought to pick the right HomologyGraph ...
|
---|
| 125 | const HomologyGraph graph = getFirstGraphWithTwoCarbons(homologies);
|
---|
| 126 | LOG(1, "First representative graph containing two saturated carbons is " << graph << ".");
|
---|
| 127 |
|
---|
| 128 | // Afterwards we go through all of this type and gather the distance and the energy value
|
---|
[c62f96] | 129 | typedef std::pair<
|
---|
| 130 | FunctionApproximation::inputs_t,
|
---|
| 131 | FunctionApproximation::outputs_t> InputOutputVector_t;
|
---|
| 132 | InputOutputVector_t DistanceEnergyVector;
|
---|
[f06d52] | 133 | std::pair<HomologyContainer::const_iterator, HomologyContainer::const_iterator> range =
|
---|
| 134 | homologies.getHomologousGraphs(graph);
|
---|
| 135 | for (HomologyContainer::const_iterator iter = range.first; iter != range.second; ++iter) {
|
---|
| 136 | // get distance out of Fragment
|
---|
| 137 | const Fragment &fragment = iter->second.first;
|
---|
| 138 | const Fragment::charges_t charges = fragment.getCharges();
|
---|
| 139 | const Fragment::positions_t positions = fragment.getPositions();
|
---|
[c62f96] | 140 | std::vector< std::pair<Vector, size_t> > DistanceVectors;
|
---|
[f06d52] | 141 | for (Fragment::charges_t::const_iterator chargeiter = charges.begin();
|
---|
| 142 | chargeiter != charges.end(); ++chargeiter) {
|
---|
| 143 | if (*chargeiter == 6) {
|
---|
| 144 | Fragment::positions_t::const_iterator positer = positions.begin();
|
---|
| 145 | const size_t steps = std::distance(charges.begin(), chargeiter);
|
---|
| 146 | std::advance(positer, steps);
|
---|
[c62f96] | 147 | DistanceVectors.push_back(
|
---|
| 148 | std::make_pair(Vector((*positer)[0], (*positer)[1], (*positer)[2]),
|
---|
| 149 | steps));
|
---|
[f06d52] | 150 | }
|
---|
| 151 | }
|
---|
[c62f96] | 152 | if (DistanceVectors.size() == (size_t)2) {
|
---|
| 153 | argument_t arg;
|
---|
| 154 | arg.indices.first = DistanceVectors[0].second;
|
---|
| 155 | arg.indices.second = DistanceVectors[1].second;
|
---|
| 156 | arg.distance = DistanceVectors[0].first.distance(DistanceVectors[1].first);
|
---|
| 157 | const double energy = iter->second.second;
|
---|
| 158 | DistanceEnergyVector.first.push_back( FunctionModel::arguments_t(1,arg) );
|
---|
| 159 | DistanceEnergyVector.second.push_back( FunctionModel::results_t(1,energy) );
|
---|
| 160 | } else {
|
---|
| 161 | ELOG(2, "main() - found not exactly two carbon atoms in fragment "
|
---|
| 162 | << fragment << ".");
|
---|
| 163 | }
|
---|
[f06d52] | 164 | }
|
---|
[c62f96] | 165 | // print training data for debugging
|
---|
| 166 | {
|
---|
| 167 | LOG(1, "INFO: I gathered the following (" << DistanceEnergyVector.first.size()
|
---|
| 168 | << "," << DistanceEnergyVector.second.size() << ") data pairs: ");
|
---|
| 169 | FunctionApproximation::inputs_t::const_iterator initer = DistanceEnergyVector.first.begin();
|
---|
| 170 | FunctionApproximation::outputs_t::const_iterator outiter = DistanceEnergyVector.second.begin();
|
---|
| 171 | for (; initer != DistanceEnergyVector.first.end(); ++initer, ++outiter) {
|
---|
| 172 | LOG(1, "INFO: (" << (*initer)[0].indices.first << "," << (*initer)[0].indices.second
|
---|
| 173 | << ") " << (*initer)[0].distance << " with energy " << *outiter);
|
---|
| 174 | }
|
---|
[f06d52] | 175 | }
|
---|
| 176 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
---|
| 177 |
|
---|
[dac89b1] | 178 | // now perform the function approximation by optimizing the model function
|
---|
[155cc2] | 179 | PairPotential_Morse morse(1., 2.9, 0.5, -80.);
|
---|
| 180 | FunctionModel &model = morse;
|
---|
[dac89b1] | 181 | FunctionApproximation approximator(1, 1, model);
|
---|
| 182 | approximator.setTrainingData(DistanceEnergyVector.first,DistanceEnergyVector.second);
|
---|
[c62f96] | 183 | approximator();
|
---|
[dac89b1] | 184 | const FunctionModel::parameters_t params = model.getParameters();
|
---|
[f06d52] | 185 |
|
---|
[65c42c] | 186 | LOG(0, "RESULT: Best parameters are " << params[0] << ","
|
---|
[155cc2] | 187 | << params[1] << "," << params[2] << " and " << params[3] << ".");
|
---|
[f06d52] | 188 |
|
---|
| 189 | return 0;
|
---|
| 190 | }
|
---|