1 | /*
|
---|
2 | * RepeatBoxAction.cpp
|
---|
3 | *
|
---|
4 | * Created on: May 12, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Helpers/MemDebug.hpp"
|
---|
9 |
|
---|
10 | #include "Actions/WorldAction/RepeatBoxAction.hpp"
|
---|
11 | #include "atom.hpp"
|
---|
12 | #include "log.hpp"
|
---|
13 | #include "molecule.hpp"
|
---|
14 | #include "vector.hpp"
|
---|
15 | #include "Matrix.hpp"
|
---|
16 | #include "verbose.hpp"
|
---|
17 | #include "World.hpp"
|
---|
18 |
|
---|
19 | #include <iostream>
|
---|
20 | #include <string>
|
---|
21 |
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 | #include "UIElements/UIFactory.hpp"
|
---|
25 | #include "UIElements/Dialog.hpp"
|
---|
26 | #include "Actions/MapOfActions.hpp"
|
---|
27 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
28 | #include "Descriptors/MoleculePtrDescriptor.hpp"
|
---|
29 |
|
---|
30 | const char WorldRepeatBoxAction::NAME[] = "repeat-box";
|
---|
31 |
|
---|
32 | WorldRepeatBoxAction::WorldRepeatBoxAction() :
|
---|
33 | Action(NAME)
|
---|
34 | {}
|
---|
35 |
|
---|
36 | WorldRepeatBoxAction::~WorldRepeatBoxAction()
|
---|
37 | {}
|
---|
38 |
|
---|
39 | Action::state_ptr WorldRepeatBoxAction::performCall() {
|
---|
40 | Dialog *dialog = UIFactory::getInstance().makeDialog();
|
---|
41 | Vector Repeater;
|
---|
42 | int count;
|
---|
43 | const element ** Elements;
|
---|
44 | molecule *mol = NULL;
|
---|
45 | int j = 0;
|
---|
46 | atom *Walker = NULL;
|
---|
47 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
48 |
|
---|
49 | dialog->queryVector(NAME, &Repeater, World::getInstance().getDomain(), false, MapOfActions::getInstance().getDescription(NAME));
|
---|
50 | //dialog->queryMolecule("molecule-by-id", &mol,MapOfActions::getInstance().getDescription("molecule-by-id"));
|
---|
51 | vector<molecule *> AllMolecules;
|
---|
52 | if (mol != NULL) {
|
---|
53 | DoLog(0) && (Log() << Verbose(0) << "Using molecule " << mol->name << "." << endl);
|
---|
54 | AllMolecules = World::getInstance().getAllMolecules(MoleculeByPtr(mol));
|
---|
55 | } else {
|
---|
56 | DoLog(0) && (Log() << Verbose(0) << "Using all molecules." << endl);
|
---|
57 | AllMolecules = World::getInstance().getAllMolecules();
|
---|
58 | }
|
---|
59 |
|
---|
60 | if(dialog->display()) {
|
---|
61 | (cout << "Repeating box " << Repeater << " times for (x,y,z) axis." << endl);
|
---|
62 | double * const cell_size = World::getInstance().getDomain();
|
---|
63 | double *M_double = ReturnFullMatrixforSymmetric(cell_size);
|
---|
64 | Matrix M = Matrix(M_double);
|
---|
65 | delete[] M_double;
|
---|
66 | Vector x,y;
|
---|
67 | int n[NDIM];
|
---|
68 | for (int axis = 0; axis < NDIM; axis++) {
|
---|
69 | Repeater[axis] = floor(Repeater[axis]);
|
---|
70 | if (Repeater[axis] < 1) {
|
---|
71 | DoeLog(1) && (eLog()<< Verbose(1) << "Repetition factor must be greater than 1!" << endl);
|
---|
72 | Repeater[axis] = 1;
|
---|
73 | }
|
---|
74 | cell_size[(abs(axis+1) == 2) ? 2 : ((abs(axis+2) == 3) ? 5 : 0)] *= Repeater[axis];
|
---|
75 | }
|
---|
76 |
|
---|
77 | molecule *newmol = NULL;
|
---|
78 | Vector ** vectors = NULL;
|
---|
79 | for (n[0] = 0; n[0] < Repeater[0]; n[0]++) {
|
---|
80 | y[0] = n[0];
|
---|
81 | for (n[1] = 0; n[1] < Repeater[1]; n[1]++) {
|
---|
82 | y[1] = n[1];
|
---|
83 | for (n[2] = 0; n[2] < Repeater[2]; n[2]++) {
|
---|
84 | y[2] = n[2];
|
---|
85 | if ((n[0] == 0) && (n[1] == 0) && (n[2] == 0))
|
---|
86 | continue;
|
---|
87 | for (vector<molecule *>::iterator MolRunner = AllMolecules.begin(); MolRunner != AllMolecules.end(); ++MolRunner) {
|
---|
88 | mol = *MolRunner;
|
---|
89 | DoLog(1) && (Log() << Verbose(1) << "Current mol is " << mol->name << "." << endl);
|
---|
90 | count = mol->getAtomCount(); // is changed becausing of adding, thus has to be stored away beforehand
|
---|
91 | if (count != 0) { // if there is more than none
|
---|
92 | Elements = new const element *[count];
|
---|
93 | vectors = new Vector *[count];
|
---|
94 | j = 0;
|
---|
95 | for(molecule::iterator AtomRunner = mol->begin(); AtomRunner != mol->end(); ++AtomRunner) {
|
---|
96 | Elements[j] = (*AtomRunner)->type;
|
---|
97 | vectors[j] = &(*AtomRunner)->x;
|
---|
98 | j++;
|
---|
99 | }
|
---|
100 | if (count != j)
|
---|
101 | DoeLog(1) && (eLog()<< Verbose(1) << "AtomCount " << count << " is not equal to number of atoms in molecule " << j << "!" << endl);
|
---|
102 | x = y;
|
---|
103 | x.MatrixMultiplication(M);
|
---|
104 | newmol = World::getInstance().createMolecule();
|
---|
105 | molecules->insert(newmol);
|
---|
106 | for (int k=count;k--;) { // go through every atom of the original cell
|
---|
107 | Walker = World::getInstance().createAtom(); // create a new body
|
---|
108 | Walker->x = (*vectors[k]) + x;
|
---|
109 | Walker->type = Elements[k]; // insert original element
|
---|
110 | cout << "new atom is " << *Walker << endl;
|
---|
111 | newmol->AddAtom(Walker); // and add to the molecule (which increments ElementsInMolecule, AtomCount, ...)
|
---|
112 | }
|
---|
113 | // free memory
|
---|
114 | delete[](Elements);
|
---|
115 | delete[](vectors);
|
---|
116 | } else {
|
---|
117 | DoLog(1) && (Log() << Verbose(1) << "\t ... is empty." << endl);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | delete dialog;
|
---|
124 | return Action::success;
|
---|
125 | } else {
|
---|
126 | delete dialog;
|
---|
127 | return Action::failure;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | Action::state_ptr WorldRepeatBoxAction::performUndo(Action::state_ptr _state) {
|
---|
132 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
133 |
|
---|
134 | return Action::failure;
|
---|
135 | // string newName = state->mol->getName();
|
---|
136 | // state->mol->setName(state->lastName);
|
---|
137 | //
|
---|
138 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
139 | }
|
---|
140 |
|
---|
141 | Action::state_ptr WorldRepeatBoxAction::performRedo(Action::state_ptr _state){
|
---|
142 | return Action::failure;
|
---|
143 | }
|
---|
144 |
|
---|
145 | bool WorldRepeatBoxAction::canUndo() {
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 |
|
---|
149 | bool WorldRepeatBoxAction::shouldUndo() {
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 |
|
---|
153 | const string WorldRepeatBoxAction::getName() {
|
---|
154 | return NAME;
|
---|
155 | }
|
---|