| 1 | /* | 
|---|
| 2 | * PairCorrelationAction.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: May 9, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include "Actions/AnalysisAction/PairCorrelationAction.hpp" | 
|---|
| 11 | #include "analysis_correlation.hpp" | 
|---|
| 12 | #include "boundary.hpp" | 
|---|
| 13 | #include "linkedcell.hpp" | 
|---|
| 14 | #include "verbose.hpp" | 
|---|
| 15 | #include "log.hpp" | 
|---|
| 16 | #include "element.hpp" | 
|---|
| 17 | #include "molecule.hpp" | 
|---|
| 18 | #include "periodentafel.hpp" | 
|---|
| 19 | #include "vector.hpp" | 
|---|
| 20 | #include "World.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include <iostream> | 
|---|
| 23 | #include <string> | 
|---|
| 24 |  | 
|---|
| 25 | using namespace std; | 
|---|
| 26 |  | 
|---|
| 27 | #include "UIElements/UIFactory.hpp" | 
|---|
| 28 | #include "UIElements/Dialog.hpp" | 
|---|
| 29 | #include "Actions/MapOfActions.hpp" | 
|---|
| 30 |  | 
|---|
| 31 | const char AnalysisPairCorrelationAction::NAME[] = "pair-correlation"; | 
|---|
| 32 |  | 
|---|
| 33 | AnalysisPairCorrelationAction::AnalysisPairCorrelationAction() : | 
|---|
| 34 | Action(NAME) | 
|---|
| 35 | {} | 
|---|
| 36 |  | 
|---|
| 37 | AnalysisPairCorrelationAction::~AnalysisPairCorrelationAction() | 
|---|
| 38 | {} | 
|---|
| 39 |  | 
|---|
| 40 | Action::state_ptr AnalysisPairCorrelationAction::performCall() { | 
|---|
| 41 | Dialog *dialog = UIFactory::getInstance().makeDialog(); | 
|---|
| 42 | int ranges[3] = {1, 1, 1}; | 
|---|
| 43 | double BinEnd = 0.; | 
|---|
| 44 | double BinStart = 0.; | 
|---|
| 45 | double BinWidth = 0.; | 
|---|
| 46 | molecule *Boundary = NULL; | 
|---|
| 47 | string outputname; | 
|---|
| 48 | string binoutputname; | 
|---|
| 49 | bool periodic; | 
|---|
| 50 | ofstream output; | 
|---|
| 51 | ofstream binoutput; | 
|---|
| 52 | std::vector< element *> elements; | 
|---|
| 53 | string type; | 
|---|
| 54 | Vector Point; | 
|---|
| 55 | BinPairMap *binmap = NULL; | 
|---|
| 56 | MoleculeListClass *molecules = World::getInstance().getMolecules(); | 
|---|
| 57 |  | 
|---|
| 58 | // first dialog: Obtain which type of correlation | 
|---|
| 59 | dialog->queryString(NAME, &type, MapOfActions::getInstance().getDescription(NAME)); | 
|---|
| 60 | if(dialog->display()) { | 
|---|
| 61 | delete dialog; | 
|---|
| 62 | } else { | 
|---|
| 63 | delete dialog; | 
|---|
| 64 | return Action::failure; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | // second dialog: Obtain parameters specific to this type | 
|---|
| 68 | dialog = UIFactory::getInstance().makeDialog(); | 
|---|
| 69 | if (type == "P") | 
|---|
| 70 | dialog->queryVector("position", &Point, false, MapOfActions::getInstance().getDescription("position")); | 
|---|
| 71 | if (type == "S") | 
|---|
| 72 | dialog->queryMolecule("molecule-by-id", &Boundary, MapOfActions::getInstance().getDescription("molecule-by-id")); | 
|---|
| 73 | dialog->queryElement("elements", &elements, MapOfActions::getInstance().getDescription("elements")); | 
|---|
| 74 | dialog->queryDouble("bin-start", &BinStart, MapOfActions::getInstance().getDescription("bin-start")); | 
|---|
| 75 | dialog->queryDouble("bin-width", &BinWidth, MapOfActions::getInstance().getDescription("bin-width")); | 
|---|
| 76 | dialog->queryDouble("bin-end", &BinEnd, MapOfActions::getInstance().getDescription("bin-end")); | 
|---|
| 77 | dialog->queryString("output-file", &outputname, MapOfActions::getInstance().getDescription("output-file")); | 
|---|
| 78 | dialog->queryString("bin-output-file", &binoutputname, MapOfActions::getInstance().getDescription("bin-output-file")); | 
|---|
| 79 | dialog->queryBoolean("periodic", &periodic, MapOfActions::getInstance().getDescription("periodic")); | 
|---|
| 80 |  | 
|---|
| 81 | if(dialog->display()) { | 
|---|
| 82 | output.open(outputname.c_str()); | 
|---|
| 83 | binoutput.open(binoutputname.c_str()); | 
|---|
| 84 | PairCorrelationMap *correlationmap = NULL; | 
|---|
| 85 | if (type == "E") { | 
|---|
| 86 | PairCorrelationMap *correlationmap = NULL; | 
|---|
| 87 | if (periodic) | 
|---|
| 88 | correlationmap = PeriodicPairCorrelation(World::getInstance().getMolecules(), elements, ranges); | 
|---|
| 89 | else | 
|---|
| 90 | correlationmap = PairCorrelation(World::getInstance().getMolecules(), elements); | 
|---|
| 91 | //OutputCorrelationToSurface(&output, correlationmap); | 
|---|
| 92 | binmap = BinData( correlationmap, BinWidth, BinStart, BinEnd ); | 
|---|
| 93 | } else if (type == "P")  { | 
|---|
| 94 | cout << "Point to correlate to is  " << Point << endl; | 
|---|
| 95 | CorrelationToPointMap *correlationmap = NULL; | 
|---|
| 96 | if (periodic) | 
|---|
| 97 | correlationmap  = PeriodicCorrelationToPoint(molecules, elements, &Point, ranges); | 
|---|
| 98 | else | 
|---|
| 99 | correlationmap = CorrelationToPoint(molecules, elements, &Point); | 
|---|
| 100 | //OutputCorrelationToSurface(&output, correlationmap); | 
|---|
| 101 | binmap = BinData( correlationmap, BinWidth, BinStart, BinEnd ); | 
|---|
| 102 | } else if (type == "S") { | 
|---|
| 103 | ASSERT(Boundary != NULL, "No molecule specified for SurfaceCorrelation."); | 
|---|
| 104 | const double radius = 4.; | 
|---|
| 105 | double LCWidth = 20.; | 
|---|
| 106 | if (BinEnd > 0) { | 
|---|
| 107 | if (BinEnd > 2.*radius) | 
|---|
| 108 | LCWidth = BinEnd; | 
|---|
| 109 | else | 
|---|
| 110 | LCWidth = 2.*radius; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | // get the boundary | 
|---|
| 114 | class Tesselation *TesselStruct = NULL; | 
|---|
| 115 | const LinkedCell *LCList = NULL; | 
|---|
| 116 | // find biggest molecule | 
|---|
| 117 | int counter  = molecules->ListOfMolecules.size(); | 
|---|
| 118 | bool *Actives = new bool[counter]; | 
|---|
| 119 | counter = 0; | 
|---|
| 120 | for (MoleculeList::iterator BigFinder = molecules->ListOfMolecules.begin(); BigFinder != molecules->ListOfMolecules.end(); BigFinder++) { | 
|---|
| 121 | Actives[counter++] = (*BigFinder)->ActiveFlag; | 
|---|
| 122 | (*BigFinder)->ActiveFlag = (*BigFinder == Boundary) ? false : true; | 
|---|
| 123 | } | 
|---|
| 124 | LCList = new LinkedCell(Boundary, LCWidth); | 
|---|
| 125 | FindNonConvexBorder(Boundary, TesselStruct, LCList, radius, NULL); | 
|---|
| 126 | CorrelationToSurfaceMap *surfacemap = NULL; | 
|---|
| 127 | if (periodic) | 
|---|
| 128 | surfacemap = PeriodicCorrelationToSurface( molecules, elements, TesselStruct, LCList, ranges); | 
|---|
| 129 | else | 
|---|
| 130 | surfacemap = CorrelationToSurface( molecules, elements, TesselStruct, LCList); | 
|---|
| 131 | OutputCorrelationToSurface(&output, surfacemap); | 
|---|
| 132 | // check whether radius was appropriate | 
|---|
| 133 | { | 
|---|
| 134 | double start; double end; | 
|---|
| 135 | GetMinMax( surfacemap, start, end); | 
|---|
| 136 | if (LCWidth < end) | 
|---|
| 137 | DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell width is smaller than the found range of values! Bins can only be correct up to: " << radius << "." << endl); | 
|---|
| 138 | } | 
|---|
| 139 | binmap = BinData( surfacemap, BinWidth, BinStart, BinEnd ); | 
|---|
| 140 | } else | 
|---|
| 141 | return Action::failure; | 
|---|
| 142 | OutputCorrelation ( &binoutput, binmap ); | 
|---|
| 143 | output.close(); | 
|---|
| 144 | binoutput.close(); | 
|---|
| 145 | delete(binmap); | 
|---|
| 146 | delete(correlationmap); | 
|---|
| 147 | delete dialog; | 
|---|
| 148 | return Action::success; | 
|---|
| 149 | } else { | 
|---|
| 150 | delete dialog; | 
|---|
| 151 | return Action::failure; | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | Action::state_ptr AnalysisPairCorrelationAction::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 AnalysisPairCorrelationAction::performRedo(Action::state_ptr _state){ | 
|---|
| 166 | return Action::failure; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | bool AnalysisPairCorrelationAction::canUndo() { | 
|---|
| 170 | return false; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | bool AnalysisPairCorrelationAction::shouldUndo() { | 
|---|
| 174 | return false; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | const string AnalysisPairCorrelationAction::getName() { | 
|---|
| 178 | return NAME; | 
|---|
| 179 | } | 
|---|