Changeset 91f907 for src/Actions


Ignore:
Timestamp:
May 18, 2016, 10:02:54 PM (9 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
ed0f88
Parents:
c1ec8e
git-author:
Frederik Heber <heber@…> (03/07/16 21:46:11)
git-committer:
Frederik Heber <heber@…> (05/18/16 22:02:54)
Message:

FitPartialChargesAction only makes unique particles.

  • i.e. we give the same particle to two given atom, if the fit for either one has been made to the the same set of graphs.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/PotentialAction/FitPartialChargesAction.cpp

    rc1ec8e r91f907  
    4545
    4646#include <boost/bimap.hpp>
     47#include <boost/bimap/multiset_of.hpp>
    4748#include <boost/bind.hpp>
    4849#include <boost/filesystem.hpp>
     
    7879#include "Action_impl_pre.hpp"
    7980/** =========== define the function ====================== */
     81
     82namespace detail {
     83  typedef std::map<KeySet, HomologyGraph> KeysetsToGraph_t;
     84
     85  typedef std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> GraphFittedChargeMap_t;
     86
     87  typedef std::map<atomId_t, double> fitted_charges_t;
     88
     89  typedef std::map<HomologyGraph, size_t> GraphIndex_t;
     90
     91  typedef std::set<size_t> AtomsGraphIndices_t;
     92  typedef boost::bimaps::bimap<
     93      AtomsGraphIndices_t,
     94      boost::bimaps::multiset_of<atomId_t> > GraphIndices_t;
     95
     96  typedef std::map<atomId_t, std::string> AtomParticleNames_t;
     97
     98  typedef std::map<std::set<size_t>, std::string> GraphToNameMap_t;
     99};
    80100
    81101static void enforceZeroTotalCharge(
     
    199219static
    200220void getKeySetsToGraphMapping(
    201     std::map<KeySet, HomologyGraph> &_keyset_graphs,
    202     std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> &_fittedcharges_per_fragment,
     221    detail::KeysetsToGraph_t &_keyset_graphs,
     222    detail::GraphFittedChargeMap_t &_fittedcharges_per_fragment,
    203223    const std::set<KeySet> &_fragments,
    204224    const AtomFragmentsMap &_atomfragments)
     
    222242static
    223243bool getPartialChargesForAllGraphs(
    224     std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> &_fittedcharges_per_fragment,
     244    detail::GraphFittedChargeMap_t &_fittedcharges_per_fragment,
    225245    const HomologyContainer &_homologies,
    226246    const double _mask_radius,
    227247    const bool enforceZeroCharge)
    228248{
    229   typedef std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> GraphFittedChargeMap_t;
    230   for (GraphFittedChargeMap_t::iterator graphiter = _fittedcharges_per_fragment.begin();
     249  for (detail::GraphFittedChargeMap_t::iterator graphiter = _fittedcharges_per_fragment.begin();
    231250      graphiter != _fittedcharges_per_fragment.end(); ++graphiter) {
    232251    const HomologyGraph &graph = graphiter->first;
     
    261280    const atom * const _walker,
    262281    const AtomFragmentsMap &_atomfragments,
    263     const std::map<KeySet, HomologyGraph> &_keyset_graphs,
    264     const std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> &_fittedcharges_per_fragment)
    265 {
    266   typedef std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> GraphFittedChargeMap_t;
     282    const detail::KeysetsToGraph_t &_keyset_graphs,
     283    const detail::GraphFittedChargeMap_t &_fittedcharges_per_fragment)
     284{
    267285  const atom * surrogate = _walker;
    268286  if (surrogate->getElementNo() == 1) {
     
    297315
    298316    // find the associated charge in the charge vector
    299     std::map<KeySet, HomologyGraph>::const_iterator keysetgraphiter =
     317    const std::map<KeySet, HomologyGraph>::const_iterator keysetgraphiter =
    300318        _keyset_graphs.find(keyset);
    301319    ASSERT( keysetgraphiter != _keyset_graphs.end(),
     
    303321        +" not contained in keyset_graphs.");
    304322    const HomologyGraph &graph = keysetgraphiter->second;
    305     const GraphFittedChargeMap_t::const_iterator chargesiter =
     323    const detail::GraphFittedChargeMap_t::const_iterator chargesiter =
    306324        _fittedcharges_per_fragment.find(graph);
    307325    ASSERT(chargesiter != _fittedcharges_per_fragment.end(),
     
    335353    const ParticleFactory &factory,
    336354    const periodentafel &periode,
    337     const std::map<atomId_t, double> &_fitted_charges)
    338 {
    339   typedef std::map<atomId_t, double> fitted_charges_t;
    340   for (fitted_charges_t::const_iterator chargeiter = _fitted_charges.begin();
     355    const detail::fitted_charges_t &_fitted_charges,
     356    const detail::GraphIndices_t &_GraphIndices,
     357    detail::AtomParticleNames_t &_atom_particlenames)
     358{
     359  detail::GraphToNameMap_t GraphToNameMap;
     360  for (detail::fitted_charges_t::const_iterator chargeiter = _fitted_charges.begin();
    341361      chargeiter != _fitted_charges.end(); ++chargeiter) {
    342     const atom * const walker = World::getInstance().getAtom(AtomById(chargeiter->first));
    343     ASSERT( walker != NULL,
    344         "PotentialFitPartialChargesAction::performCall() - atom "
    345         +toString(chargeiter->first)+" not present in the World?");
    346     const double &charge = chargeiter->second;
    347     const atomicNumber_t elementno = walker->getElementNo();
    348     const std::string name = Particle::findFreeName(periode, elementno);
    349     LOG(1, "INFO: Adding particle " << name << " for atom "
    350         << *walker << " with element " << elementno << ", charge " << charge);
    351     factory.createInstance(name, elementno, charge);
     362    const atomId_t &atomid = chargeiter->first;
     363    const detail::GraphIndices_t::right_const_iterator graphiter = _GraphIndices.right.find(atomid);
     364    const detail::GraphToNameMap_t::const_iterator nameiter = GraphToNameMap.find(graphiter->second);
     365    std::string name;
     366    if (nameiter != GraphToNameMap.end()) {
     367      name = nameiter->second;
     368    } else {
     369      const atom * const walker = World::getInstance().getAtom(AtomById(atomid));
     370      ASSERT( walker != NULL,
     371          "addToParticleRegistry() - atom "+toString(atomid)+" not present in the World?");
     372      const double &charge = chargeiter->second;
     373      const atomicNumber_t elementno = walker->getElementNo();
     374      name = Particle::findFreeName(periode, elementno);
     375      GraphToNameMap[graphiter->second] = name;
     376      LOG(1, "INFO: Adding particle " << name << " for atom "
     377          << *walker << " with element " << elementno << ", charge " << charge);
     378      factory.createInstance(name, elementno, charge);
     379    }
     380    _atom_particlenames.insert( std::make_pair(atomid, name) );
    352381  }
    353382}
     
    372401
    373402  // reduce given fragments to homologous graphs to avoid multiple fittings
    374   typedef std::map<KeySet, HomologyGraph> KeysetsToGraph_t;
    375   KeysetsToGraph_t keyset_graphs;
    376   typedef std::map<HomologyGraph, PartialNucleiChargeFitter::charges_t> GraphFittedChargeMap_t;
    377   GraphFittedChargeMap_t fittedcharges_per_fragment;
     403  detail::KeysetsToGraph_t keyset_graphs;
     404  detail::GraphFittedChargeMap_t fittedcharges_per_fragment;
    378405  getKeySetsToGraphMapping(keyset_graphs, fittedcharges_per_fragment, fragments, atomfragments);
    379406
    380407  /// then go through all fragments and get partial charges for each
     408  const HomologyContainer &homologies = World::getInstance().getHomologies();
    381409  const bool status = getPartialChargesForAllGraphs(
    382410      fittedcharges_per_fragment,
    383       World::getInstance().getHomologies(),
     411      homologies,
    384412      params.radius.get(),
    385413      params.enforceZeroCharge.get());
     
    388416
    389417  /// obtain average charge for each atom the fitted charges over all its fragments
    390   typedef std::map<atomId_t, double> fitted_charges_t;
    391   fitted_charges_t fitted_charges;
     418  detail::fitted_charges_t fitted_charges;
    392419  for (World::AtomSelectionConstIterator atomiter = world.beginAtomSelection();
    393420      atomiter != world.endAtomSelection(); ++atomiter) {
     
    403430  }
    404431
     432  /// make Particles be used for every atom that was fitted on the same number of graphs
     433  detail::GraphIndex_t GraphIndex;
     434  size_t index = 0;
     435  for (HomologyContainer::const_key_iterator iter = homologies.key_begin();
     436      iter != homologies.key_end(); iter = homologies.getNextKey(iter)) {
     437    GraphIndex.insert( std::make_pair( *iter, index++));
     438  }
     439  LOG(2, "DEBUG: There are " << index << " unique graphs in the homology container.");
     440
     441  // go through every atom, get all graphs, convert to GraphIndex and store
     442
     443  detail::GraphIndices_t GraphIndices;
     444  const AtomFragmentsMap::AtomFragmentsMap_t &atommap = atomfragments.getMap();
     445  for (World::AtomSelectionConstIterator atomiter = world.beginAtomSelection();
     446      atomiter != world.endAtomSelection(); ++atomiter) {
     447    const atomId_t walkerid = atomiter->first;
     448    const AtomFragmentsMap::AtomFragmentsMap_t::const_iterator keysetsiter =
     449        atommap.find(walkerid);
     450    ASSERT(keysetsiter != atommap.end(),
     451        "PotentialFitPartialChargesAction::performCall() - we checked already that "
     452        +toString(walkerid)+" should be present!");
     453    const AtomFragmentsMap::keysets_t & keysets = keysetsiter->second;
     454
     455    detail::AtomsGraphIndices_t AtomsGraphIndices;
     456
     457    // go over all fragments associated to this atom
     458    for (AtomFragmentsMap::keysets_t::const_iterator keysetsiter = keysets.begin();
     459        keysetsiter != keysets.end(); ++keysetsiter) {
     460      const KeySet &keyset = *keysetsiter;
     461      const std::map<KeySet, HomologyGraph>::const_iterator keysetgraphiter =
     462          keyset_graphs.find(keyset);
     463      ASSERT( keysetgraphiter != keyset_graphs.end(),
     464          "PotentialFitPartialChargesAction::performCall() - keyset "+toString(keyset)
     465          +" not contained in keyset_graphs.");
     466      const HomologyGraph &graph = keysetgraphiter->second;
     467      const detail::GraphIndex_t::const_iterator indexiter = GraphIndex.find(graph);
     468      ASSERT( indexiter != GraphIndex.end(),
     469          "PotentialFitPartialChargesAction::performCall() - graph "+toString(graph)
     470          +" not contained in GraphIndex.");
     471      AtomsGraphIndices.insert( indexiter->second );
     472    }
     473    GraphIndices.left.insert( std::make_pair(AtomsGraphIndices, walkerid) );
     474    LOG(2, "DEBUG: Atom " << *atomiter->second << " has graph indices " << AtomsGraphIndices);
     475  }
     476
    405477  /// place all fitted charges into ParticleRegistry
     478  detail::AtomParticleNames_t atom_particlenames;
    406479  addToParticleRegistry(
    407480      ParticleFactory::getConstInstance(),
    408481      *World::getInstance().getPeriode(),
    409       fitted_charges);
     482      fitted_charges,
     483      GraphIndices,
     484      atom_particlenames);
     485  LOG(1, "INFO: The atoms received the following particles " << atom_particlenames);
    410486
    411487  return Action::success;
Note: See TracChangeset for help on using the changeset viewer.