source: src/World.cpp@ 8540f0

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 8540f0 was e4b5de, checked in by Frederik Heber <heber@…>, 15 years ago

Case 'f' is now handled by CommandLineUI.

  • FragmentationFragmentationAction::handle() has not been implemented, copied from ParseCommandLineOptions()
  • FIX: FragmentationFragmentationAction had wrong NAME.
  • World has new ExitFlag variable and getter and setter functions.
  • TESTFIX: Fragmentation/3 does not copy all BondFragment* files from pre but runs Fragmentation twice and tests the ExitFlag. This makes the test more independent and closer to what it's suppose to test (i.e. iterative fragmentation with increasing order).
  • TESTFIX: removed all Fragmentation/3/pre/BondFragment* files
  • FIX: CommandLineWindow::populateFragmentationActions() was missing two of its three actions still.
  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "World.hpp"
9
10#include "atom.hpp"
11#include "config.hpp"
12#include "molecule.hpp"
13#include "periodentafel.hpp"
14#include "Descriptors/AtomDescriptor.hpp"
15#include "Descriptors/AtomDescriptor_impl.hpp"
16#include "Descriptors/MoleculeDescriptor.hpp"
17#include "Descriptors/MoleculeDescriptor_impl.hpp"
18#include "Descriptors/SelectiveIterator_impl.hpp"
19#include "Actions/ManipulateAtomsProcess.hpp"
20
21#include "Patterns/Singleton_impl.hpp"
22
23using namespace std;
24
25/******************************* getter and setter ************************/
26periodentafel *&World::getPeriode(){
27 return periode;
28}
29
30config *&World::getConfig(){
31 return configuration;
32}
33
34// Atoms
35
36atom* World::getAtom(AtomDescriptor descriptor){
37 return descriptor.find();
38}
39
40vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
41 return descriptor.findAll();
42}
43
44vector<atom*> World::getAllAtoms(){
45 return getAllAtoms(AllAtoms());
46}
47
48int World::numAtoms(){
49 return atoms.size();
50}
51
52// Molecules
53
54molecule *World::getMolecule(MoleculeDescriptor descriptor){
55 return descriptor.find();
56}
57
58std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
59 return descriptor.findAll();
60}
61
62std::vector<molecule*> World::getAllMolecules(){
63 return getAllMolecules(AllMolecules());
64}
65
66int World::numMolecules(){
67 return molecules_deprecated->ListOfMolecules.size();
68}
69
70// system
71
72double * World::getDomain() {
73 return cell_size;
74}
75
76void World::setDomain(double * matrix)
77{
78
79}
80
81char * World::getDefaultName() {
82 return defaultName;
83}
84
85void World::setDefaultName(char * name)
86{
87 delete[](defaultName);
88 const int length = strlen(name);
89 if (length < MAXSTRINGSIZE) {
90 defaultName = new char[length+2];
91 strncpy(defaultName, name, length);
92 } else {
93 defaultName = new char[MAXSTRINGSIZE];
94 strncpy(defaultName, "none", MAXSTRINGSIZE-1);
95 }
96};
97
98int World::getExitFlag() {
99 return ExitFlag;
100}
101
102void World::setExitFlag(int flag) {
103 if (ExitFlag < flag)
104 ExitFlag = flag;
105}
106
107/******************** Methods to change World state *********************/
108
109molecule* World::createMolecule(){
110 OBSERVE;
111 molecule *mol = NULL;
112 mol = NewMolecule();
113 assert(!molecules.count(currMoleculeId));
114 mol->setId(currMoleculeId++);
115 // store the molecule by ID
116 molecules[mol->getId()] = mol;
117 mol->signOn(this);
118 return mol;
119}
120
121void World::destroyMolecule(molecule* mol){
122 OBSERVE;
123 destroyMolecule(mol->getId());
124}
125
126void World::destroyMolecule(moleculeId_t id){
127 OBSERVE;
128 molecule *mol = molecules[id];
129 assert(mol);
130 DeleteMolecule(mol);
131 molecules.erase(id);
132}
133
134double *World::cell_size = NULL;
135char *World::defaultName = NULL;
136
137atom *World::createAtom(){
138 OBSERVE;
139 atomId_t id = getNextAtomId();
140 atom *res = NewAtom(id);
141 res->setWorld(this);
142 // store the atom by ID
143 atoms[res->getId()] = res;
144 return res;
145}
146
147
148int World::registerAtom(atom *atom){
149 OBSERVE;
150 atomId_t id = getNextAtomId();
151 atom->setId(id);
152 atom->setWorld(this);
153 atoms[atom->getId()] = atom;
154 return atom->getId();
155}
156
157void World::destroyAtom(atom* atom){
158 OBSERVE;
159 int id = atom->getId();
160 destroyAtom(id);
161}
162
163void World::destroyAtom(atomId_t id) {
164 OBSERVE;
165 atom *atom = atoms[id];
166 assert(atom);
167 DeleteAtom(atom);
168 atoms.erase(id);
169 releaseAtomId(id);
170}
171
172bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
173 OBSERVE;
174 // in case this call did not originate from inside the atom, we redirect it,
175 // to also let it know that it has changed
176 if(!target){
177 target = atoms[oldId];
178 assert(target && "Atom with that ID not found");
179 return target->changeId(newId);
180 }
181 else{
182 if(reserveAtomId(newId)){
183 atoms.erase(oldId);
184 atoms.insert(pair<atomId_t,atom*>(newId,target));
185 return true;
186 }
187 else{
188 return false;
189 }
190 }
191}
192
193ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
194 return new ManipulateAtomsProcess(op, descr,name,true);
195}
196
197ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
198 return manipulateAtoms(op,name,AllAtoms());
199}
200
201/********************* Internal Change methods for double Callback and Observer mechanism ********/
202
203void World::doManipulate(ManipulateAtomsProcess *proc){
204 proc->signOn(this);
205 {
206 OBSERVE;
207 proc->doManipulate(this);
208 }
209 proc->signOff(this);
210}
211/******************************* IDManagement *****************************/
212
213// Atoms
214
215atomId_t World::getNextAtomId(){
216 // see if we can reuse some Id
217 if(atomIdPool.empty()){
218 return currAtomId++;
219 }
220 else{
221 // we give out the first ID from the pool
222 atomId_t id = *(atomIdPool.begin());
223 atomIdPool.erase(id);
224 return id;
225 }
226}
227
228void World::releaseAtomId(atomId_t id){
229 atomIdPool.insert(id);
230 // defragmentation of the pool
231 set<atomId_t>::reverse_iterator iter;
232 // go through all Ids in the pool that lie immediately below the border
233 while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
234 atomIdPool.erase(--currAtomId);
235 }
236}
237
238bool World::reserveAtomId(atomId_t id){
239 if(id>=currAtomId ){
240 // add all ids between the new one and current border as available
241 for(atomId_t pos=currAtomId; pos<id; ++pos){
242 atomIdPool.insert(pos);
243 }
244 currAtomId=id+1;
245 return true;
246 }
247 else if(atomIdPool.count(id)){
248 atomIdPool.erase(id);
249 return true;
250 }
251 else{
252 // this ID could not be reserved
253 return false;
254 }
255}
256
257// Molecules
258
259/******************************* Iterators ********************************/
260
261// Build the AtomIterator from template
262CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
263
264
265World::AtomIterator World::getAtomIter(AtomDescriptor descr){
266 return AtomIterator(descr,atoms);
267}
268
269World::AtomIterator World::atomEnd(){
270 return AtomIterator(AllAtoms(),atoms,atoms.end());
271}
272
273// build the MoleculeIterator from template
274CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
275
276World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
277 return MoleculeIterator(descr,molecules);
278}
279
280World::MoleculeIterator World::moleculeEnd(){
281 return MoleculeIterator(AllMolecules(),molecules,molecules.end());
282}
283
284/******************************* Singleton Stuff **************************/
285
286World::World() :
287 periode(new periodentafel),
288 configuration(new config),
289 ExitFlag(0),
290 atoms(),
291 currAtomId(0),
292 molecules(),
293 currMoleculeId(0),
294 molecules_deprecated(new MoleculeListClass(this))
295{
296 cell_size = new double[6];
297 cell_size[0] = 20.;
298 cell_size[1] = 0.;
299 cell_size[2] = 20.;
300 cell_size[3] = 0.;
301 cell_size[4] = 0.;
302 cell_size[5] = 20.;
303 defaultName = new char[MAXSTRINGSIZE];
304 strcpy(defaultName, "none");
305 molecules_deprecated->signOn(this);
306}
307
308World::~World()
309{
310 molecules_deprecated->signOff(this);
311 delete[] cell_size;
312 delete[] defaultName;
313 delete molecules_deprecated;
314 delete periode;
315 delete configuration;
316 MoleculeSet::iterator molIter;
317 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
318 DeleteMolecule((*molIter).second);
319 }
320 molecules.clear();
321 AtomSet::iterator atIter;
322 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
323 DeleteAtom((*atIter).second);
324 }
325 atoms.clear();
326}
327
328// Explicit instantiation of the singleton mechanism at this point
329
330CONSTRUCT_SINGLETON(World)
331
332/******************************* deprecated Legacy Stuff ***********************/
333
334MoleculeListClass *&World::getMolecules() {
335 return molecules_deprecated;
336}
Note: See TracBrowser for help on using the repository browser.