/*
* Project: MoleCuilder
* Description: creates and alters molecular systems
* Copyright (C) 2025 Frederik Heber. All rights reserved.
*
*
* This file is part of MoleCuilder.
*
* MoleCuilder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MoleCuilder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MoleCuilder. If not, see .
*/
/*
* RemovePotentialAction.cpp
*
* Created on: Aug 17, 2025
* Author: heber
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "Actions/PotentialAction/RemovePotentialAction.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/toString.hpp"
#include "Potentials/PotentialFactory.hpp"
#include "Potentials/PotentialRegistry.hpp"
#include "Potentials/PotentialTrainer.hpp"
using namespace MoleCuilder;
class EmpiricalPotential;
// and construct the stuff
#include "RemovePotentialAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr PotentialRemovePotentialAction::performCall()
{
const unsigned int particleTypeNumber = PotentialFactory::getConstInstance().getPotentialsParticleTypeNumber(params.potential_type.get());
if (params.charges.get().size() != particleTypeNumber) {
STATUS("Insufficient charges given!");
return Action::failure;
}
// charges specify the potential type
SerializablePotential::ParticleTypes_t chargenumbers = PotentialTrainer::getNumbersFromElements(params.charges.get());
PotentialRegistry ®istry = PotentialRegistry::getInstance();
/* The potential name is made up from the type and the charges. This is deeply hidden inside SerializablePotential
* Hence, we just instantiate a new potential to obtain the name and delete it afterwards.
*/
EmpiricalPotential * potential = PotentialFactory::getConstInstance().createInstance(params.potential_type.get(), chargenumbers);
if (potential == NULL) {
ELOG(1, "A potential with name "+params.potential_type.get()+" and "+toString(chargenumbers)+" charges is illegal.");
return Action::failure;
}
const std::string potential_name = potential->getName();
delete potential;
if (!registry.isPresentByName(potential_name)) {
STATUS("A potential with name "+potential_name+" is not registered.");
return Action::failure;
}
potential = registry.getByName(potential_name);
PotentialRemovePotentialState * const UndoState = new PotentialRemovePotentialState(potential_name, chargenumbers, potential->getParameters(), params);
registry.unregisterInstance(potential);
delete potential;
STATUS("Successfully removed potential "+potential_name);
return ActionState::ptr(UndoState);
}
ActionState::ptr PotentialRemovePotentialAction::performUndo(ActionState::ptr _state) {
PotentialRemovePotentialState * const state = assert_cast(_state.get());
EmpiricalPotential * const potential = PotentialFactory::getConstInstance().createInstance(state->params.potential_type.get(), state->chargenumbers, state->potential_params);
if (potential == NULL) {
ELOG(1, "UNDO: Could not find a potential named "+state->params.potential_type.get()+" to add.");
return Action::failure;
}
PotentialRegistry::getInstance().registerInstance(potential);
return ActionState::ptr(_state);
}
ActionState::ptr PotentialRemovePotentialAction::performRedo(ActionState::ptr _state){
PotentialRemovePotentialState * const state = assert_cast(_state.get());
PotentialRegistry ®istry = PotentialRegistry::getInstance();
EmpiricalPotential * potential = registry.getByName(state->potential_name);
if (potential == NULL) {
ELOG(1, "REDO: Could not find a potential named "+state->potential_name+" to remove.");
return Action::failure;
}
registry.unregisterInstance(potential);
delete potential;
return ActionState::ptr(_state);
}
bool PotentialRemovePotentialAction::canUndo() {
return true;
}
bool PotentialRemovePotentialAction::shouldUndo() {
return true;
}
/** =========== end of function ====================== */