1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * FitParticleChargesAction.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jul 03, 2013
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | // needs to come before MemDebug due to placement new
|
---|
36 | #include <boost/archive/text_iarchive.hpp>
|
---|
37 |
|
---|
38 | #include "CodePatterns/MemDebug.hpp"
|
---|
39 |
|
---|
40 | #include "Atom/atom.hpp"
|
---|
41 | #include "CodePatterns/Log.hpp"
|
---|
42 | #include "Fragmentation/Exporters/ExportGraph_ToFiles.hpp"
|
---|
43 | #include "Fragmentation/Graph.hpp"
|
---|
44 | #include "World.hpp"
|
---|
45 |
|
---|
46 | #include <boost/filesystem.hpp>
|
---|
47 | #include <boost/foreach.hpp>
|
---|
48 | #include <algorithm>
|
---|
49 | #include <functional>
|
---|
50 | #include <iostream>
|
---|
51 | #include <string>
|
---|
52 |
|
---|
53 | #include "Actions/PotentialAction/FitParticleChargesAction.hpp"
|
---|
54 |
|
---|
55 | #include "Potentials/PartialNucleiChargeFitter.hpp"
|
---|
56 |
|
---|
57 | #include "Element/element.hpp"
|
---|
58 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
---|
59 | #include "Fragmentation/Homology/HomologyGraph.hpp"
|
---|
60 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
|
---|
61 | #include "FunctionApproximation/Extractors.hpp"
|
---|
62 | #include "Potentials/PartialNucleiChargeFitter.hpp"
|
---|
63 | #include "Potentials/SerializablePotential.hpp"
|
---|
64 | #include "World.hpp"
|
---|
65 |
|
---|
66 | using namespace MoleCuilder;
|
---|
67 |
|
---|
68 | // and construct the stuff
|
---|
69 | #include "FitParticleChargesAction.def"
|
---|
70 | #include "Action_impl_pre.hpp"
|
---|
71 | /** =========== define the function ====================== */
|
---|
72 |
|
---|
73 | inline
|
---|
74 | HomologyGraph getFirstGraphwithSpecifiedElements(
|
---|
75 | const HomologyContainer &homologies,
|
---|
76 | const SerializablePotential::ParticleTypes_t &types)
|
---|
77 | {
|
---|
78 | ASSERT( !types.empty(),
|
---|
79 | "getFirstGraphwithSpecifiedElements() - charges is empty?");
|
---|
80 | // create charges
|
---|
81 | Fragment::charges_t charges;
|
---|
82 | charges.resize(types.size());
|
---|
83 | std::transform(types.begin(), types.end(),
|
---|
84 | charges.begin(), boost::lambda::_1);
|
---|
85 | // convert into count map
|
---|
86 | Extractors::elementcounts_t counts_per_charge =
|
---|
87 | Extractors::_detail::getElementCounts(charges);
|
---|
88 | ASSERT( !counts_per_charge.empty(),
|
---|
89 | "getFirstGraphwithSpecifiedElements() - charge counts are empty?");
|
---|
90 | LOG(2, "DEBUG: counts_per_charge is " << counts_per_charge << ".");
|
---|
91 | // we want to check each (unique) key only once
|
---|
92 | HomologyContainer::const_key_iterator olditer = homologies.key_end();
|
---|
93 | for (HomologyContainer::const_key_iterator iter =
|
---|
94 | homologies.key_begin(); iter != homologies.key_end(); olditer = iter++) {
|
---|
95 | // if it's the same as the old one, skip it
|
---|
96 | if (*olditer == *iter)
|
---|
97 | continue;
|
---|
98 | // if it's a new key, check if every element has the right number of counts
|
---|
99 | Extractors::elementcounts_t::const_iterator countiter = counts_per_charge.begin();
|
---|
100 | for (; countiter != counts_per_charge.end(); ++countiter)
|
---|
101 | if (!(*iter).hasTimesAtomicNumber(
|
---|
102 | static_cast<size_t>(countiter->first),
|
---|
103 | static_cast<size_t>(countiter->second))
|
---|
104 | )
|
---|
105 | break;
|
---|
106 | if( countiter == counts_per_charge.end())
|
---|
107 | return *iter;
|
---|
108 | }
|
---|
109 | return HomologyGraph();
|
---|
110 | }
|
---|
111 |
|
---|
112 | ActionState::ptr PotentialFitParticleChargesAction::performCall() {
|
---|
113 |
|
---|
114 | // fragment specifies the homology fragment to use
|
---|
115 | SerializablePotential::ParticleTypes_t fragmentnumbers;
|
---|
116 | {
|
---|
117 | const std::vector<const element *> &fragment = params.fragment.get();
|
---|
118 | std::transform(fragment.begin(), fragment.end(), std::back_inserter(fragmentnumbers),
|
---|
119 | boost::bind(&element::getAtomicNumber, _1));
|
---|
120 | }
|
---|
121 |
|
---|
122 | // parse homologies into container
|
---|
123 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
124 | const HomologyGraph graph = getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers);
|
---|
125 | HomologyContainer::range_t range = homologies.getHomologousGraphs(graph);
|
---|
126 | // for the moment just use the very first fragment
|
---|
127 | if (range.first == range.second) {
|
---|
128 | STATUS("HomologyContainer does not contain specified fragment.");
|
---|
129 | return Action::failure;
|
---|
130 | }
|
---|
131 |
|
---|
132 | // average partial charges over all fragments
|
---|
133 | HomologyContainer::const_iterator iter = range.first;
|
---|
134 | if (!iter->second.containsGrids) {
|
---|
135 | STATUS("This HomologyGraph does not contain sampled grids.");
|
---|
136 | return Action::failure;
|
---|
137 | }
|
---|
138 | PartialNucleiChargeFitter::charges_t averaged_charges;
|
---|
139 | averaged_charges.resize(iter->second.fragment.getCharges().size(), 0.);
|
---|
140 | size_t NoFragments = 0;
|
---|
141 | for (;
|
---|
142 | iter != range.second; ++iter, ++NoFragments) {
|
---|
143 | if (!iter->second.containsGrids) {
|
---|
144 | ELOG(2, "This HomologyGraph does not contain sampled grids,\ndid you forget to add '--store-grids 1' to AnalyseFragmentResults.");
|
---|
145 | return Action::failure;
|
---|
146 | }
|
---|
147 | const Fragment &fragment = iter->second.fragment;
|
---|
148 | // const double &energy = iter->second.energy;
|
---|
149 | // const SamplingGrid &charge = iter->second.charge_distribution;
|
---|
150 | const SamplingGrid &potential = iter->second.potential_distribution;
|
---|
151 | if ((potential.level == 0)
|
---|
152 | || ((potential.begin[0] == potential.end[0])
|
---|
153 | && (potential.begin[1] == potential.end[1])
|
---|
154 | && (potential.begin[2] == potential.end[2]))) {
|
---|
155 | ELOG(1, "Sampled grid contains grid made of zero points.");
|
---|
156 | return Action::failure;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // then we extract positions from fragment
|
---|
160 | PartialNucleiChargeFitter::positions_t positions;
|
---|
161 | Fragment::positions_t fragmentpositions = fragment.getPositions();
|
---|
162 | positions.reserve(fragmentpositions.size());
|
---|
163 | BOOST_FOREACH( Fragment::position_t pos, fragmentpositions) {
|
---|
164 | positions.push_back( Vector(pos[0], pos[1], pos[2]) );
|
---|
165 | }
|
---|
166 | PartialNucleiChargeFitter fitter(potential, positions, params.radius.get());
|
---|
167 | fitter();
|
---|
168 | PartialNucleiChargeFitter::charges_t return_charges = fitter.getSolutionAsCharges_t();
|
---|
169 | std::transform(
|
---|
170 | return_charges.begin(), return_charges.end(),
|
---|
171 | averaged_charges.begin(),
|
---|
172 | averaged_charges.begin(),
|
---|
173 | std::plus<PartialNucleiChargeFitter::charge_t>());
|
---|
174 | }
|
---|
175 | std::transform(averaged_charges.begin(),averaged_charges.end(),
|
---|
176 | averaged_charges.begin(),
|
---|
177 | std::bind1st(std::multiplies<PartialNucleiChargeFitter::charge_t>(),1./NoFragments)
|
---|
178 | );
|
---|
179 |
|
---|
180 |
|
---|
181 | // output fitted charges
|
---|
182 | LOG(0, "STATUS: We have fitted the following charges " << averaged_charges
|
---|
183 | << ", averaged over " << NoFragments << " fragments.");
|
---|
184 |
|
---|
185 | return Action::success;
|
---|
186 | }
|
---|
187 |
|
---|
188 | ActionState::ptr PotentialFitParticleChargesAction::performUndo(ActionState::ptr _state) {
|
---|
189 | return Action::success;
|
---|
190 | }
|
---|
191 |
|
---|
192 | ActionState::ptr PotentialFitParticleChargesAction::performRedo(ActionState::ptr _state){
|
---|
193 | return Action::success;
|
---|
194 | }
|
---|
195 |
|
---|
196 | bool PotentialFitParticleChargesAction::canUndo() {
|
---|
197 | return false;
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool PotentialFitParticleChargesAction::shouldUndo() {
|
---|
201 | return false;
|
---|
202 | }
|
---|
203 | /** =========== end of function ====================== */
|
---|