[c4a323] | 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>
|
---|
[9b68fc] | 49 | #include <functional>
|
---|
[c4a323] | 50 | #include <iostream>
|
---|
| 51 | #include <string>
|
---|
| 52 |
|
---|
[f5724f] | 53 | #include "Actions/PotentialAction/FitParticleChargesAction.hpp"
|
---|
[c4a323] | 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 =
|
---|
[e63edb] | 94 | homologies.key_begin(); iter != homologies.key_end();
|
---|
| 95 | iter = homologies.getNextKey(iter)) {
|
---|
[c4a323] | 96 | // if it's the same as the old one, skip it
|
---|
[e63edb] | 97 | if (olditer == iter)
|
---|
[c4a323] | 98 | continue;
|
---|
[e63edb] | 99 | else
|
---|
| 100 | olditer = iter;
|
---|
[c4a323] | 101 | // if it's a new key, check if every element has the right number of counts
|
---|
| 102 | Extractors::elementcounts_t::const_iterator countiter = counts_per_charge.begin();
|
---|
| 103 | for (; countiter != counts_per_charge.end(); ++countiter)
|
---|
| 104 | if (!(*iter).hasTimesAtomicNumber(
|
---|
| 105 | static_cast<size_t>(countiter->first),
|
---|
| 106 | static_cast<size_t>(countiter->second))
|
---|
| 107 | )
|
---|
| 108 | break;
|
---|
| 109 | if( countiter == counts_per_charge.end())
|
---|
| 110 | return *iter;
|
---|
| 111 | }
|
---|
| 112 | return HomologyGraph();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[b5b01e] | 115 | ActionState::ptr PotentialFitParticleChargesAction::performCall() {
|
---|
[c4a323] | 116 |
|
---|
| 117 | // fragment specifies the homology fragment to use
|
---|
| 118 | SerializablePotential::ParticleTypes_t fragmentnumbers;
|
---|
| 119 | {
|
---|
| 120 | const std::vector<const element *> &fragment = params.fragment.get();
|
---|
| 121 | std::transform(fragment.begin(), fragment.end(), std::back_inserter(fragmentnumbers),
|
---|
| 122 | boost::bind(&element::getAtomicNumber, _1));
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // parse homologies into container
|
---|
| 126 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 127 | const HomologyGraph graph = getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers);
|
---|
| 128 | HomologyContainer::range_t range = homologies.getHomologousGraphs(graph);
|
---|
| 129 | // for the moment just use the very first fragment
|
---|
| 130 | if (range.first == range.second) {
|
---|
[26b4d62] | 131 | STATUS("HomologyContainer does not contain specified fragment.");
|
---|
[c4a323] | 132 | return Action::failure;
|
---|
| 133 | }
|
---|
[9b68fc] | 134 |
|
---|
| 135 | // average partial charges over all fragments
|
---|
[c4a323] | 136 | HomologyContainer::const_iterator iter = range.first;
|
---|
| 137 | if (!iter->second.containsGrids) {
|
---|
[26b4d62] | 138 | STATUS("This HomologyGraph does not contain sampled grids.");
|
---|
[c4a323] | 139 | return Action::failure;
|
---|
| 140 | }
|
---|
[9b68fc] | 141 | PartialNucleiChargeFitter::charges_t averaged_charges;
|
---|
| 142 | averaged_charges.resize(iter->second.fragment.getCharges().size(), 0.);
|
---|
| 143 | size_t NoFragments = 0;
|
---|
| 144 | for (;
|
---|
| 145 | iter != range.second; ++iter, ++NoFragments) {
|
---|
| 146 | if (!iter->second.containsGrids) {
|
---|
| 147 | ELOG(2, "This HomologyGraph does not contain sampled grids,\ndid you forget to add '--store-grids 1' to AnalyseFragmentResults.");
|
---|
| 148 | return Action::failure;
|
---|
| 149 | }
|
---|
| 150 | const Fragment &fragment = iter->second.fragment;
|
---|
| 151 | // const double &energy = iter->second.energy;
|
---|
| 152 | // const SamplingGrid &charge = iter->second.charge_distribution;
|
---|
| 153 | const SamplingGrid &potential = iter->second.potential_distribution;
|
---|
| 154 | if ((potential.level == 0)
|
---|
| 155 | || ((potential.begin[0] == potential.end[0])
|
---|
| 156 | && (potential.begin[1] == potential.end[1])
|
---|
| 157 | && (potential.begin[2] == potential.end[2]))) {
|
---|
| 158 | ELOG(1, "Sampled grid contains grid made of zero points.");
|
---|
| 159 | return Action::failure;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | // then we extract positions from fragment
|
---|
| 163 | PartialNucleiChargeFitter::positions_t positions;
|
---|
| 164 | Fragment::positions_t fragmentpositions = fragment.getPositions();
|
---|
| 165 | positions.reserve(fragmentpositions.size());
|
---|
| 166 | BOOST_FOREACH( Fragment::position_t pos, fragmentpositions) {
|
---|
| 167 | positions.push_back( Vector(pos[0], pos[1], pos[2]) );
|
---|
| 168 | }
|
---|
| 169 | PartialNucleiChargeFitter fitter(potential, positions, params.radius.get());
|
---|
| 170 | fitter();
|
---|
| 171 | PartialNucleiChargeFitter::charges_t return_charges = fitter.getSolutionAsCharges_t();
|
---|
| 172 | std::transform(
|
---|
| 173 | return_charges.begin(), return_charges.end(),
|
---|
| 174 | averaged_charges.begin(),
|
---|
| 175 | averaged_charges.begin(),
|
---|
| 176 | std::plus<PartialNucleiChargeFitter::charge_t>());
|
---|
[c4a323] | 177 | }
|
---|
[9b68fc] | 178 | std::transform(averaged_charges.begin(),averaged_charges.end(),
|
---|
| 179 | averaged_charges.begin(),
|
---|
| 180 | std::bind1st(std::multiplies<PartialNucleiChargeFitter::charge_t>(),1./NoFragments)
|
---|
| 181 | );
|
---|
[c4a323] | 182 |
|
---|
| 183 |
|
---|
| 184 | // output fitted charges
|
---|
[9b68fc] | 185 | LOG(0, "STATUS: We have fitted the following charges " << averaged_charges
|
---|
| 186 | << ", averaged over " << NoFragments << " fragments.");
|
---|
[c4a323] | 187 |
|
---|
| 188 | return Action::success;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[b5b01e] | 191 | ActionState::ptr PotentialFitParticleChargesAction::performUndo(ActionState::ptr _state) {
|
---|
[c4a323] | 192 | return Action::success;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[b5b01e] | 195 | ActionState::ptr PotentialFitParticleChargesAction::performRedo(ActionState::ptr _state){
|
---|
[c4a323] | 196 | return Action::success;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[f5724f] | 199 | bool PotentialFitParticleChargesAction::canUndo() {
|
---|
[c4a323] | 200 | return false;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[f5724f] | 203 | bool PotentialFitParticleChargesAction::shouldUndo() {
|
---|
[c4a323] | 204 | return false;
|
---|
| 205 | }
|
---|
| 206 | /** =========== end of function ====================== */
|
---|