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 "FunctionApproximation/writeDistanceEnergyTable.hpp"
|
---|
69 | #include "Helpers/defs.hpp"
|
---|
70 | #include "Potentials/Specifics/PairPotential_Morse.hpp"
|
---|
71 | #include "Potentials/Specifics/PairPotential_Angle.hpp"
|
---|
72 | #include "Potentials/Specifics/SaturationPotential.hpp"
|
---|
73 |
|
---|
74 | namespace po = boost::program_options;
|
---|
75 |
|
---|
76 | using namespace boost::assign;
|
---|
77 |
|
---|
78 | HomologyGraph getFirstGraphWithThreeCarbons(const HomologyContainer &homologies)
|
---|
79 | {
|
---|
80 | FragmentNode SaturatedCarbon(6,4); // carbon has atomic number 6 and should have 4 bonds for C3H8
|
---|
81 | FragmentNode DanglingCarbon(6,3); // carbon has atomic number 6 and should have 3 pure bonds for C3H8
|
---|
82 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
83 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
84 | if ((iter->first.hasNode(SaturatedCarbon,2)) && (iter->first.hasNode(DanglingCarbon,1)))
|
---|
85 | return iter->first;
|
---|
86 | }
|
---|
87 | return HomologyGraph();
|
---|
88 | }
|
---|
89 |
|
---|
90 | HomologyGraph getFirstGraphWithTwoCarbons(const HomologyContainer &homologies)
|
---|
91 | {
|
---|
92 | FragmentNode SaturatedCarbon(6,3); // carbon has atomic number 6 and should have 4 bonds for C2H6
|
---|
93 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
94 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
95 | if (iter->first.hasNode(SaturatedCarbon,2))
|
---|
96 | return iter->first;
|
---|
97 | }
|
---|
98 | return HomologyGraph();
|
---|
99 | }
|
---|
100 |
|
---|
101 | HomologyGraph getFirstGraphWithOneCarbon(const HomologyContainer &homologies)
|
---|
102 | {
|
---|
103 | FragmentNode SaturatedCarbon(6,2); // carbon has atomic number 6 and has 3 bonds (to other Hs)
|
---|
104 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
105 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
106 | if (iter->first.hasNode(SaturatedCarbon,1))
|
---|
107 | return iter->first;
|
---|
108 | }
|
---|
109 | return HomologyGraph();
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /** This function returns the elements of the sum over index "k" for an
|
---|
114 | * argument containing indices "i" and "j"
|
---|
115 | * @param inputs vector of all configuration (containing each a vector of all arguments)
|
---|
116 | * @param arg argument containing indices "i" and "j"
|
---|
117 | * @param cutoff cutoff criterion for sum over k
|
---|
118 | * @return vector of argument pairs (a vector) of ik and jk for at least all k
|
---|
119 | * within distance of \a cutoff to i
|
---|
120 | */
|
---|
121 | std::vector<FunctionModel::arguments_t>
|
---|
122 | getTripleFromArgument(const FunctionApproximation::inputs_t &inputs, const argument_t &arg, const double cutoff)
|
---|
123 | {
|
---|
124 | typedef std::list<argument_t> arg_list_t;
|
---|
125 | typedef std::map<size_t, arg_list_t > k_args_map_t;
|
---|
126 | k_args_map_t tempresult;
|
---|
127 | ASSERT( inputs.size() > arg.globalid,
|
---|
128 | "getTripleFromArgument() - globalid "+toString(arg.globalid)
|
---|
129 | +" is greater than all inputs "+toString(inputs.size())+".");
|
---|
130 | const FunctionModel::arguments_t &listofargs = inputs[arg.globalid];
|
---|
131 | for (FunctionModel::arguments_t::const_iterator argiter = listofargs.begin();
|
---|
132 | argiter != listofargs.end();
|
---|
133 | ++argiter) {
|
---|
134 | // first index must be either i or j but second index not
|
---|
135 | if (((argiter->indices.first == arg.indices.first)
|
---|
136 | || (argiter->indices.first == arg.indices.second))
|
---|
137 | && ((argiter->indices.second != arg.indices.first)
|
---|
138 | && (argiter->indices.second != arg.indices.second))) {
|
---|
139 | // we need arguments ik and jk
|
---|
140 | std::pair< k_args_map_t::iterator, bool> inserter =
|
---|
141 | tempresult.insert( std::make_pair( argiter->indices.second, arg_list_t(1,*argiter)));
|
---|
142 | if (!inserter.second) {
|
---|
143 | // is present one ik or jk, if ik insert jk at back
|
---|
144 | if (inserter.first->second.begin()->indices.first == arg.indices.first)
|
---|
145 | inserter.first->second.push_back(*argiter);
|
---|
146 | else // if jk, insert ik at front
|
---|
147 | inserter.first->second.push_front(*argiter);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | // // or second index must be either i or j but first index not
|
---|
151 | // else if (((argiter->indices.first != arg.indices.first)
|
---|
152 | // && (argiter->indices.first != arg.indices.second))
|
---|
153 | // && ((argiter->indices.second == arg.indices.first)
|
---|
154 | // || (argiter->indices.second == arg.indices.second))) {
|
---|
155 | // // we need arguments ki and kj
|
---|
156 | // std::pair< k_args_map_t::iterator, bool> inserter =
|
---|
157 | // tempresult.insert( std::make_pair( argiter->indices.first, arg_list_t(1,*argiter)));
|
---|
158 | // if (!inserter.second) {
|
---|
159 | // // is present one ki or kj, if ki insert kj at back
|
---|
160 | // if (inserter.first->second.begin()->indices.second == arg.indices.first)
|
---|
161 | // inserter.first->second.push_back(*argiter);
|
---|
162 | // else // if kj, insert ki at front
|
---|
163 | // inserter.first->second.push_front(*argiter);
|
---|
164 | // }
|
---|
165 | // }
|
---|
166 | }
|
---|
167 | // check that i,j are NOT contained
|
---|
168 | ASSERT( tempresult.count(arg.indices.first) == 0,
|
---|
169 | "getTripleFromArgument() - first index of argument present in k_args_map?");
|
---|
170 | ASSERT( tempresult.count(arg.indices.second) == 0,
|
---|
171 | "getTripleFromArgument() - first index of argument present in k_args_map?");
|
---|
172 |
|
---|
173 | // convert
|
---|
174 | std::vector<FunctionModel::arguments_t> result;
|
---|
175 | for (k_args_map_t::const_iterator iter = tempresult.begin();
|
---|
176 | iter != tempresult.end();
|
---|
177 | ++iter) {
|
---|
178 | ASSERT( iter->second.size() == 2,
|
---|
179 | "getTripleFromArgument() - for index "+toString(iter->first)+" we did not find both ik and jk.");
|
---|
180 | result.push_back( FunctionModel::arguments_t(iter->second.begin(), iter->second.end()) );
|
---|
181 | }
|
---|
182 | return result;
|
---|
183 | }
|
---|
184 |
|
---|
185 | int main(int argc, char **argv)
|
---|
186 | {
|
---|
187 | std::cout << "Hello to the World from LevMar!" << std::endl;
|
---|
188 |
|
---|
189 | // load homology file
|
---|
190 | po::options_description desc("Allowed options");
|
---|
191 | desc.add_options()
|
---|
192 | ("help", "produce help message")
|
---|
193 | ("homology-file", po::value< boost::filesystem::path >(), "homology file to parse")
|
---|
194 | ;
|
---|
195 |
|
---|
196 | po::variables_map vm;
|
---|
197 | po::store(po::parse_command_line(argc, argv, desc), vm);
|
---|
198 | po::notify(vm);
|
---|
199 |
|
---|
200 | if (vm.count("help")) {
|
---|
201 | std::cout << desc << "\n";
|
---|
202 | return 1;
|
---|
203 | }
|
---|
204 |
|
---|
205 | boost::filesystem::path homology_file;
|
---|
206 | if (vm.count("homology-file")) {
|
---|
207 | homology_file = vm["homology-file"].as<boost::filesystem::path>();
|
---|
208 | LOG(1, "INFO: Parsing " << homology_file.string() << ".");
|
---|
209 | } else {
|
---|
210 | LOG(0, "homology-file level was not set.");
|
---|
211 | }
|
---|
212 | HomologyContainer homologies;
|
---|
213 | if (boost::filesystem::exists(homology_file)) {
|
---|
214 | std::ifstream returnstream(homology_file.string().c_str());
|
---|
215 | if (returnstream.good()) {
|
---|
216 | boost::archive::text_iarchive ia(returnstream);
|
---|
217 | ia >> homologies;
|
---|
218 | } else {
|
---|
219 | ELOG(2, "Failed to parse from " << homology_file.string() << ".");
|
---|
220 | }
|
---|
221 | returnstream.close();
|
---|
222 | } else {
|
---|
223 | ELOG(0, homology_file << " does not exist.");
|
---|
224 | }
|
---|
225 |
|
---|
226 | // first we try to look into the HomologyContainer
|
---|
227 | LOG(1, "INFO: Listing all present homologies ...");
|
---|
228 | for (HomologyContainer::container_t::const_iterator iter =
|
---|
229 | homologies.begin(); iter != homologies.end(); ++iter) {
|
---|
230 | LOG(1, "INFO: graph " << iter->first << " has Fragment "
|
---|
231 | << iter->second.first << " and associated energy " << iter->second.second << ".");
|
---|
232 | }
|
---|
233 |
|
---|
234 | /******************** Angle TRAINING ********************/
|
---|
235 | {
|
---|
236 | // then we ought to pick the right HomologyGraph ...
|
---|
237 | const HomologyGraph graph = getFirstGraphWithThreeCarbons(homologies);
|
---|
238 | LOG(1, "First representative graph containing three saturated carbons is " << graph << ".");
|
---|
239 |
|
---|
240 | // Afterwards we go through all of this type and gather the distance and the energy value
|
---|
241 | TrainingData AngleData(
|
---|
242 | boost::bind(&Extractors::reorderArgumentsByIncreasingDistance,
|
---|
243 | boost::bind(&Extractors::gatherAllSymmetricDistanceArguments,
|
---|
244 | boost::bind(&Extractors::gatherPositionOfTuples,
|
---|
245 | _1, Fragment::charges_t(3,6.)
|
---|
246 | ), _2 // gather carbon triples
|
---|
247 | )
|
---|
248 | )
|
---|
249 | );
|
---|
250 | AngleData(homologies.getHomologousGraphs(graph));
|
---|
251 | LOG(1, "INFO: I gathered the following training data:\n" <<
|
---|
252 | _detail::writeDistanceEnergyTable(AngleData.getDistanceEnergyTable()));
|
---|
253 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
---|
254 |
|
---|
255 | // now perform the function approximation by optimizing the model function
|
---|
256 | FunctionModel::parameters_t params(PairPotential_Angle::MAXPARAMS, 0.);
|
---|
257 | params[PairPotential_Angle::energy_offset] = -1.;
|
---|
258 | params[PairPotential_Angle::spring_constant] = 1.;
|
---|
259 | params[PairPotential_Angle::equilibrium_distance] = 0.2;
|
---|
260 | PairPotential_Angle angle;
|
---|
261 | LOG(0, "INFO: Initial parameters are " << params << ".");
|
---|
262 | angle.setParameters(params);
|
---|
263 | FunctionModel &model = angle;
|
---|
264 | FunctionApproximation approximator(AngleData, model);
|
---|
265 | if (model.isBoxConstraint() && approximator.checkParameterDerivatives())
|
---|
266 | approximator(FunctionApproximation::ParameterDerivative);
|
---|
267 | else
|
---|
268 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
---|
269 | params = model.getParameters();
|
---|
270 |
|
---|
271 | LOG(0, "RESULT: Best parameters are " << params << ".");
|
---|
272 | }
|
---|
273 |
|
---|
274 | /******************** MORSE TRAINING ********************/
|
---|
275 | {
|
---|
276 | // then we ought to pick the right HomologyGraph ...
|
---|
277 | const HomologyGraph graph = getFirstGraphWithTwoCarbons(homologies);
|
---|
278 | LOG(1, "First representative graph containing two saturated carbons is " << graph << ".");
|
---|
279 |
|
---|
280 | // Afterwards we go through all of this type and gather the distance and the energy value
|
---|
281 | TrainingData MorseData(
|
---|
282 | boost::bind(&Extractors::gatherAllSymmetricDistanceArguments,
|
---|
283 | boost::bind(&Extractors::gatherPositionOfTuples,
|
---|
284 | _1, Fragment::charges_t(2,6.)
|
---|
285 | ), _2 // gather first carbon pair
|
---|
286 | )
|
---|
287 | );
|
---|
288 | MorseData(homologies.getHomologousGraphs(graph));
|
---|
289 | LOG(1, "INFO: I gathered the following training data:\n" <<
|
---|
290 | _detail::writeDistanceEnergyTable(MorseData.getDistanceEnergyTable()));
|
---|
291 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
---|
292 |
|
---|
293 | // now perform the function approximation by optimizing the model function
|
---|
294 | FunctionModel::parameters_t params(PairPotential_Morse::MAXPARAMS, 0.);
|
---|
295 | params[PairPotential_Morse::dissociation_energy] = 0.5;
|
---|
296 | params[PairPotential_Morse::energy_offset] = -1.;
|
---|
297 | params[PairPotential_Morse::spring_constant] = 1.;
|
---|
298 | params[PairPotential_Morse::equilibrium_distance] = 2.9;
|
---|
299 | PairPotential_Morse morse;
|
---|
300 | morse.setParameters(params);
|
---|
301 | FunctionModel &model = morse;
|
---|
302 | FunctionApproximation approximator(MorseData, model); // we only give CC distance, hence 1 input dim
|
---|
303 | if (model.isBoxConstraint() && approximator.checkParameterDerivatives())
|
---|
304 | approximator(FunctionApproximation::ParameterDerivative);
|
---|
305 | else
|
---|
306 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
---|
307 | params = model.getParameters();
|
---|
308 |
|
---|
309 | LOG(0, "RESULT: Best parameters are " << params << ".");
|
---|
310 | }
|
---|
311 |
|
---|
312 | /******************* SATURATION TRAINING *******************/
|
---|
313 | FunctionModel::parameters_t params(SaturationPotential::MAXPARAMS, 0.);
|
---|
314 | {
|
---|
315 | // then we ought to pick the right HomologyGraph ...
|
---|
316 | const HomologyGraph graph = getFirstGraphWithOneCarbon(homologies);
|
---|
317 | LOG(1, "First representative graph containing one saturated carbon is " << graph << ".");
|
---|
318 |
|
---|
319 | // Afterwards we go through all of this type and gather the distance and the energy value
|
---|
320 | TrainingData TersoffData(
|
---|
321 | TrainingData::extractor_t(&Extractors::gatherAllDistances) // gather first carbon pair
|
---|
322 | );
|
---|
323 | TersoffData( homologies.getHomologousGraphs(graph) );
|
---|
324 | LOG(1, "INFO: I gathered the following training data:\n" <<
|
---|
325 | _detail::writeDistanceEnergyTable(TersoffData.getDistanceEnergyTable()));
|
---|
326 | // NOTICE that distance are in bohrradi as they come from MPQC!
|
---|
327 |
|
---|
328 | // now perform the function approximation by optimizing the model function
|
---|
329 | boost::function< std::vector<FunctionModel::arguments_t>(const argument_t &, const double)> triplefunction =
|
---|
330 | boost::bind(&getTripleFromArgument, boost::cref(TersoffData.getTrainingInputs()), _1, _2);
|
---|
331 | srand((unsigned)time(0)); // seed with current time
|
---|
332 | LOG(0, "INFO: Initial parameters are " << params << ".");
|
---|
333 |
|
---|
334 | SaturationPotential saturation(triplefunction);
|
---|
335 | saturation.setParameters(params);
|
---|
336 | FunctionModel &model = saturation;
|
---|
337 | FunctionApproximation approximator(TersoffData, model); // CH4 has 5 atoms, hence 5*4/2 distances
|
---|
338 | if (model.isBoxConstraint() && approximator.checkParameterDerivatives())
|
---|
339 | approximator(FunctionApproximation::ParameterDerivative);
|
---|
340 | else
|
---|
341 | ELOG(0, "We require parameter derivatives for a box constraint minimization.");
|
---|
342 |
|
---|
343 | params = model.getParameters();
|
---|
344 |
|
---|
345 | LOG(0, "RESULT: Best parameters are " << params << ".");
|
---|
346 |
|
---|
347 | // std::cout << "\tsaturationparticle:";
|
---|
348 | // std::cout << "\tparticle_type=C,";
|
---|
349 | // std::cout << "\tA=" << params[SaturationPotential::A] << ",";
|
---|
350 | // std::cout << "\tB=" << params[SaturationPotential::B] << ",";
|
---|
351 | // std::cout << "\tlambda=" << params[SaturationPotential::lambda] << ",";
|
---|
352 | // std::cout << "\tmu=" << params[SaturationPotential::mu] << ",";
|
---|
353 | // std::cout << "\tbeta=" << params[SaturationPotential::beta] << ",";
|
---|
354 | // std::cout << "\tn=" << params[SaturationPotential::n] << ",";
|
---|
355 | // std::cout << "\tc=" << params[SaturationPotential::c] << ",";
|
---|
356 | // std::cout << "\td=" << params[SaturationPotential::d] << ",";
|
---|
357 | // std::cout << "\th=" << params[SaturationPotential::h] << ",";
|
---|
358 | //// std::cout << "\toffset=" << params[SaturationPotential::offset] << ",";
|
---|
359 | // std::cout << "\tR=" << saturation.R << ",";
|
---|
360 | // std::cout << "\tS=" << saturation.S << ";";
|
---|
361 | // std::cout << std::endl;
|
---|
362 |
|
---|
363 | // check L2 and Lmax error against training set
|
---|
364 | LOG(1, "INFO: L2sum = " << TersoffData.getL2Error(model)
|
---|
365 | << ", LMax = " << TersoffData.getLMaxError(model) << ".");
|
---|
366 | }
|
---|
367 |
|
---|
368 | return 0;
|
---|
369 | }
|
---|