source: src/Actions/PotentialAction/AddPotentialAction.cpp@ a4b917

Candidate_v1.7.0 stable
Last change on this file since a4b917 was a4b917, checked in by Frederik Heber <frederik.heber@…>, 6 weeks ago

Adds add and remove potential actions.

  • DOC: Adds missing actions to userguide.
  • TESTS: added actions test case with undo, redo and various cases.
  • Property mode set to 100644
File size: 4.2 KB
Line 
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 * AddPotentialAction.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/AddPotentialAction.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
46using namespace MoleCuilder;
47
48class EmpiricalPotential;
49
50// and construct the stuff
51#include "AddPotentialAction.def"
52#include "Action_impl_pre.hpp"
53/** =========== define the function ====================== */
54
55ActionState::ptr PotentialAddPotentialAction::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 &registry = PotentialRegistry::getInstance();
66 EmpiricalPotential * const potential = PotentialFactory::getConstInstance().createInstance(params.potential_type.get(), chargenumbers);
67 if (potential == NULL) {
68 STATUS("Failed to instantiate a potential of type "+params.potential_type.get()+".");
69 return Action::failure;
70 }
71 if (!registry.isPresentByName(potential->getName())) {
72 registry.registerInstance(potential);
73 } else {
74 STATUS("A potential of type "+params.potential_type.get()+" with charges "+toString<SerializablePotential::ParticleTypes_t>(chargenumbers)+" is already registered.");
75 delete potential;
76 return Action::failure;
77 }
78 STATUS("Successfully registered a new potential of type "+params.potential_type.get()+".");
79 PotentialAddPotentialState * const UndoState = new PotentialAddPotentialState(potential->getName(), chargenumbers, params);
80 return ActionState::ptr(UndoState);
81}
82
83ActionState::ptr PotentialAddPotentialAction::performUndo(ActionState::ptr _state) {
84 PotentialAddPotentialState * const state = assert_cast<PotentialAddPotentialState*>(_state.get());
85
86 PotentialRegistry &registry = PotentialRegistry::getInstance();
87 EmpiricalPotential *potential = registry.getByName(state->potential_name);
88 if (potential == NULL) {
89 ELOG(1, "UNDO: Could not find a potential named "+state->potential_name+" to remove.");
90 return Action::failure;
91 }
92 registry.unregisterInstance(potential);
93 delete potential;
94 return ActionState::ptr(_state);
95}
96
97ActionState::ptr PotentialAddPotentialAction::performRedo(ActionState::ptr _state){
98 PotentialAddPotentialState * const state = assert_cast<PotentialAddPotentialState*>(_state.get());
99
100 // We assume here that the potential may be safely added again and drop all checks
101 PotentialRegistry &registry = PotentialRegistry::getInstance();
102 EmpiricalPotential * const potential = PotentialFactory::getConstInstance().createInstance(state->params.potential_type.get(), state->chargenumbers);
103 registry.registerInstance(potential);
104 return ActionState::ptr(_state);
105}
106
107bool PotentialAddPotentialAction::canUndo() {
108 return true;
109}
110
111bool PotentialAddPotentialAction::shouldUndo() {
112 return true;
113}
114/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.