| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2025 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 | * RemovePotentialAction.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Aug 17, 2025 | 
|---|
| 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 "Actions/PotentialAction/RemovePotentialAction.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include "CodePatterns/Log.hpp" | 
|---|
| 40 | #include "CodePatterns/toString.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "Potentials/PotentialFactory.hpp" | 
|---|
| 43 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| 44 | #include "Potentials/PotentialTrainer.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | using namespace MoleCuilder; | 
|---|
| 47 |  | 
|---|
| 48 | class EmpiricalPotential; | 
|---|
| 49 |  | 
|---|
| 50 | // and construct the stuff | 
|---|
| 51 | #include "RemovePotentialAction.def" | 
|---|
| 52 | #include "Action_impl_pre.hpp" | 
|---|
| 53 | /** =========== define the function ====================== */ | 
|---|
| 54 |  | 
|---|
| 55 | ActionState::ptr PotentialRemovePotentialAction::performCall() | 
|---|
| 56 | { | 
|---|
| 57 | const unsigned int particleTypeNumber = PotentialFactory::getConstInstance().getPotentialsParticleTypeNumber(params.potential_type.get()); | 
|---|
| 58 | if (params.charges.get().size() != particleTypeNumber) { | 
|---|
| 59 | STATUS("Insufficient charges given!"); | 
|---|
| 60 | return Action::failure; | 
|---|
| 61 | } | 
|---|
| 62 | // charges specify the potential type | 
|---|
| 63 | SerializablePotential::ParticleTypes_t chargenumbers = PotentialTrainer::getNumbersFromElements(params.charges.get()); | 
|---|
| 64 |  | 
|---|
| 65 | PotentialRegistry ®istry = PotentialRegistry::getInstance(); | 
|---|
| 66 | /* The potential name is made up from the type and the charges. This is deeply hidden inside SerializablePotential | 
|---|
| 67 | * Hence, we just instantiate a new potential to obtain the name and delete it afterwards. | 
|---|
| 68 | */ | 
|---|
| 69 | EmpiricalPotential * potential = PotentialFactory::getConstInstance().createInstance(params.potential_type.get(), chargenumbers); | 
|---|
| 70 | if (potential == NULL) { | 
|---|
| 71 | ELOG(1, "A potential with name "+params.potential_type.get()+" and "+toString<SerializablePotential::ParticleTypes_t>(chargenumbers)+" charges is illegal."); | 
|---|
| 72 | return Action::failure; | 
|---|
| 73 | } | 
|---|
| 74 | const std::string potential_name = potential->getName(); | 
|---|
| 75 | delete potential; | 
|---|
| 76 | if (!registry.isPresentByName(potential_name)) { | 
|---|
| 77 | STATUS("A potential with name "+potential_name+" is not registered."); | 
|---|
| 78 | return Action::failure; | 
|---|
| 79 | } | 
|---|
| 80 | potential = registry.getByName(potential_name); | 
|---|
| 81 | PotentialRemovePotentialState * const UndoState = new PotentialRemovePotentialState(potential_name, chargenumbers, potential->getParameters(), params); | 
|---|
| 82 | registry.unregisterInstance(potential); | 
|---|
| 83 | delete potential; | 
|---|
| 84 | STATUS("Successfully removed potential "+potential_name); | 
|---|
| 85 |  | 
|---|
| 86 | return ActionState::ptr(UndoState); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | ActionState::ptr PotentialRemovePotentialAction::performUndo(ActionState::ptr _state) { | 
|---|
| 90 | PotentialRemovePotentialState * const state = assert_cast<PotentialRemovePotentialState*>(_state.get()); | 
|---|
| 91 | EmpiricalPotential * const potential = PotentialFactory::getConstInstance().createInstance(state->params.potential_type.get(), state->chargenumbers, state->potential_params); | 
|---|
| 92 | if (potential == NULL) { | 
|---|
| 93 | ELOG(1, "UNDO: Could not find a potential named "+state->params.potential_type.get()+" to add."); | 
|---|
| 94 | return Action::failure; | 
|---|
| 95 | } | 
|---|
| 96 | PotentialRegistry::getInstance().registerInstance(potential); | 
|---|
| 97 | return ActionState::ptr(_state); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | ActionState::ptr PotentialRemovePotentialAction::performRedo(ActionState::ptr _state){ | 
|---|
| 101 | PotentialRemovePotentialState * const state = assert_cast<PotentialRemovePotentialState*>(_state.get()); | 
|---|
| 102 |  | 
|---|
| 103 | PotentialRegistry ®istry = PotentialRegistry::getInstance(); | 
|---|
| 104 | EmpiricalPotential * potential = registry.getByName(state->potential_name); | 
|---|
| 105 | if (potential == NULL) { | 
|---|
| 106 | ELOG(1, "REDO: Could not find a potential named "+state->potential_name+" to remove."); | 
|---|
| 107 | return Action::failure; | 
|---|
| 108 | } | 
|---|
| 109 | registry.unregisterInstance(potential); | 
|---|
| 110 | delete potential; | 
|---|
| 111 | return ActionState::ptr(_state); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | bool PotentialRemovePotentialAction::canUndo() { | 
|---|
| 115 | return true; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | bool PotentialRemovePotentialAction::shouldUndo() { | 
|---|
| 119 | return true; | 
|---|
| 120 | } | 
|---|
| 121 | /** =========== end of function ====================== */ | 
|---|