| 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 | * FitPartialChargesAction.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/bind.hpp>
|
|---|
| 47 | #include <boost/filesystem.hpp>
|
|---|
| 48 | #include <boost/foreach.hpp>
|
|---|
| 49 | #include <algorithm>
|
|---|
| 50 | #include <functional>
|
|---|
| 51 | #include <iostream>
|
|---|
| 52 | #include <string>
|
|---|
| 53 |
|
|---|
| 54 | #include "Actions/PotentialAction/FitPartialChargesAction.hpp"
|
|---|
| 55 |
|
|---|
| 56 | #include "Potentials/PartialNucleiChargeFitter.hpp"
|
|---|
| 57 |
|
|---|
| 58 | #include "Element/element.hpp"
|
|---|
| 59 | #include "Element/periodentafel.hpp"
|
|---|
| 60 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
|---|
| 61 | #include "Fragmentation/Homology/HomologyGraph.hpp"
|
|---|
| 62 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
|
|---|
| 63 | #include "FunctionApproximation/Extractors.hpp"
|
|---|
| 64 | #include "Potentials/PartialNucleiChargeFitter.hpp"
|
|---|
| 65 | #include "Potentials/Particles/ParticleFactory.hpp"
|
|---|
| 66 | #include "Potentials/Particles/ParticleRegistry.hpp"
|
|---|
| 67 | #include "Potentials/SerializablePotential.hpp"
|
|---|
| 68 | #include "World.hpp"
|
|---|
| 69 |
|
|---|
| 70 | using namespace MoleCuilder;
|
|---|
| 71 |
|
|---|
| 72 | // and construct the stuff
|
|---|
| 73 | #include "FitPartialChargesAction.def"
|
|---|
| 74 | #include "Action_impl_pre.hpp"
|
|---|
| 75 | /** =========== define the function ====================== */
|
|---|
| 76 |
|
|---|
| 77 | inline
|
|---|
| 78 | HomologyGraph getFirstGraphwithSpecifiedElements(
|
|---|
| 79 | const HomologyContainer &homologies,
|
|---|
| 80 | const SerializablePotential::ParticleTypes_t &types)
|
|---|
| 81 | {
|
|---|
| 82 | ASSERT( !types.empty(),
|
|---|
| 83 | "getFirstGraphwithSpecifiedElements() - charges is empty?");
|
|---|
| 84 | // create charges
|
|---|
| 85 | Fragment::charges_t charges;
|
|---|
| 86 | charges.resize(types.size());
|
|---|
| 87 | std::transform(types.begin(), types.end(),
|
|---|
| 88 | charges.begin(), boost::lambda::_1);
|
|---|
| 89 | // convert into count map
|
|---|
| 90 | Extractors::elementcounts_t counts_per_charge =
|
|---|
| 91 | Extractors::_detail::getElementCounts(charges);
|
|---|
| 92 | ASSERT( !counts_per_charge.empty(),
|
|---|
| 93 | "getFirstGraphwithSpecifiedElements() - charge counts are empty?");
|
|---|
| 94 | LOG(2, "DEBUG: counts_per_charge is " << counts_per_charge << ".");
|
|---|
| 95 | // we want to check each (unique) key only once
|
|---|
| 96 | HomologyContainer::const_key_iterator olditer = homologies.key_end();
|
|---|
| 97 | for (HomologyContainer::const_key_iterator iter =
|
|---|
| 98 | homologies.key_begin(); iter != homologies.key_end();
|
|---|
| 99 | iter = homologies.getNextKey(iter)) {
|
|---|
| 100 | // if it's the same as the old one, skip it
|
|---|
| 101 | if (olditer == iter)
|
|---|
| 102 | continue;
|
|---|
| 103 | else
|
|---|
| 104 | olditer = iter;
|
|---|
| 105 | // if it's a new key, check if every element has the right number of counts
|
|---|
| 106 | Extractors::elementcounts_t::const_iterator countiter = counts_per_charge.begin();
|
|---|
| 107 | for (; countiter != counts_per_charge.end(); ++countiter)
|
|---|
| 108 | if (!(*iter).hasTimesAtomicNumber(
|
|---|
| 109 | static_cast<size_t>(countiter->first),
|
|---|
| 110 | static_cast<size_t>(countiter->second))
|
|---|
| 111 | )
|
|---|
| 112 | break;
|
|---|
| 113 | if( countiter == counts_per_charge.end())
|
|---|
| 114 | return *iter;
|
|---|
| 115 | }
|
|---|
| 116 | return HomologyGraph();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | ActionState::ptr PotentialFitPartialChargesAction::performCall() {
|
|---|
| 120 |
|
|---|
| 121 | // fragment specifies the homology fragment to use
|
|---|
| 122 | SerializablePotential::ParticleTypes_t fragmentnumbers;
|
|---|
| 123 | {
|
|---|
| 124 | const std::vector<const element *> &fragment = params.fragment.get();
|
|---|
| 125 | std::transform(fragment.begin(), fragment.end(), std::back_inserter(fragmentnumbers),
|
|---|
| 126 | boost::bind(&element::getAtomicNumber, _1));
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | // parse homologies into container
|
|---|
| 130 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
|---|
| 131 | const HomologyGraph graph = getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers);
|
|---|
| 132 | HomologyContainer::range_t range = homologies.getHomologousGraphs(graph);
|
|---|
| 133 | // for the moment just use the very first fragment
|
|---|
| 134 | if (range.first == range.second) {
|
|---|
| 135 | STATUS("HomologyContainer does not contain specified fragment.");
|
|---|
| 136 | return Action::failure;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // average partial charges over all fragments
|
|---|
| 140 | HomologyContainer::const_iterator iter = range.first;
|
|---|
| 141 | if (!iter->second.containsGrids) {
|
|---|
| 142 | STATUS("This HomologyGraph does not contain sampled grids.");
|
|---|
| 143 | return Action::failure;
|
|---|
| 144 | }
|
|---|
| 145 | PartialNucleiChargeFitter::charges_t averaged_charges;
|
|---|
| 146 | averaged_charges.resize(iter->second.fragment.getCharges().size(), 0.);
|
|---|
| 147 | size_t NoFragments = 0;
|
|---|
| 148 | for (;
|
|---|
| 149 | iter != range.second; ++iter, ++NoFragments) {
|
|---|
| 150 | if (!iter->second.containsGrids) {
|
|---|
| 151 | ELOG(2, "This HomologyGraph does not contain sampled grids,\ndid you forget to add '--store-grids 1' to AnalyseFragmentResults.");
|
|---|
| 152 | return Action::failure;
|
|---|
| 153 | }
|
|---|
| 154 | const Fragment &fragment = iter->second.fragment;
|
|---|
| 155 | // const double &energy = iter->second.energy;
|
|---|
| 156 | // const SamplingGrid &charge = iter->second.charge_distribution;
|
|---|
| 157 | const SamplingGrid &potential = iter->second.potential_distribution;
|
|---|
| 158 | if ((potential.level == 0)
|
|---|
| 159 | || ((potential.begin[0] == potential.end[0])
|
|---|
| 160 | && (potential.begin[1] == potential.end[1])
|
|---|
| 161 | && (potential.begin[2] == potential.end[2]))) {
|
|---|
| 162 | ELOG(1, "Sampled grid contains grid made of zero points.");
|
|---|
| 163 | return Action::failure;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | // then we extract positions from fragment
|
|---|
| 167 | PartialNucleiChargeFitter::positions_t positions;
|
|---|
| 168 | Fragment::positions_t fragmentpositions = fragment.getPositions();
|
|---|
| 169 | positions.reserve(fragmentpositions.size());
|
|---|
| 170 | BOOST_FOREACH( Fragment::position_t pos, fragmentpositions) {
|
|---|
| 171 | positions.push_back( Vector(pos[0], pos[1], pos[2]) );
|
|---|
| 172 | }
|
|---|
| 173 | PartialNucleiChargeFitter fitter(potential, positions, params.radius.get());
|
|---|
| 174 | fitter();
|
|---|
| 175 | PartialNucleiChargeFitter::charges_t return_charges = fitter.getSolutionAsCharges_t();
|
|---|
| 176 | std::transform(
|
|---|
| 177 | return_charges.begin(), return_charges.end(),
|
|---|
| 178 | averaged_charges.begin(),
|
|---|
| 179 | averaged_charges.begin(),
|
|---|
| 180 | std::plus<PartialNucleiChargeFitter::charge_t>());
|
|---|
| 181 | }
|
|---|
| 182 | std::transform(averaged_charges.begin(),averaged_charges.end(),
|
|---|
| 183 | averaged_charges.begin(),
|
|---|
| 184 | std::bind1st(std::multiplies<PartialNucleiChargeFitter::charge_t>(),1./NoFragments)
|
|---|
| 185 | );
|
|---|
| 186 |
|
|---|
| 187 | // make sum of charges zero if desired
|
|---|
| 188 | if (params.enforceZeroCharge.get()) {
|
|---|
| 189 | double charge_sum = 0.;
|
|---|
| 190 | charge_sum = std::accumulate(averaged_charges.begin(), averaged_charges.end(), charge_sum);
|
|---|
| 191 | if (fabs(charge_sum) > MYEPSILON) {
|
|---|
| 192 | std::transform(
|
|---|
| 193 | averaged_charges.begin(), averaged_charges.end(),
|
|---|
| 194 | averaged_charges.begin(),
|
|---|
| 195 | boost::bind(std::minus<double>(), _1, charge_sum/averaged_charges.size()));
|
|---|
| 196 | }
|
|---|
| 197 | charge_sum = 0.;
|
|---|
| 198 | charge_sum = std::accumulate(averaged_charges.begin(), averaged_charges.end(), charge_sum);
|
|---|
| 199 | ASSERT( fabs(charge_sum) < MYEPSILON,
|
|---|
| 200 | "PotentialFitPartialChargesAction::performCall() - enforcing neutral net charge failed, "
|
|---|
| 201 | +toString(charge_sum)+" is the remaining net charge.");
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | // place all fitted charges into ParticleRegistry
|
|---|
| 205 | const ParticleFactory &factory =
|
|---|
| 206 | const_cast<const ParticleFactory&>(ParticleFactory::getInstance());
|
|---|
| 207 | const periodentafel &periode = *World::getInstance().getPeriode();
|
|---|
| 208 | ASSERT(averaged_charges.size() == fragmentnumbers.size(),
|
|---|
| 209 | "PotentialFitPartialChargesAction::performCall() - charges and elements length mismatch.");
|
|---|
| 210 | for (size_t i=0;i<averaged_charges.size(); ++i) {
|
|---|
| 211 | const std::string name = Particle::findFreeName(periode, fragmentnumbers[i]);
|
|---|
| 212 | LOG(2, "INFO: Adding particle " << name << " for element "
|
|---|
| 213 | << fragmentnumbers[i] << ", charge " << averaged_charges[i]);
|
|---|
| 214 | factory.createInstance(name, fragmentnumbers[i], averaged_charges[i]);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | // output fitted charges
|
|---|
| 218 | LOG(0, "STATUS: We have fitted the following charges " << averaged_charges
|
|---|
| 219 | << ", averaged over " << NoFragments << " fragments.");
|
|---|
| 220 |
|
|---|
| 221 | return Action::success;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | ActionState::ptr PotentialFitPartialChargesAction::performUndo(ActionState::ptr _state) {
|
|---|
| 225 | return Action::success;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | ActionState::ptr PotentialFitPartialChargesAction::performRedo(ActionState::ptr _state){
|
|---|
| 229 | return Action::success;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | bool PotentialFitPartialChargesAction::canUndo() {
|
|---|
| 233 | return false;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | bool PotentialFitPartialChargesAction::shouldUndo() {
|
|---|
| 237 | return false;
|
|---|
| 238 | }
|
|---|
| 239 | /** =========== end of function ====================== */
|
|---|