/*
* 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 .
*/
/*
* AddPotentialAction.cpp
*
* Created on: Aug 17, 2025
* Author: heber
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "Actions/PotentialAction/AddPotentialAction.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 "AddPotentialAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr PotentialAddPotentialAction::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();
EmpiricalPotential * const potential = PotentialFactory::getConstInstance().createInstance(params.potential_type.get(), chargenumbers);
if (potential == NULL) {
STATUS("Failed to instantiate a potential of type "+params.potential_type.get()+".");
return Action::failure;
}
if (!registry.isPresentByName(potential->getName())) {
registry.registerInstance(potential);
} else {
STATUS("A potential of type "+params.potential_type.get()+" with charges "+toString(chargenumbers)+" is already registered.");
delete potential;
return Action::failure;
}
STATUS("Successfully registered a new potential of type "+params.potential_type.get()+".");
PotentialAddPotentialState * const UndoState = new PotentialAddPotentialState(potential->getName(), chargenumbers, params);
return ActionState::ptr(UndoState);
}
ActionState::ptr PotentialAddPotentialAction::performUndo(ActionState::ptr _state) {
PotentialAddPotentialState * const state = assert_cast(_state.get());
PotentialRegistry ®istry = PotentialRegistry::getInstance();
EmpiricalPotential *potential = registry.getByName(state->potential_name);
if (potential == NULL) {
ELOG(1, "UNDO: Could not find a potential named "+state->potential_name+" to remove.");
return Action::failure;
}
registry.unregisterInstance(potential);
delete potential;
return ActionState::ptr(_state);
}
ActionState::ptr PotentialAddPotentialAction::performRedo(ActionState::ptr _state){
PotentialAddPotentialState * const state = assert_cast(_state.get());
// We assume here that the potential may be safely added again and drop all checks
PotentialRegistry ®istry = PotentialRegistry::getInstance();
EmpiricalPotential * const potential = PotentialFactory::getConstInstance().createInstance(state->params.potential_type.get(), state->chargenumbers);
registry.registerInstance(potential);
return ActionState::ptr(_state);
}
bool PotentialAddPotentialAction::canUndo() {
return true;
}
bool PotentialAddPotentialAction::shouldUndo() {
return true;
}
/** =========== end of function ====================== */