source: src/Actions/WorldAction/RepeatBoxAction.cpp@ 360c8b

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 360c8b was 45f835, checked in by Frederik Heber <heber@…>, 14 years ago

Added Undo/Redo capabilities to RepeatBoxAction.

  • adapted regression tests.
  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * RepeatBoxAction.cpp
10 *
11 * Created on: May 12, 2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "Descriptors/MoleculePtrDescriptor.hpp"
23#include "atom.hpp"
24#include "CodePatterns/Log.hpp"
25#include "molecule.hpp"
26#include "LinearAlgebra/Vector.hpp"
27#include "LinearAlgebra/RealSpaceMatrix.hpp"
28#include "CodePatterns/Verbose.hpp"
29#include "World.hpp"
30#include "Box.hpp"
31
32#include <iostream>
33#include <set>
34#include <string>
35#include <vector>
36
37#include "Actions/WorldAction/RepeatBoxAction.hpp"
38
39using namespace MoleCuilder;
40
41// and construct the stuff
42#include "RepeatBoxAction.def"
43#include "Action_impl_pre.hpp"
44/** =========== define the function ====================== */
45
46void repeatMoleculesinDomain(Vector Repeater, const std::vector<molecule *> &AllMolecules)
47{
48 int count;
49 const element ** Elements;
50 int j = 0;
51 atom *Walker = NULL;
52 MoleculeListClass *molecules = World::getInstance().getMolecules();
53
54 LOG(0, "STATUS: Repeating box " << Repeater << " times for (x,y,z) axis.");
55
56 // set new domain
57 RealSpaceMatrix M = World::getInstance().getDomain().getM();
58 RealSpaceMatrix newM = M;
59 Vector x,y;
60 int n[NDIM];
61 RealSpaceMatrix repMat;
62 for (int axis = 0; axis < NDIM; axis++) {
63 Repeater[axis] = floor(Repeater[axis]);
64 if (Repeater[axis] < 1) {
65 ELOG(1, "Repetition factor must be greater than 1!");
66 Repeater[axis] = 1;
67 }
68 repMat.at(axis,axis) = Repeater[axis];
69 }
70 newM *= repMat;
71 World::getInstance().setDomain(newM);
72
73 // add molecules in each repeated domain part
74 molecule *newmol = NULL;
75 std::vector<Vector> vectors;
76 for (n[0] = 0; n[0] < Repeater[0]; n[0]++) {
77 y[0] = n[0];
78 for (n[1] = 0; n[1] < Repeater[1]; n[1]++) {
79 y[1] = n[1];
80 for (n[2] = 0; n[2] < Repeater[2]; n[2]++) {
81 y[2] = n[2];
82 if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0))
83 continue;
84 for (vector<molecule *>::const_iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
85 const molecule *mol = *MolRunner;
86 LOG(2, "INFO: Current mol is " << mol->name << ".");
87 count = mol->getAtomCount(); // is changed becausing of adding, thus has to be stored away beforehand
88 if (count != 0) { // if there is more than none
89 Elements = new const element *[count];
90 vectors.resize(count);
91 j = 0;
92 for(molecule::const_iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
93 Elements[j] = (*AtomRunner)->getType();
94 vectors[j] = (*AtomRunner)->getPosition();
95 j++;
96 }
97 if (count != j)
98 ELOG(1, "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!");
99 x = y;
100 x *= M;
101 newmol = World::getInstance().createMolecule();
102 // TODO: Remove this when World don't has deprecated MoleculeListClass anymore
103 molecules->insert(newmol);
104 for (int k=count;k--;) { // go through every atom of the original cell
105 Walker = World::getInstance().createAtom(); // create a new body
106 Walker->setPosition((vectors[k]) + x);
107 Walker->setType(Elements[k]); // insert original element
108 cout << "new atom is " << *Walker << endl;
109 newmol->AddAtom(Walker); // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
110 }
111 // free memory
112 delete[](Elements);
113 } else {
114 LOG(1, "\t ... is empty.");
115 }
116 }
117 }
118 }
119 }
120}
121
122Action::state_ptr WorldRepeatBoxAction::performCall() {
123 molecule *mol = NULL;
124
125 // obtain information
126 getParametersfromValueStorage();
127
128 std::vector<molecule *> AllMolecules;
129 if (mol != NULL) {
130 DoLog(0) && (Log() << Verbose(0) << "Using molecule " << mol->name << "." << endl);
131 AllMolecules = World::getInstance().getAllMolecules(MoleculeByPtr(mol));
132 } else {
133 DoLog(0) && (Log() << Verbose(0) << "Using all molecules." << endl);
134 AllMolecules = World::getInstance().getAllMolecules();
135 }
136
137 // prepare undo state
138 RealSpaceMatrix olddomain = World::getInstance().getDomain().getM();
139 std::set<molecule *> oldmolecules;
140 for(std::vector<molecule *>::const_iterator iter = AllMolecules.begin();
141 iter != AllMolecules.end();
142 ++iter)
143 oldmolecules.insert(*iter);
144 WorldRepeatBoxState *undostate = new WorldRepeatBoxState(olddomain, oldmolecules, params);
145
146 repeatMoleculesinDomain(params.Repeater, AllMolecules);
147
148 // give final box size
149 LOG(0, "Box domain is now " << World::getInstance().getDomain().getM());
150
151 return Action::state_ptr(undostate);
152}
153
154Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) {
155 WorldRepeatBoxState *state = assert_cast<WorldRepeatBoxState*>(_state.get());
156 MoleculeListClass *molecules = World::getInstance().getMolecules();
157
158 // set old domain
159 World::getInstance().setDomain(state->olddomain);
160
161 // remove all added molecules (and their atoms)
162 std::vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
163 for (std::vector<molecule *>::iterator iter = allmolecules.begin();
164 iter != allmolecules.end();
165 ++iter) {
166 if (state->oldmolecules.find(*iter) == state->oldmolecules.end()) {
167 (*iter)->removeAtomsinMolecule();
168 // TODO: Remove this when World don't has deprecated MoleculeListClass anymore
169 molecules->erase(*iter);
170 World::getInstance().destroyMolecule(*iter);
171 }
172 }
173
174 // give final box size
175 LOG(0, "Box domain restored to " << World::getInstance().getDomain().getM());
176
177 return Action::state_ptr(_state);
178}
179
180Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){
181 WorldRepeatBoxState *state = assert_cast<WorldRepeatBoxState*>(_state.get());
182
183 std::vector<molecule *> originalmolecules;
184 for(std::set<molecule *>::const_iterator iter = state->oldmolecules.begin();
185 iter != state->oldmolecules.end();
186 ++iter)
187 originalmolecules.push_back(*iter);
188 repeatMoleculesinDomain(state->params.Repeater, originalmolecules);
189
190 // give final box size
191 LOG(0, "Box domain is again " << World::getInstance().getDomain().getM());
192
193 return Action::state_ptr(_state);
194}
195
196bool WorldRepeatBoxAction::canUndo() {
197 return true;
198}
199
200bool WorldRepeatBoxAction::shouldUndo() {
201 return true;
202}
203/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.