[2f9faf] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2016 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 | * FitFragmentPartialChargesAction.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Oct 09, 2016
|
---|
| 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/bimap.hpp>
|
---|
| 47 | #include <boost/bimap/multiset_of.hpp>
|
---|
| 48 | #include <boost/bind.hpp>
|
---|
| 49 | #include <boost/filesystem.hpp>
|
---|
| 50 | #include <boost/foreach.hpp>
|
---|
| 51 | #include <boost/mpl/for_each.hpp>
|
---|
| 52 |
|
---|
| 53 | #include <algorithm>
|
---|
| 54 | #include <functional>
|
---|
| 55 | #include <iostream>
|
---|
| 56 | #include <string>
|
---|
| 57 |
|
---|
| 58 | #include "Actions/PotentialAction/FitFragmentPartialChargesAction.hpp"
|
---|
| 59 |
|
---|
| 60 | #include "Potentials/PartialNucleiChargeFitter.hpp"
|
---|
| 61 |
|
---|
| 62 | #include "AtomIdSet.hpp"
|
---|
| 63 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
| 64 | #include "Element/element.hpp"
|
---|
| 65 | #include "Element/periodentafel.hpp"
|
---|
| 66 | #include "Fragmentation/Homology/AtomFragmentsMap.hpp"
|
---|
| 67 | #include "Fragmentation/Homology/HomologyGraph.hpp"
|
---|
| 68 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
|
---|
| 69 | #include "Fragmentation/Summation/Containers/PartialChargesFused.hpp"
|
---|
| 70 | #include "Fragmentation/Summation/Containers/PartialChargesMap.hpp"
|
---|
| 71 | #include "Fragmentation/Summation/SetValues/IndexedPartialCharges.hpp"
|
---|
| 72 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
|
---|
| 73 | #include "Fragmentation/Summation/ZeroInstanceInitializer.hpp"
|
---|
| 74 | #include "FunctionApproximation/Extractors.hpp"
|
---|
| 75 | #include "Potentials/PartialNucleiChargeFitter.hpp"
|
---|
| 76 | #include "Potentials/Particles/ParticleFactory.hpp"
|
---|
| 77 | #include "Potentials/Particles/ParticleRegistry.hpp"
|
---|
| 78 | #include "Potentials/SerializablePotential.hpp"
|
---|
| 79 | #include "World.hpp"
|
---|
| 80 |
|
---|
| 81 | using namespace MoleCuilder;
|
---|
| 82 |
|
---|
| 83 | // and construct the stuff
|
---|
| 84 | #include "FitFragmentPartialChargesAction.def"
|
---|
| 85 | #include "Action_impl_pre.hpp"
|
---|
| 86 | /** =========== define the function ====================== */
|
---|
| 87 |
|
---|
| 88 | namespace detail {
|
---|
| 89 | typedef std::map<KeySet, HomologyGraph> KeysetsToGraph_t;
|
---|
| 90 |
|
---|
| 91 | typedef std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> GraphFittedChargeMap_t;
|
---|
| 92 |
|
---|
| 93 | typedef std::map<atomId_t, double> fitted_charges_t;
|
---|
| 94 |
|
---|
| 95 | typedef std::map<HomologyGraph, size_t> GraphIndex_t;
|
---|
| 96 |
|
---|
| 97 | typedef std::set<size_t> AtomsGraphIndices_t;
|
---|
| 98 | typedef boost::bimaps::bimap<
|
---|
| 99 | boost::bimaps::multiset_of<AtomsGraphIndices_t>,
|
---|
| 100 | atomId_t > GraphIndices_t;
|
---|
| 101 |
|
---|
| 102 | typedef std::map<std::set<size_t>, std::map<atomicNumber_t, std::string> > AtomParticleNames_t;
|
---|
| 103 |
|
---|
| 104 | typedef std::map<std::set<size_t>, std::string> GraphToNameMap_t;
|
---|
| 105 |
|
---|
| 106 | typedef std::map<KeySet, IndexedPartialCharges::values_t> fittedcharges_per_fragment_t;
|
---|
| 107 |
|
---|
| 108 | typedef std::map<AtomFragmentsMap::indices_t, KeySet> indices_to_keysets_t;
|
---|
| 109 |
|
---|
| 110 | typedef std::map< std::string, std::pair<double, size_t> > average_charge_map_t;
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | static void enforceZeroTotalCharge(
|
---|
| 114 | PartialNucleiChargeFitter::charges_t &_averaged_charges)
|
---|
| 115 | {
|
---|
| 116 | double charge_sum = 0.;
|
---|
| 117 | charge_sum = std::accumulate(_averaged_charges.begin(), _averaged_charges.end(), charge_sum);
|
---|
| 118 | if (fabs(charge_sum) > MYEPSILON) {
|
---|
| 119 | std::transform(
|
---|
| 120 | _averaged_charges.begin(), _averaged_charges.end(),
|
---|
| 121 | _averaged_charges.begin(),
|
---|
| 122 | boost::bind(std::minus<double>(), _1, charge_sum/_averaged_charges.size()));
|
---|
| 123 | }
|
---|
| 124 | charge_sum = 0.;
|
---|
| 125 | charge_sum = std::accumulate(_averaged_charges.begin(), _averaged_charges.end(), charge_sum);
|
---|
| 126 | ASSERT( fabs(charge_sum) < MYEPSILON,
|
---|
| 127 | "enforceZeroTotalCharge() - enforcing neutral net charge failed, "
|
---|
| 128 | +toString(charge_sum)+" is the remaining net charge.");
|
---|
| 129 |
|
---|
| 130 | LOG(2, "DEBUG: final charges with net zero charge are " << _averaged_charges);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | static
|
---|
| 134 | std::set<KeySet> accumulateKeySetsForAtoms(
|
---|
| 135 | const AtomFragmentsMap::AtomFragmentsMap_t &_atommap,
|
---|
| 136 | const std::vector<const atom *> &_selected_atoms)
|
---|
| 137 | {
|
---|
| 138 | std::set<KeySet> fragments;
|
---|
| 139 | for (std::vector<const atom *>::const_iterator iter = _selected_atoms.begin();
|
---|
| 140 | iter != _selected_atoms.end(); ++iter) {
|
---|
| 141 | const atomId_t atomid = (*iter)->getId();
|
---|
| 142 | const AtomFragmentsMap::AtomFragmentsMap_t::const_iterator atomiter =
|
---|
| 143 | _atommap.find(atomid);
|
---|
| 144 | if ((*iter)->getElementNo() != 1) {
|
---|
| 145 | if (atomiter == _atommap.end()) {
|
---|
| 146 | ELOG(2, "There are no fragments associated to " << atomid << ".");
|
---|
| 147 | continue;
|
---|
| 148 | }
|
---|
| 149 | const AtomFragmentsMap::keysets_t &keysets = atomiter->second;
|
---|
| 150 | LOG(2, "DEBUG: atom " << atomid << " has " << keysets.size() << " fragments.");
|
---|
| 151 | fragments.insert( keysets.begin(), keysets.end() );
|
---|
| 152 | } else {
|
---|
| 153 | LOG(3, "DEBUG: Skipping atom " << atomid << " as it's hydrogen.");
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | return fragments;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | static
|
---|
| 160 | void getKeySetsToGraphMapping(
|
---|
| 161 | detail::KeysetsToGraph_t &_keyset_graphs,
|
---|
| 162 | const std::set<KeySet> &_fragments,
|
---|
| 163 | const AtomFragmentsMap &_atomfragments)
|
---|
| 164 | {
|
---|
| 165 | for (std::set<KeySet>::const_iterator fragmentiter = _fragments.begin();
|
---|
| 166 | fragmentiter != _fragments.end(); ++fragmentiter) {
|
---|
| 167 | const KeySet &keyset = *fragmentiter;
|
---|
| 168 | const AtomFragmentsMap::indices_t &forceindices = _atomfragments.getFullKeyset(keyset);
|
---|
| 169 | ASSERT( !forceindices.empty(),
|
---|
| 170 | "getKeySetsToGraphMapping() - force keyset to "+toString(keyset)+" is empty.");
|
---|
| 171 | KeySet forcekeyset;
|
---|
| 172 | forcekeyset.insert(forceindices.begin(), forceindices.end());
|
---|
| 173 | forcekeyset.erase(-1);
|
---|
| 174 | const HomologyGraph graph(forcekeyset);
|
---|
| 175 | LOG(2, "DEBUG: Associating keyset " << forcekeyset << " with graph " << graph);
|
---|
| 176 | _keyset_graphs.insert( std::make_pair(keyset, graph) );
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | static
|
---|
| 181 | bool getPartialChargesForAllIndexSets(
|
---|
| 182 | detail::fittedcharges_per_fragment_t &_fittedcharges_per_fragment,
|
---|
| 183 | const FragmentationResultContainer &_container,
|
---|
| 184 | const std::set<KeySet> &_fragments,
|
---|
| 185 | const double _mask_radius,
|
---|
| 186 | const bool enforceZeroCharge)
|
---|
| 187 | {
|
---|
| 188 | const std::map<JobId_t, MPQCData> &shortrangedata = _container.getShortRangeResults();
|
---|
| 189 | const std::map<JobId_t, VMGData> &longrangedata = _container.getLongRangeResults();
|
---|
| 190 | const KeySetsContainer &keysets = _container.getKeySets();
|
---|
| 191 |
|
---|
| 192 | std::map<JobId_t, MPQCData>::const_iterator shortiter = shortrangedata.begin();
|
---|
| 193 | std::map<JobId_t, VMGData>::const_iterator longiter = longrangedata.begin();
|
---|
| 194 | KeySetsContainer::ArrayOfIntVectors::const_iterator keysetiter = keysets.KeySets.begin();
|
---|
| 195 | for (; keysetiter != keysets.KeySets.end(); ++keysetiter, ++shortiter, ++longiter) {
|
---|
| 196 | // check whether its an allowed keyset (atomfragments has keyset, not forcekeyset)
|
---|
| 197 | KeySet currentset;
|
---|
| 198 | currentset.insert(keysetiter->begin(), keysetiter->end());
|
---|
| 199 | if (_fragments.find(currentset) == _fragments.end())
|
---|
| 200 | continue;
|
---|
| 201 |
|
---|
| 202 | // obtain positions and sampled potential
|
---|
| 203 | const Fragment::positions_t &fragmentpositions = shortiter->second.positions;
|
---|
| 204 | const SamplingGrid &potential = longiter->second.both_sampled_potential;
|
---|
| 205 | if ((potential.level == 0)
|
---|
| 206 | || ((potential.begin[0] == potential.end[0])
|
---|
| 207 | && (potential.begin[1] == potential.end[1])
|
---|
| 208 | && (potential.begin[2] == potential.end[2]))) {
|
---|
| 209 | ELOG(1, "Sampled grid contains grid made of zero points.");
|
---|
| 210 | return 0;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | // convert std::vector<double> to Vector
|
---|
| 214 | PartialNucleiChargeFitter::positions_t positions;
|
---|
| 215 | positions.reserve(fragmentpositions.size());
|
---|
| 216 | BOOST_FOREACH( Fragment::position_t pos, fragmentpositions) {
|
---|
| 217 | positions.push_back( Vector(pos[0], pos[1], pos[2]) );
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | // fit charges
|
---|
| 221 | PartialNucleiChargeFitter fitter(potential, positions, _mask_radius);
|
---|
| 222 | fitter();
|
---|
| 223 | PartialNucleiChargeFitter::charges_t return_charges = fitter.getSolutionAsCharges_t();
|
---|
| 224 | LOG(2, "DEBUG: fitted charges are " << return_charges);
|
---|
| 225 |
|
---|
| 226 | // make sum of charges zero if desired
|
---|
| 227 | if (enforceZeroCharge)
|
---|
| 228 | enforceZeroTotalCharge(return_charges);
|
---|
| 229 |
|
---|
| 230 | // output status info fitted charges
|
---|
| 231 | LOG(2, "DEBUG: For fragment " << *keysetiter
|
---|
| 232 | << " we have fitted the following charges " << return_charges << ".");
|
---|
| 233 |
|
---|
| 234 | IndexedPartialCharges::values_t values;
|
---|
| 235 | for (PartialNucleiChargeFitter::charges_t::const_iterator chargeiter = return_charges.begin();
|
---|
| 236 | chargeiter != return_charges.end(); ++chargeiter) {
|
---|
| 237 | values.push_back(partial_charge_t(*chargeiter));
|
---|
| 238 | }
|
---|
| 239 | _fittedcharges_per_fragment.insert( std::make_pair(currentset, values) );
|
---|
| 240 | }
|
---|
| 241 | return true;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | static const atom * getNonHydrogenSurrogate(const atom * const _walker)
|
---|
| 245 | {
|
---|
| 246 | const atom * surrogate = _walker;
|
---|
| 247 | if (surrogate->getElementNo() == 1) {
|
---|
| 248 | // it's hydrogen, check its bonding and use its bond partner instead to request
|
---|
| 249 | // keysets
|
---|
| 250 | const BondList &ListOfBonds = surrogate->getListOfBonds();
|
---|
| 251 | if ( ListOfBonds.size() != 1) {
|
---|
| 252 | ELOG(1, "Solitary hydrogen in atom " << surrogate->getId() << " detected.");
|
---|
| 253 | return _walker;
|
---|
| 254 | }
|
---|
| 255 | surrogate = (*ListOfBonds.begin())->GetOtherAtom(surrogate);
|
---|
| 256 | }
|
---|
| 257 | return surrogate;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | static void addToParticleRegistry(
|
---|
| 261 | const ParticleFactory &factory,
|
---|
| 262 | ParticleRegistry ®istry,
|
---|
| 263 | const periodentafel &periode,
|
---|
| 264 | const detail::fitted_charges_t &_fitted_charges,
|
---|
| 265 | const detail::GraphIndices_t &_GraphIndices,
|
---|
| 266 | detail::AtomParticleNames_t &_atom_particlenames)
|
---|
| 267 | {
|
---|
| 268 | /// We step here through the atoms and check for all atoms of the
|
---|
| 269 | /// same element and which belong to the same set of graphs (via the set
|
---|
| 270 | /// of their unique enumeration indices) then they get the same particle
|
---|
| 271 | detail::average_charge_map_t average_charge_map;
|
---|
| 272 | for (detail::fitted_charges_t::const_iterator chargeiter = _fitted_charges.begin();
|
---|
| 273 | chargeiter != _fitted_charges.end(); ++chargeiter) {
|
---|
| 274 | const atomId_t &atomid = chargeiter->first;
|
---|
| 275 | const double &charge = chargeiter->second;
|
---|
| 276 | const atom * const walker = World::getInstance().getAtom(AtomById(atomid));
|
---|
| 277 | ASSERT( walker != NULL,
|
---|
| 278 | "addToParticleRegistry() - atom "+toString(atomid)
|
---|
| 279 | +" not present in the World?");
|
---|
| 280 | const detail::GraphIndices_t::right_const_iterator graphiter =
|
---|
| 281 | _GraphIndices.right.find(atomid);
|
---|
| 282 | ASSERT(graphiter != _GraphIndices.right.end(),
|
---|
| 283 | "addToParticleRegistry() - atom #"+toString(atomid)
|
---|
| 284 | +" not contained in GraphIndices.");
|
---|
| 285 | const detail::AtomParticleNames_t::iterator nameiter =
|
---|
| 286 | _atom_particlenames.find(graphiter->second);
|
---|
| 287 | const atomicNumber_t elementno = walker->getElementNo();
|
---|
| 288 | std::string name;
|
---|
| 289 | if ((nameiter != _atom_particlenames.end()) && (nameiter->second.count(elementno))) {
|
---|
| 290 | name = (nameiter->second)[elementno];
|
---|
| 291 | detail::average_charge_map_t::iterator averageiter = average_charge_map.find(name);
|
---|
| 292 | ASSERT( averageiter != average_charge_map.end(),
|
---|
| 293 | "addToParticleRegistry() - could not find name "+toString(nameiter->second)
|
---|
| 294 | +" in average charge map.");
|
---|
| 295 | // add charge to particle
|
---|
| 296 | averageiter->second.first += charge;
|
---|
| 297 | ++(averageiter->second.second);
|
---|
| 298 | LOG(1, "INFO: Adding additional charge " << charge << " to particle " << name << " for atom "
|
---|
| 299 | << *walker << " with element " << elementno << ".");
|
---|
| 300 | } else {
|
---|
| 301 | if (nameiter == _atom_particlenames.end())
|
---|
| 302 | _atom_particlenames.insert(
|
---|
| 303 | std::make_pair(graphiter->second, std::map<atomicNumber_t, std::string>()) );
|
---|
| 304 | name = Particle::findFreeName(periode, elementno);
|
---|
| 305 | _atom_particlenames[graphiter->second][elementno] = name;
|
---|
| 306 | LOG(1, "INFO: Adding particle " << name << " for atom "
|
---|
| 307 | << *walker << " with element " << elementno << " and initial charge "
|
---|
| 308 | << charge << ".");
|
---|
| 309 | factory.createInstance(name, elementno, charge);
|
---|
| 310 | #ifndef NDEBUG
|
---|
| 311 | std::pair< detail::average_charge_map_t::iterator, bool> inserter =
|
---|
| 312 | #endif
|
---|
| 313 | average_charge_map.insert( std::make_pair(name, std::make_pair(charge,1)) );
|
---|
| 314 | ASSERT( inserter.second,
|
---|
| 315 | "addToParticleRegistry() - name "+name+" already present in average charge map?");
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | // go through map, average and insert average charge into factory
|
---|
| 320 | for (detail::average_charge_map_t::const_iterator averageiter = average_charge_map.begin();
|
---|
| 321 | averageiter != average_charge_map.end(); ++averageiter) {
|
---|
| 322 | const std::string &name = averageiter->first;
|
---|
| 323 | const double &charge = averageiter->second.first;
|
---|
| 324 | const size_t × = averageiter->second.second;
|
---|
| 325 | ASSERT( times != 0,
|
---|
| 326 | "addToParticleRegistry() - zero times in average charge map is not allowed.");
|
---|
| 327 | Particle *particle = registry.getByName(name);
|
---|
| 328 | particle->charge = charge/(double)times;
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | static bool isNotHydrogen(const atom * const _atom)
|
---|
| 333 | {
|
---|
| 334 | return (_atom->getElementNo() != (atomicNumber_t) 1);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | static struct KeySetSizeComp {
|
---|
| 338 | bool operator() (const KeySet &a, const KeySet &b) { return a.size()<b.size(); }
|
---|
| 339 | } keyset_comparator;
|
---|
| 340 |
|
---|
| 341 | #include <boost/fusion/sequence.hpp>
|
---|
| 342 | #include <boost/mpl/for_each.hpp>
|
---|
| 343 |
|
---|
| 344 | #include "Fragmentation/Summation/AllLevelOrthogonalSummator.hpp"
|
---|
| 345 | #include "Fragmentation/Summation/IndexSet.hpp"
|
---|
| 346 | #include "Fragmentation/Summation/IndexSetContainer.hpp"
|
---|
| 347 | #include "Fragmentation/Summation/SubsetMap.hpp"
|
---|
| 348 | #include "Fragmentation/Summation/Containers/PartialChargesFused.hpp"
|
---|
| 349 | #include "Fragmentation/Summation/Containers/PartialChargesMap.hpp"
|
---|
| 350 | #include "Fragmentation/Summation/SetValues/IndexedPartialCharges.hpp"
|
---|
| 351 |
|
---|
| 352 | ActionState::ptr PotentialFitFragmentPartialChargesAction::performCall()
|
---|
| 353 | {
|
---|
| 354 | // check for selected atoms
|
---|
| 355 | const World &world = World::getConstInstance();
|
---|
| 356 | const std::vector<const atom *> selected_atoms = world.getSelectedAtoms();
|
---|
| 357 | if (selected_atoms.empty()) {
|
---|
| 358 | STATUS("There are no atoms selected for fitting partial charges to.");
|
---|
| 359 | return Action::failure;
|
---|
| 360 | }
|
---|
| 361 | LOG(3, "There are " << selected_atoms.size() << " selected atoms.");
|
---|
| 362 |
|
---|
| 363 | /// obtain possible fragments to each selected atom
|
---|
| 364 | const AtomFragmentsMap &atomfragments = AtomFragmentsMap::getConstInstance();
|
---|
| 365 | if (!atomfragments.checkCompleteness()) {
|
---|
| 366 | ELOG(0, "AtomFragmentsMap failed internal consistency check, missing forcekeysets?");
|
---|
| 367 | return Action::failure;
|
---|
| 368 | }
|
---|
| 369 | const std::set<KeySet> fragments =
|
---|
| 370 | accumulateKeySetsForAtoms( atomfragments.getMap(), selected_atoms);
|
---|
| 371 | const size_t NoNonHydrogens =
|
---|
| 372 | std::count_if(selected_atoms.begin(), selected_atoms.end(), isNotHydrogen);
|
---|
| 373 | if (fragments.size() < NoNonHydrogens) {
|
---|
| 374 | ELOG(0, "Obtained fewer fragments than there are atoms, has AtomFragments been loaded?");
|
---|
| 375 | return Action::failure;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | // get results from container
|
---|
| 379 | FragmentationResultContainer& resultscontainer = FragmentationResultContainer::getInstance();
|
---|
| 380 | #if defined(HAVE_JOBMARKET) && defined(HAVE_VMG)
|
---|
| 381 | const bool DoLongrange = resultscontainer.areFullRangeResultsPresent();
|
---|
| 382 | if (!DoLongrange) {
|
---|
| 383 | ELOG(1, "FragmentationResultContainer does not contain long-range results, cannot fit partial charges.");
|
---|
| 384 | return Action::failure;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | if (resultscontainer.getKeySets().KeySets.empty()) {
|
---|
| 388 | STATUS("There are no results in the container.");
|
---|
| 389 | return Action::failure;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | // reduce given fragments to homologous graphs for later lookup
|
---|
| 393 | detail::KeysetsToGraph_t keyset_graphs;
|
---|
| 394 | detail::fittedcharges_per_fragment_t fittedcharges_per_fragment;
|
---|
| 395 | getKeySetsToGraphMapping(keyset_graphs, fragments, atomfragments);
|
---|
| 396 |
|
---|
| 397 | /// then go through all fragments and get partial charges for each
|
---|
| 398 | const bool status = getPartialChargesForAllIndexSets(
|
---|
| 399 | fittedcharges_per_fragment,
|
---|
| 400 | resultscontainer,
|
---|
| 401 | fragments,
|
---|
| 402 | params.radius.get(),
|
---|
| 403 | params.enforceZeroCharge.get());
|
---|
| 404 | if (!status)
|
---|
| 405 | return Action::failure;
|
---|
| 406 |
|
---|
| 407 | /// obtain average charge for each atom the fitted charges over all its fragments
|
---|
| 408 | detail::fitted_charges_t fitted_charges;
|
---|
| 409 | {
|
---|
| 410 | // place all into boost::fusion map for summation
|
---|
| 411 | std::map<JobId_t, PartialChargesMap_t> PartialCharges_fused;
|
---|
| 412 | std::vector<IndexSet> indexsets;
|
---|
| 413 | JobId_t counter = 0;
|
---|
| 414 | for (detail::fittedcharges_per_fragment_t::const_iterator iter = fittedcharges_per_fragment.begin();
|
---|
| 415 | iter != fittedcharges_per_fragment.end(); ++iter) {
|
---|
| 416 | const KeySet ¤tset = iter->first;
|
---|
| 417 | // place as IndexSet into container
|
---|
| 418 | {
|
---|
| 419 | IndexSet tempset;
|
---|
| 420 | tempset.insert(currentset.begin(), currentset.end());
|
---|
| 421 | indexsets.push_back(tempset);
|
---|
| 422 | }
|
---|
| 423 | LOG(3, "Inserting " << iter->first << " with charges " << iter->second << " into chargemap instance.");
|
---|
| 424 | PartialChargesMap_t chargemap;
|
---|
| 425 | const AtomFragmentsMap::indices_t &full_currentset = atomfragments.getFullKeyset(currentset);
|
---|
| 426 | IndexedPartialCharges::indices_t indices(full_currentset.begin(), full_currentset.end());
|
---|
| 427 | boost::fusion::at_key<PartialChargesFused::partial_charges_t>(chargemap) =
|
---|
| 428 | IndexedPartialCharges(full_currentset, iter->second);
|
---|
| 429 | PartialCharges_fused.insert( std::make_pair(counter++, chargemap) );
|
---|
| 430 | }
|
---|
| 431 | ASSERT( counter == fragments.size(),
|
---|
| 432 | "PotentialFitPartialChargesAction::performCall() - not all fragments' keysets were created?");
|
---|
| 433 | ASSERT( indexsets.size() == fragments.size(),
|
---|
| 434 | "PotentialFitPartialChargesAction::performCall() - not all fragments' keysets were created?");
|
---|
| 435 |
|
---|
| 436 | // prepare index set hierarchy
|
---|
| 437 | std::map< JobId_t, size_t > IdentityLookup;
|
---|
| 438 | size_t MaxLevel = std::max_element(fragments.begin(), fragments.end(), keyset_comparator)->size();
|
---|
| 439 | LOG(3, "The maximum level is " << MaxLevel);
|
---|
| 440 | size_t max_indices = fragments.size();
|
---|
| 441 | LOG(3, "There are " << max_indices << " keysets for the selected atoms.");
|
---|
| 442 | {
|
---|
| 443 | for (size_t index = 0; index < max_indices; ++index)
|
---|
| 444 | IdentityLookup.insert( std::make_pair( (JobId_t)index, index ) );
|
---|
| 445 | }
|
---|
| 446 | IndexSetContainer::ptr container(new IndexSetContainer(indexsets));
|
---|
| 447 | SubsetMap::ptr subsetmap(new SubsetMap(*container));
|
---|
| 448 |
|
---|
| 449 | // and sum up
|
---|
| 450 | PartialChargesMap_t ZeroInstances;
|
---|
| 451 | ZeroInstanceInitializer<PartialChargesMap_t> initZeroInstance(ZeroInstances);
|
---|
| 452 | boost::mpl::for_each<PartialChargesVector_t>(boost::ref(initZeroInstance));
|
---|
| 453 | //!> results per level of summed up partial charges
|
---|
| 454 | std::vector<PartialChargesMap_t> Result_PartialCharges_fused(MaxLevel);
|
---|
| 455 | //!> results per index set in terms of value and contribution
|
---|
| 456 | std::map<
|
---|
| 457 | IndexSet::ptr,
|
---|
| 458 | std::pair<PartialChargesMap_t, PartialChargesMap_t> > Result_perIndexSet_PartialCharges;
|
---|
| 459 | AllLevelOrthogonalSummator<PartialChargesMap_t> partialchargeSummer(
|
---|
| 460 | subsetmap,
|
---|
| 461 | PartialCharges_fused,
|
---|
| 462 | container->getContainer(),
|
---|
| 463 | IdentityLookup,
|
---|
| 464 | Result_PartialCharges_fused,
|
---|
| 465 | Result_perIndexSet_PartialCharges,
|
---|
| 466 | ZeroInstances);
|
---|
| 467 | boost::mpl::for_each<PartialChargesVector_t>(boost::ref(partialchargeSummer));
|
---|
| 468 |
|
---|
| 469 | // TODO: place results into fitted_charges
|
---|
| 470 | const IndexedPartialCharges::indexedvalues_t indexed_partial_charges =
|
---|
| 471 | boost::fusion::at_key<PartialChargesFused::partial_charges_t>(
|
---|
| 472 | Result_PartialCharges_fused.back()
|
---|
| 473 | ).getValues();
|
---|
| 474 | for (IndexedPartialCharges::indexedvalues_t::const_iterator iter = indexed_partial_charges.begin();
|
---|
| 475 | iter != indexed_partial_charges.end(); ++iter)
|
---|
| 476 | fitted_charges.insert( std::make_pair( iter->first, iter->second.charge));
|
---|
| 477 | LOG(3, "Summation has brought forth the following charges per atom index: " << fitted_charges);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | // make a unique list of the present HomologyGraphs
|
---|
| 481 | typedef std::set<HomologyGraph> unique_graphs_t;
|
---|
| 482 | unique_graphs_t unique_graphs;
|
---|
| 483 | for (detail::KeysetsToGraph_t::const_iterator keysetiter = keyset_graphs.begin();
|
---|
| 484 | keysetiter != keyset_graphs.end(); ++keysetiter)
|
---|
| 485 | unique_graphs.insert(keysetiter->second);
|
---|
| 486 |
|
---|
| 487 | /// make Particles be used for every atom that was fitted on the same number of graphs
|
---|
| 488 | detail::GraphIndex_t GraphIndex;
|
---|
| 489 | size_t index = 0;
|
---|
| 490 | for (unique_graphs_t::const_iterator graphiter = unique_graphs.begin();
|
---|
| 491 | graphiter != unique_graphs.end(); ++graphiter) {
|
---|
| 492 | GraphIndex.insert( std::make_pair( *graphiter, index++));
|
---|
| 493 | }
|
---|
| 494 | LOG(2, "DEBUG: There are " << index << " unique graphs in the homology container.");
|
---|
| 495 | ASSERT( index == unique_graphs.size(),
|
---|
| 496 | "PotentialFitFragmentPartialChargesAction::performCall() - could not enumerate all unique graphs?");
|
---|
| 497 |
|
---|
| 498 | // go through every non-hydrogen atom, get all graphs, convert to GraphIndex and store
|
---|
| 499 | detail::GraphIndices_t GraphIndices;
|
---|
| 500 | const AtomFragmentsMap::AtomFragmentsMap_t &atommap = atomfragments.getMap();
|
---|
| 501 | for (std::vector<const atom *>::const_iterator atomiter = selected_atoms.begin();
|
---|
| 502 | atomiter != selected_atoms.end(); ++atomiter) {
|
---|
| 503 | // use the non-hydrogen here
|
---|
| 504 | const atomId_t walkerid = (*atomiter)->getId();
|
---|
| 505 | const atomId_t surrogateid = getNonHydrogenSurrogate(*atomiter)->getId();
|
---|
| 506 | if (surrogateid != walkerid)
|
---|
| 507 | continue;
|
---|
| 508 | const AtomFragmentsMap::AtomFragmentsMap_t::const_iterator keysetsiter =
|
---|
| 509 | atommap.find(walkerid);
|
---|
| 510 | ASSERT(keysetsiter != atommap.end(),
|
---|
| 511 | "PotentialFitFragmentPartialChargesAction::performCall() - we checked already that "
|
---|
| 512 | +toString(surrogateid)+" should be present!");
|
---|
| 513 | const AtomFragmentsMap::keysets_t & keysets = keysetsiter->second;
|
---|
| 514 |
|
---|
| 515 | // go over all fragments associated to this atom
|
---|
| 516 | detail::AtomsGraphIndices_t AtomsGraphIndices;
|
---|
| 517 | for (AtomFragmentsMap::keysets_t::const_iterator keysetsiter = keysets.begin();
|
---|
| 518 | keysetsiter != keysets.end(); ++keysetsiter) {
|
---|
| 519 | const KeySet &keyset = *keysetsiter;
|
---|
| 520 | const std::map<KeySet, HomologyGraph>::const_iterator keysetgraphiter =
|
---|
| 521 | keyset_graphs.find(keyset);
|
---|
| 522 | ASSERT( keysetgraphiter != keyset_graphs.end(),
|
---|
| 523 | "PotentialFitFragmentPartialChargesAction::performCall() - keyset "+toString(keyset)
|
---|
| 524 | +" not contained in keyset_graphs.");
|
---|
| 525 | const HomologyGraph &graph = keysetgraphiter->second;
|
---|
| 526 | const detail::GraphIndex_t::const_iterator indexiter = GraphIndex.find(graph);
|
---|
| 527 | ASSERT( indexiter != GraphIndex.end(),
|
---|
| 528 | "PotentialFitFragmentPartialChargesAction::performCall() - graph "+toString(graph)
|
---|
| 529 | +" not contained in GraphIndex.");
|
---|
| 530 | AtomsGraphIndices.insert( indexiter->second );
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | GraphIndices.insert( detail::GraphIndices_t::value_type(AtomsGraphIndices, walkerid) );
|
---|
| 534 |
|
---|
| 535 | LOG(2, "DEBUG: Atom #" << walkerid << "," << **atomiter << ". has graph indices "
|
---|
| 536 | << AtomsGraphIndices);
|
---|
| 537 | }
|
---|
| 538 | // then graphs from non-hydrogen bond partner for all hydrogens
|
---|
| 539 | for (std::vector<const atom *>::const_iterator atomiter = selected_atoms.begin();
|
---|
| 540 | atomiter != selected_atoms.end(); ++atomiter) {
|
---|
| 541 | // use the non-hydrogen here
|
---|
| 542 | const atomId_t walkerid = (*atomiter)->getId();
|
---|
| 543 | const atomId_t surrogateid = getNonHydrogenSurrogate((*atomiter))->getId();
|
---|
| 544 | if (surrogateid == walkerid)
|
---|
| 545 | continue;
|
---|
| 546 | detail::GraphIndices_t::right_const_iterator graphiter = GraphIndices.right.find(surrogateid);
|
---|
| 547 | ASSERT( graphiter != GraphIndices.right.end(),
|
---|
| 548 | "PotentialFitFragmentPartialChargesAction::performCall() - atom #"+toString(surrogateid)
|
---|
| 549 | +" not contained in GraphIndices.");
|
---|
| 550 | const detail::AtomsGraphIndices_t &AtomsGraphIndices = graphiter->second;
|
---|
| 551 | GraphIndices.insert( detail::GraphIndices_t::value_type(AtomsGraphIndices, walkerid) );
|
---|
| 552 | LOG(2, "DEBUG: Hydrogen #" << walkerid << ", " << **atomiter
|
---|
| 553 | << ", has graph indices " << AtomsGraphIndices);
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | /// place all fitted (and now averaged) charges into ParticleRegistry
|
---|
| 557 | detail::AtomParticleNames_t atom_particlenames;
|
---|
| 558 | addToParticleRegistry(
|
---|
| 559 | ParticleFactory::getConstInstance(),
|
---|
| 560 | ParticleRegistry::getInstance(),
|
---|
| 561 | *World::getInstance().getPeriode(),
|
---|
| 562 | fitted_charges,
|
---|
| 563 | GraphIndices,
|
---|
| 564 | atom_particlenames);
|
---|
| 565 |
|
---|
| 566 | for (World::AtomSelectionIterator atomiter = World::getInstance().beginAtomSelection();
|
---|
| 567 | atomiter != World::getInstance().endAtomSelection(); ++atomiter) {
|
---|
| 568 | atom * const walker = atomiter->second;
|
---|
| 569 | const atomId_t walkerid = atomiter->first;
|
---|
| 570 | const detail::GraphIndices_t::right_const_iterator graphiter =
|
---|
| 571 | GraphIndices.right.find(walkerid);
|
---|
| 572 | ASSERT( graphiter != GraphIndices.right.end(),
|
---|
| 573 | "PotentialFitFragmentPartialChargesAction::performCall() - cannot find "
|
---|
| 574 | +toString(walkerid)+" in GraphIndices.");
|
---|
| 575 | const detail::AtomsGraphIndices_t &graphindex = graphiter->second;
|
---|
| 576 | const detail::AtomParticleNames_t::const_iterator particlesetiter =
|
---|
| 577 | atom_particlenames.find(graphindex);
|
---|
| 578 | ASSERT( particlesetiter != atom_particlenames.end(),
|
---|
| 579 | "PotentialFitFragmentPartialChargesAction::performCall() - cannot find "
|
---|
| 580 | +toString(graphindex)+" in atom_particlenames.");
|
---|
| 581 | const std::map<atomicNumber_t, std::string>::const_iterator nameiter =
|
---|
| 582 | particlesetiter->second.find(walker->getElementNo());
|
---|
| 583 | ASSERT( nameiter != particlesetiter->second.end(),
|
---|
| 584 | "PotentialFitFragmentPartialChargesAction::performCall() - ");
|
---|
| 585 | walker->setParticleName(nameiter->second);
|
---|
| 586 | LOG(1, "INFO: atom " << *walker << " received the following particle "
|
---|
| 587 | << walker->getParticleName());
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | return Action::success;
|
---|
| 591 | #else
|
---|
| 592 | ELOG(0, "Long-range support not compiled in, cannot fit partial charges to fragment's potential grids.");
|
---|
| 593 | return Action::failure;
|
---|
| 594 | #endif
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | ActionState::ptr PotentialFitFragmentPartialChargesAction::performUndo(ActionState::ptr _state) {
|
---|
| 598 | return Action::success;
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | ActionState::ptr PotentialFitFragmentPartialChargesAction::performRedo(ActionState::ptr _state){
|
---|
| 602 | return Action::success;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | bool PotentialFitFragmentPartialChargesAction::canUndo() {
|
---|
| 606 | return false;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | bool PotentialFitFragmentPartialChargesAction::shouldUndo() {
|
---|
| 610 | return false;
|
---|
| 611 | }
|
---|
| 612 | /** =========== end of function ====================== */
|
---|