source: src/Python/PythonScripting_impl.hpp@ 51cdfd

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 51cdfd was 415ddd, checked in by Frederik Heber <heber@…>, 11 years ago

ActionQueue now contains a run thread.

  • otherwise the progress bar of the QtMainWindow cannot be seen as waitForResults() takes up all of the executing thread.
  • added mutex for operations modifying the queue.
  • added ActionQueue::run() and ::stop(), used by friend stopQueue().
  • insertAction() now makes use of a tempqueue that is added to true queue during run() instead of calling the actions directly.
  • new stopQueue() in cleanUp.hpp is used by module_exit in pyMoleCuilder and in main() before saveAll().
  • cleanup: printTiming() requires now list of action tokens and added new stopAndPurgeQueue() to place waiting for Actions to end into extra function.
  • added ActionQueue::wait() which allows for synchronization in python scripts, is ignored in session python scripts. Otherwise we wait for ActionQueue's queue to empty during execution of load-session which hangs.
  • DOCU: added note to python documentation.
  • added waitQueue() also to purgeStaticInstances().
  • static UIFactory::isFactoryPresent() added that allows checking whether we have a UI or are executed within a python script (i.e. pyMoleCuilder).
  • DOCU: Extended docu on threads and who this affects python scripts.
  • TESTFIX: changed regression tests on storing python sessions.
  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * PythonScripting.hpp
3 *
4 * Created on: Apr 13, 2012
5 * Author: heber
6 */
7
8#ifndef PYTHONSCRIPTING_HPP_
9#define PYTHONSCRIPTING_HPP_
10
11
12// include config.h
13#ifdef HAVE_CONFIG_H
14#include <config.h>
15#endif
16
17
18#include <boost/python.hpp>
19#include <boost/python/module.hpp>
20#include <boost/python/args.hpp>
21
22#include "CodePatterns/toString.hpp"
23
24// all "getter" functions
25#include "modules.hpp"
26
27//!> define all present actions
28#include "GlobalListOfActions.hpp"
29
30//!> python wrapping for all of these actions
31#include "AllActionPython.hpp"
32
33namespace MoleCuilder {
34
35namespace PythonTypes {
36
37inline void IndexError(){
38 PyErr_SetString(PyExc_IndexError, "Index out of range");
39 boost::python::throw_error_already_set();
40}
41
42template<class T>
43struct vec_item{
44 typedef typename T::value_type V;
45 static V& get(T& x, int i){
46 static V nothing;
47 if(i < 0) i += x.size();
48 if(i >= 0 && i < int(x.size())) return x[i];
49 IndexError();
50 return nothing;
51 }
52 static void set(T& x, int i, V const& v){
53 if(i < 0) i += x.size();
54 if(i >= 0 && i < int(x.size())) x[i] = v;
55 else IndexError();
56 }
57 static void del(T& x, int i){
58 if(i < 0) i += x.size();
59 if(i >= 0 && i < int(x.size())) x.erase(x.begin() + i);
60 else IndexError();
61 }
62 static void add(T& x, V const& v){
63 x.push_back(v);
64 }
65};
66
67
68} /* namespace PythonTypes */
69
70} /* namespace MoleCuilder */
71
72
73BOOST_PYTHON_MODULE(pyMoleCuilder)
74{
75 // from this moment on, we need to be sure to deeinitialize in the correct or
76 // this is handled by the cleanup function
77 atexit(MoleCuilder::detail::module_exit);
78
79 // set the docstring of the current module scope
80 boost::python::scope().attr("__doc__") = "pyMolecuilder are the python bindings to all Actions of the program suite MoleCuilder.\n\nMoleCuilder is a program to build molecular (dynamics) worlds, allowing you indefinite manipulation, control and analysis over the atoms and molecules within a simulation domain.";
81
82 boost::python::def(
83 "reinit",
84 MoleCuilder::detail::module_reinit,
85 "Reinitializes the internal state of the python module as if it had been freshly imported,saves all input files beforehand."
86 );
87 boost::python::def(
88 "wait",
89 MoleCuilder::detail::module_wait,
90 "Waits until all currently queued actions have been processed."
91 );
92 boost::python::def< MoleCuilder::detail::doubleVec() >(
93 "getBoundingBox",
94 MoleCuilder::detail::module_getBoundingBox,
95 "returns the cuboid bounding box of the current domain."
96 );
97 boost::python::def< double() >(
98 "getDomainVolume",
99 MoleCuilder::detail::module_getDomainVolume,
100 "returns the volume of the simulation domain."
101 );
102 boost::python::def< double() >(
103 "getSelectedMolarMass",
104 MoleCuilder::detail::module_getSelectedMolarMass,
105 "returns the molar mass of all selected atoms."
106 );
107
108 // STL Vectors:
109 // doubleVec
110 boost::python::class_< std::vector< double > >("PythonType_doubleVec")
111 .def("__len__", &std::vector< double >::size)
112 .def("clear", &std::vector< double >::clear)
113 .def("append", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::add,
114 boost::python::with_custodian_and_ward<1, 2>()) // let container keep value
115 .def("__getitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::get,
116 boost::python::return_value_policy<boost::python::copy_non_const_reference>())
117 .def("__setitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::set,
118 boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
119 .def("__delitem__", &MoleCuilder::PythonTypes::vec_item< std::vector< double > >::del)
120 .def("__iter__", boost::python::iterator< std::vector< double > >())
121 ;
122
123 // access to all Actions
124#define export_print(z,n,list) \
125 BOOST_PP_CAT(export_, BOOST_PP_SEQ_ELEM(n, list))();
126#define BOOST_PP_LOCAL_MACRO(n) export_print(~, n, GLOBALLISTOFPYTHONACTIONS)
127#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFPYTHONACTIONS)))
128#include BOOST_PP_LOCAL_ITERATE()
129#undef instance_print
130}
131
132
133#endif /* PYTHONSCRIPTING_HPP_ */
Note: See TracBrowser for help on using the repository browser.