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/assign/list_of.hpp>
|
---|
43 | #include <boost/bind.hpp>
|
---|
44 | #include <boost/filesystem.hpp>
|
---|
45 | #include <boost/function.hpp>
|
---|
46 | #include <boost/program_options.hpp>
|
---|
47 |
|
---|
48 | #include <cstdlib>
|
---|
49 | #include <ctime>
|
---|
50 | #include <fstream>
|
---|
51 | #include <iostream>
|
---|
52 | #include <iterator>
|
---|
53 | #include <list>
|
---|
54 | #include <vector>
|
---|
55 |
|
---|
56 | #include <levmar.h>
|
---|
57 |
|
---|
58 | #include "CodePatterns/Assert.hpp"
|
---|
59 | #include "CodePatterns/Log.hpp"
|
---|
60 |
|
---|
61 | #include "LinearAlgebra/Vector.hpp"
|
---|
62 |
|
---|
63 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
---|
64 | #include "Fragmentation/SetValues/Fragment.hpp"
|
---|
65 | #include "FunctionApproximation/Extractors.hpp"
|
---|
66 | #include "FunctionApproximation/FunctionApproximation.hpp"
|
---|
67 | #include "FunctionApproximation/FunctionModel.hpp"
|
---|
68 | #include "FunctionApproximation/TrainingData.hpp"
|
---|
69 | #include "FunctionApproximation/writeDistanceEnergyTable.hpp"
|
---|
70 | #include "Helpers/defs.hpp"
|
---|
71 | #include "Potentials/PotentialFactory.hpp"
|
---|
72 | #include "Potentials/PotentialRegistry.hpp"
|
---|
73 | #include "Potentials/Specifics/PairPotential_Morse.hpp"
|
---|
74 | #include "Potentials/Specifics/PairPotential_Angle.hpp"
|
---|
75 | #include "Potentials/Specifics/SaturationPotential.hpp"
|
---|
76 | #include "types.hpp"
|
---|
77 |
|
---|
78 | namespace po = boost::program_options;
|
---|
79 |
|
---|
80 | using namespace boost::assign;
|
---|
81 |
|
---|
82 | HomologyGraph getFirstGraphwithTimesSpecificElement(
|
---|
83 | const HomologyContainer &homologies,
|
---|
84 | const size_t _number,
|
---|
85 | const size_t _times)
|
---|
86 | {
|
---|
87 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
88 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
89 | if (iter->first.hasTimesAtomicNumber(_number,_times))
|
---|
90 | return iter->first;
|
---|
91 | }
|
---|
92 | return HomologyGraph();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** This function returns the elements of the sum over index "k" for an
|
---|
96 | * argument containing indices "i" and "j"
|
---|
97 | * @param inputs vector of all configuration (containing each a vector of all arguments)
|
---|
98 | * @param arg argument containing indices "i" and "j"
|
---|
99 | * @param cutoff cutoff criterion for sum over k
|
---|
100 | * @return vector of argument pairs (a vector) of ik and jk for at least all k
|
---|
101 | * within distance of \a cutoff to i
|
---|
102 | */
|
---|
103 | std::vector<FunctionModel::arguments_t>
|
---|
104 | getTripleFromArgument(const FunctionApproximation::inputs_t &inputs, const argument_t &arg, const double cutoff)
|
---|
105 | {
|
---|
106 | typedef std::list<argument_t> arg_list_t;
|
---|
107 | typedef std::map<size_t, arg_list_t > k_args_map_t;
|
---|
108 | k_args_map_t tempresult;
|
---|
109 | ASSERT( inputs.size() > arg.globalid,
|
---|
110 | "getTripleFromArgument() - globalid "+toString(arg.globalid)
|
---|
111 | +" is greater than all inputs "+toString(inputs.size())+".");
|
---|
112 | const FunctionModel::arguments_t &listofargs = inputs[arg.globalid];
|
---|
113 | for (FunctionModel::arguments_t::const_iterator argiter = listofargs.begin();
|
---|
114 | argiter != listofargs.end();
|
---|
115 | ++argiter) {
|
---|
116 | // first index must be either i or j but second index not
|
---|
117 | if (((argiter->indices.first == arg.indices.first)
|
---|
118 | || (argiter->indices.first == arg.indices.second))
|
---|
119 | && ((argiter->indices.second != arg.indices.first)
|
---|
120 | && (argiter->indices.second != arg.indices.second))) {
|
---|
121 | // we need arguments ik and jk
|
---|
122 | std::pair< k_args_map_t::iterator, bool> inserter =
|
---|
123 | tempresult.insert( std::make_pair( argiter->indices.second, arg_list_t(1,*argiter)));
|
---|
124 | if (!inserter.second) {
|
---|
125 | // is present one ik or jk, if ik insert jk at back
|
---|
126 | if (inserter.first->second.begin()->indices.first == arg.indices.first)
|
---|
127 | inserter.first->second.push_back(*argiter);
|
---|
128 | else // if jk, insert ik at front
|
---|
129 | inserter.first->second.push_front(*argiter);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | // // or second index must be either i or j but first index not
|
---|
133 | // else if (((argiter->indices.first != arg.indices.first)
|
---|
134 | // && (argiter->indices.first != arg.indices.second))
|
---|
135 | // && ((argiter->indices.second == arg.indices.first)
|
---|
136 | // || (argiter->indices.second == arg.indices.second))) {
|
---|
137 | // // we need arguments ki and kj
|
---|
138 | // std::pair< k_args_map_t::iterator, bool> inserter =
|
---|
139 | // tempresult.insert( std::make_pair( argiter->indices.first, arg_list_t(1,*argiter)));
|
---|
140 | // if (!inserter.second) {
|
---|
141 | // // is present one ki or kj, if ki insert kj at back
|
---|
142 | // if (inserter.first->second.begin()->indices.second == arg.indices.first)
|
---|
143 | // inserter.first->second.push_back(*argiter);
|
---|
144 | // else // if kj, insert ki at front
|
---|
145 | // inserter.first->second.push_front(*argiter);
|
---|
146 | // }
|
---|
147 | // }
|
---|
148 | }
|
---|
149 | // check that i,j are NOT contained
|
---|
150 | ASSERT( tempresult.count(arg.indices.first) == 0,
|
---|
151 | "getTripleFromArgument() - first index of argument present in k_args_map?");
|
---|
152 | ASSERT( tempresult.count(arg.indices.second) == 0,
|
---|
153 | "getTripleFromArgument() - first index of argument present in k_args_map?");
|
---|
154 |
|
---|
155 | // convert
|
---|
156 | std::vector<FunctionModel::arguments_t> result;
|
---|
157 | for (k_args_map_t::const_iterator iter = tempresult.begin();
|
---|
158 | iter != tempresult.end();
|
---|
159 | ++iter) {
|
---|
160 | ASSERT( iter->second.size() == 2,
|
---|
161 | "getTripleFromArgument() - for index "+toString(iter->first)+" we did not find both ik and jk.");
|
---|
162 | result.push_back( FunctionModel::arguments_t(iter->second.begin(), iter->second.end()) );
|
---|
163 | }
|
---|
164 | return result;
|
---|
165 | }
|
---|
166 |
|
---|
167 | int main(int argc, char **argv)
|
---|
168 | {
|
---|
169 | std::cout << "Hello to the World from LevMar!" << std::endl;
|
---|
170 |
|
---|
171 | // load homology file
|
---|
172 | po::options_description desc("Allowed options");
|
---|
173 | desc.add_options()
|
---|
174 | ("help", "produce help message")
|
---|
175 | ("homology-file", po::value< boost::filesystem::path >(), "homology file to parse")
|
---|
176 | ("fit-potential", po::value< std::string >(), "potential type to fit")
|
---|
177 | ("charges", po::value< FunctionModel::charges_t >()->multitoken(), "charges specifying the fragment")
|
---|
178 | ;
|
---|
179 |
|
---|
180 | po::variables_map vm;
|
---|
181 | po::store(po::parse_command_line(argc, argv, desc), vm);
|
---|
182 | po::notify(vm);
|
---|
183 |
|
---|
184 | if (vm.count("help")) {
|
---|
185 | std::cout << desc << "\n";
|
---|
186 | return 1;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // homology-file
|
---|
190 | boost::filesystem::path homology_file;
|
---|
191 | if (vm.count("homology-file")) {
|
---|
192 | homology_file = vm["homology-file"].as<boost::filesystem::path>();
|
---|
193 | LOG(1, "INFO: Parsing " << homology_file.string() << ".");
|
---|
194 | } else {
|
---|
195 | ELOG(0, "homology file (homology-file) was not set.");
|
---|
196 | return 1;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // type of potential to fit
|
---|
200 | std::string potentialtype;
|
---|
201 | if (vm.count("fit-potential")) {
|
---|
202 | potentialtype = vm["fit-potential"].as<std::string>();
|
---|
203 | } else {
|
---|
204 | ELOG(0, "potential type to fit (fit-potential) was not set.");
|
---|
205 | return 1;
|
---|
206 | }
|
---|
207 |
|
---|
208 | // charges
|
---|
209 | FunctionModel::charges_t charges;
|
---|
210 | if (vm.count("charges")) {
|
---|
211 | charges = vm["charges"].as< FunctionModel::charges_t >();
|
---|
212 | } else {
|
---|
213 | ELOG(0, "Vector of charges specifying the fragment (charges) was not set.");
|
---|
214 | return 1;
|
---|
215 | }
|
---|
216 |
|
---|
217 | // parse homologies into container
|
---|
218 | HomologyContainer homologies;
|
---|
219 | if (boost::filesystem::exists(homology_file)) {
|
---|
220 | std::ifstream returnstream(homology_file.string().c_str());
|
---|
221 | if (returnstream.good()) {
|
---|
222 | boost::archive::text_iarchive ia(returnstream);
|
---|
223 | ia >> homologies;
|
---|
224 | } else {
|
---|
225 | ELOG(0, "Failed to parse from " << homology_file.string() << ".");
|
---|
226 | return 1;
|
---|
227 | }
|
---|
228 | returnstream.close();
|
---|
229 | } else {
|
---|
230 | ELOG(0, homology_file << " does not exist.");
|
---|
231 | return 1;
|
---|
232 | }
|
---|
233 |
|
---|
234 | // first we try to look into the HomologyContainer
|
---|
235 | LOG(1, "INFO: Listing all present homologies ...");
|
---|
236 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
237 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
238 | LOG(1, "INFO: graph " << iter->first << " has Fragment "
|
---|
239 | << iter->second.first << " and associated energy " << iter->second.second << ".");
|
---|
240 | }
|
---|
241 |
|
---|
242 | LOG(0, "STATUS: I'm training now a " << potentialtype << " potential on charges " << charges << ".");
|
---|
243 |
|
---|
244 | /******************** TRAINING ********************/
|
---|
245 | // fit potential
|
---|
246 | FunctionModel *model =
|
---|
247 | PotentialFactory::getInstance().createInstance(
|
---|
248 | potentialtype,
|
---|
249 | charges);
|
---|
250 | ASSERT( model != NULL,
|
---|
251 | "main() - model returned from PotentialFactory is NULL.");
|
---|
252 | FunctionModel::parameters_t params(model->getParameterDimension(), 0.);
|
---|
253 | {
|
---|
254 | // then we ought to pick the right HomologyGraph ...
|
---|
255 | const HomologyGraph graph = getFirstGraphwithTimesSpecificElement(homologies,8,1);
|
---|
256 | if (graph != HomologyGraph()) {
|
---|
257 | LOG(1, "First representative graph containing charges is " << graph << ".");
|
---|
258 |
|
---|
259 | // Afterwards we go through all of this type and gather the distance and the energy value
|
---|
260 | TrainingData data(model->getFragmentSpecificExtractor(charges));
|
---|
261 | data(homologies.getHomologousGraphs(graph));
|
---|
262 | LOG(1, "INFO: I gathered the following training data:\n" <<
|
---|
263 | _detail::writeDistanceEnergyTable(data.getDistanceEnergyTable()));
|
---|
264 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
---|
265 |
|
---|
266 | // now perform the function approximation by optimizing the model function
|
---|
267 | FunctionApproximation approximator(data, *model);
|
---|
268 | if (model->isBoxConstraint() && approximator.checkParameterDerivatives()) {
|
---|
269 | // we set parameters here because we want to test with default ones
|
---|
270 | srand((unsigned)time(0)); // seed with current time
|
---|
271 | model->setParametersToRandomInitialValues(data);
|
---|
272 | LOG(0, "INFO: Initial parameters are " << model->getParameters() << ".");
|
---|
273 | approximator(FunctionApproximation::ParameterDerivative);
|
---|
274 | } else {
|
---|
275 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
---|
276 | return 1;
|
---|
277 | }
|
---|
278 |
|
---|
279 | // LOG(0, "RESULT: " << *model << ".");
|
---|
280 | //
|
---|
281 | params = model->getParameters();
|
---|
282 |
|
---|
283 | }
|
---|
284 | }
|
---|
285 | delete model;
|
---|
286 | // remove static instances
|
---|
287 | PotentialFactory::purgeInstance();
|
---|
288 |
|
---|
289 | return 0;
|
---|
290 | }
|
---|
291 |
|
---|