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