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 "Helpers/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "Actions/WorldAction/RepeatBoxAction.hpp"
|
---|
23 | #include "Actions/ActionRegistry.hpp"
|
---|
24 | #include "atom.hpp"
|
---|
25 | #include "Helpers/Log.hpp"
|
---|
26 | #include "molecule.hpp"
|
---|
27 | #include "LinearAlgebra/Vector.hpp"
|
---|
28 | #include "LinearAlgebra/Matrix.hpp"
|
---|
29 | #include "Helpers/Verbose.hpp"
|
---|
30 | #include "World.hpp"
|
---|
31 | #include "Box.hpp"
|
---|
32 |
|
---|
33 | #include <iostream>
|
---|
34 | #include <string>
|
---|
35 | #include <vector>
|
---|
36 |
|
---|
37 | using namespace std;
|
---|
38 |
|
---|
39 | #include "UIElements/UIFactory.hpp"
|
---|
40 | #include "UIElements/Dialog.hpp"
|
---|
41 | #include "Actions/ValueStorage.hpp"
|
---|
42 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
43 | #include "Descriptors/MoleculePtrDescriptor.hpp"
|
---|
44 |
|
---|
45 | const char WorldRepeatBoxAction::NAME[] = "repeat-box";
|
---|
46 |
|
---|
47 | WorldRepeatBoxAction::WorldRepeatBoxAction() :
|
---|
48 | Action(NAME)
|
---|
49 | {}
|
---|
50 |
|
---|
51 | WorldRepeatBoxAction::~WorldRepeatBoxAction()
|
---|
52 | {}
|
---|
53 |
|
---|
54 | void WorldRepeatBox(Vector &Repeater) {
|
---|
55 | ValueStorage::getInstance().setCurrentValue(WorldRepeatBoxAction::NAME, Repeater);
|
---|
56 | ActionRegistry::getInstance().getActionByName(WorldRepeatBoxAction::NAME)->call(Action::NonInteractive);
|
---|
57 | };
|
---|
58 |
|
---|
59 | void WorldRepeatBoxAction::getParametersfromValueStorage()
|
---|
60 | {};
|
---|
61 |
|
---|
62 | Dialog* WorldRepeatBoxAction::fillDialog(Dialog *dialog) {
|
---|
63 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
64 |
|
---|
65 | dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
|
---|
66 |
|
---|
67 | return dialog;
|
---|
68 | }
|
---|
69 |
|
---|
70 | Action::state_ptr WorldRepeatBoxAction::performCall() {
|
---|
71 | Vector Repeater;
|
---|
72 | int count;
|
---|
73 | const element ** Elements;
|
---|
74 | molecule *mol = NULL;
|
---|
75 | int j = 0;
|
---|
76 | atom *Walker = NULL;
|
---|
77 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
78 |
|
---|
79 | ValueStorage::getInstance().queryCurrentValue(NAME, Repeater);
|
---|
80 |
|
---|
81 | vector<molecule *> AllMolecules;
|
---|
82 | if (mol != NULL) {
|
---|
83 | DoLog(0) && (Log() << Verbose(0) << "Using molecule " << mol->name << "." << endl);
|
---|
84 | AllMolecules = World::getInstance().getAllMolecules(MoleculeByPtr(mol));
|
---|
85 | } else {
|
---|
86 | DoLog(0) && (Log() << Verbose(0) << "Using all molecules." << endl);
|
---|
87 | AllMolecules = World::getInstance().getAllMolecules();
|
---|
88 | }
|
---|
89 |
|
---|
90 | (cout << "Repeating box " << Repeater << " times for (x,y,z) axis." << endl);
|
---|
91 | Matrix M = World::getInstance().getDomain().getM();
|
---|
92 | Matrix newM = M;
|
---|
93 | Vector x,y;
|
---|
94 | int n[NDIM];
|
---|
95 | Matrix repMat;
|
---|
96 | for (int axis = 0; axis < NDIM; axis++) {
|
---|
97 | Repeater[axis] = floor(Repeater[axis]);
|
---|
98 | if (Repeater[axis] < 1) {
|
---|
99 | DoeLog(1) && (eLog()<< Verbose(1) << "Repetition factor must be greater than 1!" << endl);
|
---|
100 | Repeater[axis] = 1;
|
---|
101 | }
|
---|
102 | repMat.at(axis,axis) = Repeater[axis];
|
---|
103 | }
|
---|
104 | newM *= repMat;
|
---|
105 | World::getInstance().setDomain(newM);
|
---|
106 |
|
---|
107 | molecule *newmol = NULL;
|
---|
108 | std::vector<Vector> vectors;
|
---|
109 | for (n[0] = 0; n[0] < Repeater[0]; n[0]++) {
|
---|
110 | y[0] = n[0];
|
---|
111 | for (n[1] = 0; n[1] < Repeater[1]; n[1]++) {
|
---|
112 | y[1] = n[1];
|
---|
113 | for (n[2] = 0; n[2] < Repeater[2]; n[2]++) {
|
---|
114 | y[2] = n[2];
|
---|
115 | if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0))
|
---|
116 | continue;
|
---|
117 | for (vector<molecule *>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
|
---|
118 | mol = *MolRunner;
|
---|
119 | DoLog(1) && (Log() << Verbose(1) << "Current mol is " << mol->name << "." << endl);
|
---|
120 | count = mol->getAtomCount(); // is changed becausing of adding, thus has to be stored away beforehand
|
---|
121 | if (count != 0) { // if there is more than none
|
---|
122 | Elements = new const element *[count];
|
---|
123 | vectors.resize(count);
|
---|
124 | j = 0;
|
---|
125 | for(molecule::iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
|
---|
126 | Elements[j] = (*AtomRunner)->getType();
|
---|
127 | vectors[j] = (*AtomRunner)->getPosition();
|
---|
128 | j++;
|
---|
129 | }
|
---|
130 | if (count != j)
|
---|
131 | DoeLog(1) && (eLog()<< Verbose(1) << "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl);
|
---|
132 | x = y;
|
---|
133 | x *= M;
|
---|
134 | newmol = World::getInstance().createMolecule();
|
---|
135 | molecules->insert(newmol);
|
---|
136 | for (int k=count;k--;) { // go through every atom of the original cell
|
---|
137 | Walker = World::getInstance().createAtom(); // create a new body
|
---|
138 | Walker->setPosition((vectors[k]) + x);
|
---|
139 | Walker->setType(Elements[k]); // insert original element
|
---|
140 | cout << "new atom is " << *Walker << endl;
|
---|
141 | newmol->AddAtom(Walker); // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
|
---|
142 | }
|
---|
143 | // free memory
|
---|
144 | delete[](Elements);
|
---|
145 | } else {
|
---|
146 | DoLog(1) && (Log() << Verbose(1) << "\t ... is empty." << endl);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | return Action::success;
|
---|
153 | }
|
---|
154 |
|
---|
155 | Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) {
|
---|
156 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
157 |
|
---|
158 | return Action::failure;
|
---|
159 | // string newName = state->mol->getName();
|
---|
160 | // state->mol->setName(state->lastName);
|
---|
161 | //
|
---|
162 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
163 | }
|
---|
164 |
|
---|
165 | Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){
|
---|
166 | return Action::failure;
|
---|
167 | }
|
---|
168 |
|
---|
169 | bool WorldRepeatBoxAction::canUndo() {
|
---|
170 | return false;
|
---|
171 | }
|
---|
172 |
|
---|
173 | bool WorldRepeatBoxAction::shouldUndo() {
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 |
|
---|
177 | const string WorldRepeatBoxAction::getName() {
|
---|
178 | return NAME;
|
---|
179 | }
|
---|