source: src/Actions/UndoRedoHelpers.cpp@ a7aebd

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
Last change on this file since a7aebd was a7aebd, checked in by Frederik Heber <heber@…>, 10 years ago

World removes empty molecules.

  • on destroyAtom() of molecule's last atom, destroyMolecule() is called.
  • FIX: destroyMolecule(molecule *) did use OBSERVE unncessarily. This caused seg'fault.
  • extracted removeAtomsinMolecule from molecule class. This is necessary since World will automatically remove empty molecules.
  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013-2014 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * UndoRedoHelpers.cpp
26 *
27 * Created on: Apr 5, 2012
28 * Author: heber
29 */
30
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "CodePatterns/MemDebug.hpp"
38
39#include "UndoRedoHelpers.hpp"
40
41#include <boost/bind.hpp>
42#include <boost/foreach.hpp>
43#include <boost/function.hpp>
44
45#include "Atom/atom.hpp"
46#include "molecule.hpp"
47#include "Descriptors/AtomIdDescriptor.hpp"
48#include "Descriptors/MoleculeIdDescriptor.hpp"
49#include "CodePatterns/Assert.hpp"
50#include "CodePatterns/Log.hpp"
51#include "World.hpp"
52#include "WorldTime.hpp"
53
54bool MoleCuilder::AddAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
55{
56 size_t i=0;
57 for (; i<atoms.size(); ++i) {
58 // re-create the atom
59 LOG(3, "DEBUG: Re-adding atom " << atoms[i].getId() << ".");
60 atom *Walker = World::getInstance().createAtom();
61 if (!atoms[i].setAtom(*Walker)) {
62 ELOG(1, "Failed to set id.");
63 World::getInstance().destroyAtom(Walker);
64 break;
65 }
66 }
67 if (i<atoms.size()) {
68 // remove all previous ones, too
69 for (size_t j=0;j<i;++j)
70 World::getInstance().destroyAtom(atoms[j].getId());
71 // and announce the failure
72 return false;
73 }
74 return true;
75}
76
77void MoleCuilder::RemoveAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
78{
79 BOOST_FOREACH(const AtomicInfo &_atom, atoms) {
80 World::getInstance().destroyAtom(_atom.getId());
81 }
82}
83
84void MoleCuilder::StoreBondInformationFromAtoms(
85 const std::vector<const atom*> &atoms,
86 std::vector< BondInfo > &bonds)
87{
88 ASSERT( bonds.empty(),
89 "StoreBondInformationFromAtoms() - give bonds vector is not empty.");
90 bonds.reserve(atoms.size()*4);
91 for (std::vector<const atom*>::const_iterator atomiter = atoms.begin();
92 atomiter != atoms.end(); ++atomiter) {
93 const BondList & _atom_bonds = (*atomiter)->getListOfBonds();
94 for(BondList::const_iterator iter = _atom_bonds.begin(); iter != _atom_bonds.end(); ++iter)
95 bonds.push_back( BondInfo(*iter) );
96 }
97}
98
99bool MoleCuilder::AddBondsFromBondInfo(const std::vector< BondInfo > &bonds)
100{
101 bool status = true;
102 for(std::vector< BondInfo >::const_iterator iter = bonds.begin();
103 iter != bonds.end(); ++iter)
104 if (!(*iter).RecreateBond())
105 status = false;
106 return status;
107}
108
109void MoleCuilder::SetAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
110{
111 BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
112 const atomId_t id = _atominfo.getId();
113 atom * const _atom = World::getInstance().getAtom(AtomById(id));
114 ASSERT( _atom != NULL,
115 "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
116 +toString(id)+" in the world.");
117 _atominfo.setAtom( *_atom );
118 }
119}
120
121void MoleCuilder::SelectAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
122{
123 BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
124 const atomId_t id = _atominfo.getId();
125 World::getInstance().selectAtom(id);
126 }
127}
128
129void MoleCuilder::ResetAtomPosition(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &MovedToVector)
130{
131 boost::function<void(atom *, const Vector&)> setter =
132 boost::bind(&atom::setPosition, _1, _2);
133 ResetByFunction(movedatoms, MovedToVector, setter);
134}
135
136void MoleCuilder::ResetAtomVelocity(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &VelocityVector)
137{
138 boost::function<void(atom *, const Vector&)> setter =
139 boost::bind(&atom::setAtomicVelocity, _1, _2);
140 ResetByFunction(movedatoms, VelocityVector, setter);
141}
142
143void MoleCuilder::ResetAtomForce(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &ForceVector)
144{
145 boost::function<void(atom *, const Vector&)> setter =
146 boost::bind(&atom::setAtomicForce, _1, _2);
147 ResetByFunction(movedatoms, ForceVector, setter);
148}
149
150void MoleCuilder::ResetByFunction(
151 const std::vector<AtomicInfo> &movedatoms,
152 const std::vector<Vector> &MovedToVector,
153 boost::function<void(atom *, const Vector&)> &setter)
154{
155 std::vector<Vector>::const_iterator positer = MovedToVector.begin();
156 ASSERT(movedatoms.size() == MovedToVector.size(),
157 "MoleCuilder::ResetAtomPosition() - the number of atoms "
158 +toString(movedatoms.size())+" and the number of positions "
159 +toString(MovedToVector.size())+" is not the same.");
160 BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
161 const atomId_t id = _atominfo.getId();
162 atom * const _atom = World::getInstance().getAtom(AtomById(id));
163 ASSERT( _atom != NULL,
164 "FillSphericalSurfaceAction::performRedo() - cannot find atom with id "
165 +toString(id)+" in the world.");
166 setter(_atom, *positer );
167 ++positer;
168 }
169}
170
171void MoleCuilder::RemoveMoleculesWithAtomsByIds(const std::vector<moleculeId_t> &ids)
172{
173 for (std::vector<moleculeId_t>::const_iterator iter = ids.begin();
174 iter != ids.end(); ++iter) {
175 molecule * mol = World::getInstance().getMolecule(MoleculeById(*iter));
176 if (mol != NULL) {
177 removeAtomsinMolecule(mol);
178 // molecules are automatically removed when empty
179 }
180 }
181}
182
183void MoleCuilder::removeLastStep(const std::vector<atomId_t> &_atoms, const unsigned int _step)
184{
185 for (size_t i=0; i<_atoms.size(); ++i) {
186 atom * const _atom = World::getInstance().getAtom(AtomById(_atoms[i]));
187 _atom->removeStep(_step);
188 }
189}
190
191void MoleCuilder::addNewStep(const std::vector<AtomicInfo> &_movedatoms, const unsigned int _step)
192{
193 for(size_t i=0; i< _movedatoms.size(); ++i) {
194 atom * const _atom = World::getInstance().getAtom(AtomById(_movedatoms[i].getId()));
195 _atom->UpdateStep(_step);
196 }
197}
198
199void MoleCuilder::addNewStep(const std::vector<atomId_t> &_ids, const unsigned int _step)
200{
201 for(size_t i=0; i< _ids.size(); ++i) {
202 atom * const _atom = World::getInstance().getAtom(AtomById(_ids[i]));
203 _atom->UpdateStep(_step);
204 }
205}
206
207std::vector<atomId_t> MoleCuilder::getIdsFromAtomicInfo(const std::vector<AtomicInfo> &movedatoms)
208{
209 std::vector<atomId_t> ids(movedatoms.size(), (size_t)-1);
210 std::transform(
211 movedatoms.begin(), movedatoms.end(),
212 ids.begin(),
213 boost::bind(&AtomicInfo::getId, _1));
214 return ids;
215}
Note: See TracBrowser for help on using the repository browser.