| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2020 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 | * BondifyAction.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Oct 07, 2020 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "CodePatterns/Assert.hpp" | 
|---|
| 38 | #include "CodePatterns/Log.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include <string> | 
|---|
| 41 |  | 
|---|
| 42 | #include "Actions/AtomAction/BondifyAction.hpp" | 
|---|
| 43 | #include "Actions/UndoRedoHelpers.hpp" | 
|---|
| 44 | #include "Atom/atom.hpp" | 
|---|
| 45 | #include "Atom/AtomicInfo.hpp" | 
|---|
| 46 | #include "Bond/bond.hpp" | 
|---|
| 47 | #include "Bond/BondInfo.hpp" | 
|---|
| 48 | #include "Descriptors/AtomsWithinDistanceOfDescriptor.hpp" | 
|---|
| 49 | #include "Graph/BondGraph.hpp" | 
|---|
| 50 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp" | 
|---|
| 51 | #include "RandomNumbers/RandomNumberDistribution.hpp" | 
|---|
| 52 | #include "RandomNumbers/RandomNumberGenerator.hpp" | 
|---|
| 53 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp" | 
|---|
| 54 | #include "World.hpp" | 
|---|
| 55 |  | 
|---|
| 56 | using namespace MoleCuilder; | 
|---|
| 57 |  | 
|---|
| 58 | // and construct the stuff | 
|---|
| 59 | #include "BondifyAction.def" | 
|---|
| 60 | #include "Action_impl_pre.hpp" | 
|---|
| 61 |  | 
|---|
| 62 | typedef std::vector< std::pair<atom *, int> > candidates_t; | 
|---|
| 63 |  | 
|---|
| 64 | const double MAX_DISTANCE = 5.0; | 
|---|
| 65 |  | 
|---|
| 66 | static int getNumberOfHydrogenAtoms( | 
|---|
| 67 | const atom *Walker) | 
|---|
| 68 | { | 
|---|
| 69 | int num_hydrogens = 0; | 
|---|
| 70 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 71 | for (BondList::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 72 | bonditer != ListOfBonds.end(); ++bonditer) { | 
|---|
| 73 | num_hydrogens += (int)((*bonditer)->GetOtherAtom(Walker)->getElementNo() == 1); | 
|---|
| 74 | } | 
|---|
| 75 | return num_hydrogens; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | static candidates_t getCandidatesInVicinity( | 
|---|
| 79 | const BondGraph &BG, | 
|---|
| 80 | const atom * const Walker) | 
|---|
| 81 | { | 
|---|
| 82 | const std::vector< atom *> atoms_in_vicinity = World::getInstance().getAllAtoms( | 
|---|
| 83 | AtomsWithinDistanceOf(MAX_DISTANCE, Walker->getPosition())); | 
|---|
| 84 | candidates_t candidates; | 
|---|
| 85 | for (std::vector<atom *>::const_iterator iter = atoms_in_vicinity.begin(); | 
|---|
| 86 | iter != atoms_in_vicinity.end(); ++iter) { | 
|---|
| 87 | const atom *OtherWalker = *iter; | 
|---|
| 88 | // skip hydrogens | 
|---|
| 89 | if (OtherWalker->getElementNo() == 1) | 
|---|
| 90 | continue; | 
|---|
| 91 | // skip atoms that are bonded already | 
|---|
| 92 | if (OtherWalker->IsBondedTo(Walker)) | 
|---|
| 93 | continue; | 
|---|
| 94 | const double distance = OtherWalker->getPosition().distance(Walker->getPosition()); | 
|---|
| 95 | range<double> typical_distance = BG.getMinMaxDistance(Walker, OtherWalker); | 
|---|
| 96 | if (typical_distance.isInRange(distance)) { | 
|---|
| 97 | int num_hydrogens = getNumberOfHydrogenAtoms(OtherWalker); | 
|---|
| 98 | candidates.push_back( std::make_pair(const_cast<atom *>(OtherWalker), num_hydrogens)); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | return candidates; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | static int getTotalBondDegree( | 
|---|
| 105 | const atom * Walker) | 
|---|
| 106 | { | 
|---|
| 107 | int num_degreed_bonds = 0; | 
|---|
| 108 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 109 | for (BondList::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 110 | bonditer != ListOfBonds.end(); ++bonditer) { | 
|---|
| 111 | num_degreed_bonds += (*bonditer)->getDegree(); | 
|---|
| 112 | } | 
|---|
| 113 | return num_degreed_bonds; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | class CandidatesAtValence { | 
|---|
| 117 | public: | 
|---|
| 118 | typedef std::multimap<int, atom*> num_hydrogens_atom_map_t; | 
|---|
| 119 | typedef std::vector< std::pair<atom *, int> > candidates_hydrogens_to_remove_t; | 
|---|
| 120 | typedef std::vector<candidates_hydrogens_to_remove_t> all_candidates_t; | 
|---|
| 121 |  | 
|---|
| 122 | int max_hydrogens; | 
|---|
| 123 |  | 
|---|
| 124 | CandidatesAtValence(const int _max_hydrogens) : max_hydrogens(_max_hydrogens) {} | 
|---|
| 125 |  | 
|---|
| 126 | void setMaxHydrogens(const int _max_hydrogens) { | 
|---|
| 127 | max_hydrogens = _max_hydrogens; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | all_candidates_t operator()( | 
|---|
| 131 | const int open_valence, | 
|---|
| 132 | const num_hydrogens_atom_map_t& sorted_candidates) const | 
|---|
| 133 | { | 
|---|
| 134 | all_candidates_t all_candidates; | 
|---|
| 135 | if (sorted_candidates.empty()) | 
|---|
| 136 | return all_candidates; | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 139 | /* pick from beginning (i.e. smallest valence first) and gather all | 
|---|
| 140 | * possible candidate sets. | 
|---|
| 141 | */ | 
|---|
| 142 | int remaining_valence = open_valence; | 
|---|
| 143 | num_hydrogens_atom_map_t::const_iterator iter = sorted_candidates.begin(); | 
|---|
| 144 | candidates_hydrogens_to_remove_t candidate_set; | 
|---|
| 145 | exploreCandidateSetRecursively( | 
|---|
| 146 | iter, sorted_candidates.end(), | 
|---|
| 147 | candidate_set, remaining_valence, | 
|---|
| 148 | all_candidates); | 
|---|
| 149 |  | 
|---|
| 150 | for (all_candidates_t::const_iterator all_iter = all_candidates.begin(); | 
|---|
| 151 | all_iter != all_candidates.end(); ++all_iter) { | 
|---|
| 152 | LOG(2, "DEBUG: Candidate set is " << candidateToString(*all_iter)); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | return all_candidates; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void exploreCandidateSetRecursively( | 
|---|
| 159 | num_hydrogens_atom_map_t::const_iterator iter, | 
|---|
| 160 | num_hydrogens_atom_map_t::const_iterator iter_end, | 
|---|
| 161 | candidates_hydrogens_to_remove_t candidate_set, | 
|---|
| 162 | int remaining_valence, | 
|---|
| 163 | all_candidates_t& all_candidates | 
|---|
| 164 | ) const | 
|---|
| 165 | { | 
|---|
| 166 | const int & num_hydrogens = iter->first; | 
|---|
| 167 | atom * const walker = iter->second; | 
|---|
| 168 | ++iter; | 
|---|
| 169 | const int usable_valence = | 
|---|
| 170 | std::min( | 
|---|
| 171 | std::min(num_hydrogens, remaining_valence), | 
|---|
| 172 | max_hydrogens | 
|---|
| 173 | ); | 
|---|
| 174 | candidate_set.push_back( | 
|---|
| 175 | std::make_pair(walker, 0) | 
|---|
| 176 | ); | 
|---|
| 177 | for (int i=0;i<= usable_valence; ++i) { | 
|---|
| 178 | candidate_set.back().second = i; | 
|---|
| 179 | // check whether we are complete with the candidate_set | 
|---|
| 180 | if (remaining_valence - i <= 0) { | 
|---|
| 181 | // we have a complete set | 
|---|
| 182 | ASSERT(remaining_valence -i == 0, | 
|---|
| 183 | "exploreCandidateSetRecursively() - usable valence "+toString(i) | 
|---|
| 184 | +" must always be less or equal remaining valence "+toString(remaining_valence)); | 
|---|
| 185 | all_candidates.push_back(candidate_set); | 
|---|
| 186 | } else { | 
|---|
| 187 | // not complete, need to recurse further if we can | 
|---|
| 188 | if (iter != iter_end) | 
|---|
| 189 | exploreCandidateSetRecursively( | 
|---|
| 190 | iter, | 
|---|
| 191 | iter_end, | 
|---|
| 192 | candidate_set, | 
|---|
| 193 | remaining_valence-i, | 
|---|
| 194 | all_candidates); | 
|---|
| 195 | } | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | static std::string candidateToString( | 
|---|
| 200 | const candidates_hydrogens_to_remove_t &candidates) | 
|---|
| 201 | { | 
|---|
| 202 | std::stringstream output; | 
|---|
| 203 | for (candidates_hydrogens_to_remove_t::const_iterator iter = candidates.begin(); | 
|---|
| 204 | iter != candidates.end(); ++iter) | 
|---|
| 205 | output << "[" << *iter->first << "," << iter->second << "], "; | 
|---|
| 206 | return output.str(); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 |  | 
|---|
| 210 | }; | 
|---|
| 211 |  | 
|---|
| 212 | /** =========== define the function ====================== */ | 
|---|
| 213 | ActionState::ptr AtomBondifyAction::performCall() { | 
|---|
| 214 | // ensure we have one selected atom | 
|---|
| 215 | World &world = World::getInstance(); | 
|---|
| 216 | const BondGraph &BG = *world.getBondGraph(); | 
|---|
| 217 |  | 
|---|
| 218 | // check precondition | 
|---|
| 219 | const std::vector<atom *> atoms = world.getSelectedAtoms(); | 
|---|
| 220 | if (atoms.size() != 1) { | 
|---|
| 221 | STATUS("Exactly one atom must be selected for bondify."); | 
|---|
| 222 | return Action::failure; | 
|---|
| 223 | } | 
|---|
| 224 | atom *Walker = atoms[0]; | 
|---|
| 225 |  | 
|---|
| 226 | // check whether atom has unoccupied valence orbitals | 
|---|
| 227 | const int num_valence = Walker->getType()->getNoValenceOrbitals(); | 
|---|
| 228 |  | 
|---|
| 229 | // Look at all bond neighbors | 
|---|
| 230 | const int num_degreed_bonds = getTotalBondDegree(Walker); | 
|---|
| 231 | int open_valence = num_valence - num_degreed_bonds; | 
|---|
| 232 | LOG(1, "INFO: We have " << open_valence << " unoccupied  valence"); | 
|---|
| 233 |  | 
|---|
| 234 | if (open_valence <= 0) { | 
|---|
| 235 | STATUS("Nothing to do. Atom is saturated already, we need some unoccupied valence orbitals to have something to work on."); | 
|---|
| 236 | return Action::success; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | // gather all atoms in vicinity | 
|---|
| 240 | candidates_t candidates = getCandidatesInVicinity(BG, Walker); | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 | /** We revert the map's key and values, as we want the entries | 
|---|
| 244 | * sorted by the number of hydrogens. | 
|---|
| 245 | */ | 
|---|
| 246 | std::multimap<int, atom*> sorted_candidates; | 
|---|
| 247 | for (candidates_t::const_iterator iter = candidates.begin(); | 
|---|
| 248 | iter != candidates.end(); ++iter) { | 
|---|
| 249 | if (iter->second > 0) { | 
|---|
| 250 | sorted_candidates.insert( std::make_pair(iter->second, iter->first) ); | 
|---|
| 251 | } | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | /** | 
|---|
| 255 | * Try to find the maximum number of bonds to use instead of having to | 
|---|
| 256 | * add hydrogens in the end. | 
|---|
| 257 | */ | 
|---|
| 258 | CandidatesAtValence::all_candidates_t all_candidates; | 
|---|
| 259 | CandidatesAtValence candidateGetter(params.max_hydrogens.get()); | 
|---|
| 260 | ++open_valence; | 
|---|
| 261 | while ((all_candidates.size() == 0) && (--open_valence > 0)) { | 
|---|
| 262 | all_candidates = candidateGetter(open_valence, sorted_candidates); | 
|---|
| 263 | } | 
|---|
| 264 | if (all_candidates.empty()) { | 
|---|
| 265 | STATUS("Could not find any suitable candidate sets."); | 
|---|
| 266 | return Action::success; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | // pick one set at random | 
|---|
| 270 | const std::string oldtype = RandomNumberDistributionFactory::getConstInstance().getCurrentTypeName(); | 
|---|
| 271 | RandomNumberDistributionFactory::getInstance().setCurrentType("uniform_01"); | 
|---|
| 272 | const RandomNumberGenerator& rng = RandomNumberGeneratorFactory::getConstInstance().makeRandomNumberGenerator(); | 
|---|
| 273 | int id = (int)(rng()*(all_candidates.size()-1)); | 
|---|
| 274 | RandomNumberDistributionFactory::getInstance().setCurrentType(oldtype); | 
|---|
| 275 | CandidatesAtValence::candidates_hydrogens_to_remove_t &picked_set = all_candidates[id]; | 
|---|
| 276 | LOG(1, "INFO: Picked candidate set is " << CandidatesAtValence::candidateToString(picked_set)); | 
|---|
| 277 |  | 
|---|
| 278 | // execute that | 
|---|
| 279 | std::vector<AtomicInfo> removedHydrogens; | 
|---|
| 280 | std::vector<BondInfo> addedBonds; | 
|---|
| 281 | int removed_hydrogens = 0; | 
|---|
| 282 | int added_bonds = 0; | 
|---|
| 283 | for (CandidatesAtValence::candidates_hydrogens_to_remove_t::iterator iter = picked_set.begin(); | 
|---|
| 284 | iter != picked_set.end(); ++iter) { | 
|---|
| 285 | atom * const other_walker = iter->first; | 
|---|
| 286 | const int &num_hydrogens = iter->second; | 
|---|
| 287 |  | 
|---|
| 288 | // remove the hydrogens closest to Walker | 
|---|
| 289 | typedef std::map<double, atom *> closest_map_t; | 
|---|
| 290 | closest_map_t closest_map; | 
|---|
| 291 | const BondList& ListOfBonds = other_walker->getListOfBonds(); | 
|---|
| 292 | for (BondList::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 293 | bonditer != ListOfBonds.end(); ++bonditer) { | 
|---|
| 294 | atom * const hydrogen = (*bonditer)->GetOtherAtom(other_walker); | 
|---|
| 295 | if (hydrogen->getElementNo() != 1) | 
|---|
| 296 | continue; | 
|---|
| 297 | closest_map.insert( | 
|---|
| 298 | std::make_pair( | 
|---|
| 299 | Walker->getPosition().distance(hydrogen->getPosition()), | 
|---|
| 300 | hydrogen | 
|---|
| 301 | ) | 
|---|
| 302 | ); | 
|---|
| 303 | } | 
|---|
| 304 | ASSERT( closest_map.size() >= (size_t)num_hydrogens, | 
|---|
| 305 | "AtomBondifyAction::performCall() - Atom "+toString(*other_walker) | 
|---|
| 306 | +" has less hydrogens "+toString(closest_map.size()) | 
|---|
| 307 | +" than expected "+toString(num_hydrogens)); | 
|---|
| 308 | closest_map_t::iterator removeiter = closest_map.begin(); | 
|---|
| 309 | for (int i=0;i<num_hydrogens; ++i) { | 
|---|
| 310 | ASSERT( removeiter != closest_map.end(), | 
|---|
| 311 | "AtomBondifyAction::performCall() - already at end of closest map?"); | 
|---|
| 312 | LOG(2, "DEBUG: Removing hydrogen at distance " << removeiter->first | 
|---|
| 313 | << " to Walker at " << Walker->getPosition()); | 
|---|
| 314 | removedHydrogens.push_back(AtomicInfo(*removeiter->second)); | 
|---|
| 315 | world.destroyAtom(removeiter->second); | 
|---|
| 316 | removeiter->second = NULL; | 
|---|
| 317 | ++removeiter; | 
|---|
| 318 | ++removed_hydrogens; | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | // add the bond | 
|---|
| 322 | bond::ptr new_bond = Walker->addBond(other_walker); | 
|---|
| 323 | addedBonds.push_back(BondInfo(new_bond)); | 
|---|
| 324 | new_bond->setDegree(num_hydrogens); | 
|---|
| 325 | ++added_bonds; | 
|---|
| 326 | LOG(2, "DEBUG: Added new bond " << *new_bond); | 
|---|
| 327 | } | 
|---|
| 328 | ASSERT( removed_hydrogens == open_valence, | 
|---|
| 329 | "AtomBondifyAction::performCall() - candidate set has not the number of hydrogens " | 
|---|
| 330 | +toString(removed_hydrogens)+" to match the unoccupied valence "+toString(open_valence)); | 
|---|
| 331 |  | 
|---|
| 332 | LOG(1, "INFO: Removed " << removed_hydrogens << " hydrogen atoms and added " | 
|---|
| 333 | << added_bonds << " new bonds."); | 
|---|
| 334 |  | 
|---|
| 335 | return ActionState::ptr(new AtomBondifyState(removedHydrogens, addedBonds, params)); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | ActionState::ptr AtomBondifyAction::performUndo(ActionState::ptr _state) { | 
|---|
| 339 | AtomBondifyState *state = assert_cast<AtomBondifyState*>(_state.get()); | 
|---|
| 340 |  | 
|---|
| 341 | AddAtomsFromAtomicInfo(state->removedHydrogens); | 
|---|
| 342 | RemoveBondsFromBondInfo(state->addedBonds); | 
|---|
| 343 |  | 
|---|
| 344 | return ActionState::ptr(_state); | 
|---|
| 345 | } | 
|---|
| 346 |  | 
|---|
| 347 | ActionState::ptr AtomBondifyAction::performRedo(ActionState::ptr _state){ | 
|---|
| 348 | AtomBondifyState *state = assert_cast<AtomBondifyState*>(_state.get()); | 
|---|
| 349 |  | 
|---|
| 350 | RemoveAtomsFromAtomicInfo(state->removedHydrogens); | 
|---|
| 351 | AddBondsFromBondInfo(state->addedBonds); | 
|---|
| 352 |  | 
|---|
| 353 | return ActionState::ptr(_state); | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | bool AtomBondifyAction::canUndo() { | 
|---|
| 357 | return true; | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | bool AtomBondifyAction::shouldUndo() { | 
|---|
| 361 | return true; | 
|---|
| 362 | } | 
|---|
| 363 | /** =========== end of function ====================== */ | 
|---|