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