source: src/World.cpp@ 673c7f

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 673c7f was 97ebf8, checked in by Frederik Heber <heber@…>, 15 years ago

Added all commands defined in ParseCommandLineOptions() as Actions.

  • Actions are not yet used, except verbose, version and help.
  • Files are present and included in Makefile.am
  • not unit tests written so far
  • no action has been tested so far (except for MapOfActions)
  • structure introduced to to transition from ParseCommandLineOptions to actions.
  • program name and config file are fixed arguments.

Signed-off-by: Frederik Heber <heber@…>

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