Ignore:
Timestamp:
Aug 12, 2010, 4:34:46 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
ae959a
Parents:
a7a087
git-author:
Frederik Heber <heber@…> (08/12/10 16:32:32)
git-committer:
Frederik Heber <heber@…> (08/12/10 16:34:46)
Message:

BUGFIX: SubgraphDissectionAction() does not copy system anymore, has Undo/Redo.

  • This fixes ticket #88.
  • Added Undo/Redo, needed World::changeMoleculeId() for that
  • BUGFIX: performCall() let DepthFirstSearchAnalysis() copy atoms and copied the molecules once more.
  • BUGFIX: performCall() did not reset fathers of copied atoms to point to themselves (BUG with redo arose).
  • TEST: Added Undo/Redo part to Graph/2 (subgraph dissection).
  • TESTFIX: Analysis/4 uses --unselect-molecule-by-formula and id of this molecule is 1 (as system is not copied anymore).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/FragmentationAction/SubgraphDissectionAction.cpp

    ra7a087 rfaa1c9  
    1515#include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
    1616#include "Actions/ActionRegistry.hpp"
     17#include "Descriptors/AtomIdDescriptor.hpp"
    1718#include "Descriptors/MoleculeDescriptor.hpp"
    1819
     
    2223#include "config.hpp"
    2324#include "Helpers/Log.hpp"
     25#include "Helpers/Verbose.hpp"
    2426#include "molecule.hpp"
    2527#include "stackclass.hpp"
    26 #include "verbose.hpp"
    2728#include "World.hpp"
    2829
     
    3536#include "UIElements/Dialog.hpp"
    3637#include "Actions/ValueStorage.hpp"
     38
     39// memento to remember the state when undoing
     40
     41typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
     42
     43class FragmentationSubgraphDissectionState : public ActionState {
     44public:
     45  FragmentationSubgraphDissectionState(const MolAtomList &_moleculelist) :
     46   moleculelist(_moleculelist)
     47  {}
     48  MolAtomList moleculelist;
     49};
    3750
    3851const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
     
    6376  DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
    6477
     78  // first create undo state
     79  MolAtomList moleculelist;
     80  vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
     81  for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
     82    std::vector<atomId_t> atomlist;
     83    atomlist.resize((*moliter)->size());
     84    for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
     85      atomlist.push_back((*atomiter)->getId());
     86    }
     87    moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
     88  }
     89  FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist);
     90
    6591  MoleculeListClass *molecules = World::getInstance().getMolecules();
    6692  config * const configuration = World::getInstance().getConfig();
    6793
    6894  // 0a. remove all present molecules
    69   vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
    7095  for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
    7196    molecules->erase(*MolRunner);
     
    75100  // 0b. remove all bonds and construct a molecule with all atoms
    76101  molecule *mol = World::getInstance().createMolecule();
    77   vector <atom *> allatoms = World::getInstance().getAllAtoms();
    78   for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
    79     for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
    80       delete(*BondRunner);
    81     mol->AddAtom(*AtomRunner);
     102  {
     103    vector <atom *> allatoms = World::getInstance().getAllAtoms();
     104    for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
     105      for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
     106        delete(*BondRunner);
     107      mol->AddAtom(*AtomRunner);
     108    }
    82109  }
    83110
     
    100127  delete(BackEdgeStack);
    101128  if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
    102     World::getInstance().destroyMolecule(mol);
     129    //World::getInstance().destroyMolecule(mol);
    103130    DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
    104131    return Action::failure;
    105132  }
    106 <<<<<<< HEAD
    107 
    108   // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
    109   // the original one as parsed in)
    110   // TODO: Optimize this, when molecules just contain pointer list of global atoms!
    111 
    112   // 4a. create array of molecules to fill
    113   const int MolCount = Subgraphs->next->Count();
    114   char number[MAXSTRINGSIZE];
    115   molecule **moleculelist = new molecule *[MolCount];
    116   MoleculeLeafClass *MolecularWalker = Subgraphs;
    117   for (int i=0;i<MolCount;i++) {
    118     MolecularWalker = MolecularWalker->next;
    119     moleculelist[i] = World::getInstance().createMolecule();
    120     moleculelist[i]->ActiveFlag = true;
    121     strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
    122     if (MolCount > 1) {
    123       sprintf(number, "-%d", i+1);
    124       strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
    125     }
    126     DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
    127     for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
    128       DoLog(1) && (Log() << Verbose(1) << **iter << endl);
    129     }
    130     molecules->insert(moleculelist[i]);
    131   }
    132 
    133   // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
    134   int FragmentCounter = 0;
    135   map<int, atom *> AtomToFragmentMap;
    136   MolecularWalker = Subgraphs;
    137   while (MolecularWalker->next != NULL) {
    138     MolecularWalker = MolecularWalker->next;
    139     for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
    140       atom * Walker = *iter;
    141       DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
    142       MolecularWalker->Leaf->erase(iter);
    143       moleculelist[FragmentCounter]->AddAtom(Walker);    // counting starts at 1
    144     }
    145     FragmentCounter++;
    146   }
    147   // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
    148   // 4d. destroy the original molecule
    149 =======
    150   int FragmentCounter = Subgraphs->Count();
     133  int FragmentCounter = Subgraphs->next->Count();
    151134
    152135  // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
    153   // 3. destroy the original molecule
    154 >>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
    155   for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
    156     World::getInstance().destroyAtom(*AtomRunner);
    157   World::getInstance().destroyMolecule(mol);
    158 
    159 <<<<<<< HEAD
    160   // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintain their ListOfBonds, but we have to remove them from first..last list
    161   // TODO: check whether this is really not needed anymore
    162   // 4e. free Leafs
    163   MolecularWalker = Subgraphs;
    164 =======
     136  {
     137    // 3a. destroy the original molecule
     138    for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
     139      World::getInstance().destroyAtom(*AtomRunner);
     140    World::getInstance().destroyMolecule(mol);
     141    // 3b. correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
     142    vector <atom *> allatoms = World::getInstance().getAllAtoms();
     143    for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
     144      (*AtomRunner)->father = *AtomRunner;
     145    }
     146  }
     147
    165148  // 4. free Leafs
    166149  MoleculeLeafClass *MolecularWalker = Subgraphs;
    167 >>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
    168150  while (MolecularWalker->next != NULL) {
     151    MolecularWalker->Leaf = NULL;
    169152    MolecularWalker = MolecularWalker->next;
    170153    delete(MolecularWalker->previous);
    171154  }
     155  MolecularWalker->Leaf = NULL;
    172156  delete(MolecularWalker);
    173 <<<<<<< HEAD
    174   delete[](moleculelist);
    175 =======
    176 >>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
    177157  DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
    178158
    179   return Action::success;
     159  return Action::state_ptr(UndoState);
    180160}
    181161
    182162Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
    183 //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
    184 
    185   return Action::failure;
    186 //  string newName = state->mol->getName();
    187 //  state->mol->setName(state->lastName);
    188 //
    189 //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
     163  FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
     164
     165  {
     166    // remove all present molecules
     167    MoleculeListClass *molecules = World::getInstance().getMolecules();
     168    vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
     169    for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
     170      molecules->erase(*MolRunner);
     171      World::getInstance().destroyMolecule(*MolRunner);
     172    }
     173  }
     174
     175  {
     176    // construct the old state
     177    molecule *mol = NULL;
     178    for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
     179      mol = World::getInstance().createMolecule();
     180      if (mol->getId() != (*iter).first)
     181        World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
     182      for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter)
     183        mol->AddAtom(World::getInstance().getAtom(AtomById(*atomiter)));
     184    }
     185  }
     186
     187  return Action::state_ptr(_state);
    190188}
    191189
    192190Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
    193   return Action::failure;
     191  return performCall();
    194192}
    195193
    196194bool FragmentationSubgraphDissectionAction::canUndo() {
    197   return false;
     195  return true;
    198196}
    199197
    200198bool FragmentationSubgraphDissectionAction::shouldUndo() {
    201   return false;
     199  return true;
    202200}
    203201
Note: See TracChangeset for help on using the changeset viewer.