← Back to team overview

yade-dev team mailing list archive

[Branch ~yade-dev/yade/trunk] Rev 1911: 1. Add python2.5 relative module import workaround (hope it works)

 

------------------------------------------------------------
revno: 1911
committer: Václav Šmilauer <eudoxos@xxxxxxxx>
branch nick: trunk
timestamp: Fri 2009-12-18 21:55:02 +0100
message:
  1. Add python2.5 relative module import workaround (hope it works)
  2. Move some python accessors from yadeWrapper to their respective classes
  3. Half work on periodic cell scaling in the OpenGL renderer
modified:
  core/Cell.hpp
  core/Timing.hpp
  core/main/main.py.in
  pkg/common/Engine/Dispatcher/InteractionGeometryDispatcher.cpp
  pkg/common/Engine/ParallelEngine.cpp
  pkg/common/Engine/ParallelEngine.hpp
  pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp
  pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp
  py/yadeWrapper/yadeWrapper.cpp
  scripts/constitutive-law.py


--
lp:yade
https://code.launchpad.net/~yade-dev/yade/trunk

Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-dev/yade/trunk/+edit-subscription.
=== modified file 'core/Cell.hpp'
--- core/Cell.hpp	2009-12-18 15:04:57 +0000
+++ core/Cell.hpp	2009-12-18 20:55:02 +0000
@@ -33,6 +33,9 @@
 		//! for details
 		Vector3r shear;
 
+		//! reference values of size and shear (for rendering, mainly)
+		Vector3r refSize, refShear, refCenter;
+
 	// caches
 	Vector3r _shearAngle;
 	Vector3r _shearSin;

=== modified file 'core/Timing.hpp'
--- core/Timing.hpp	2009-08-28 13:23:39 +0000
+++ core/Timing.hpp	2009-12-18 20:55:02 +0000
@@ -1,6 +1,7 @@
 // 2009 © Václav Šmilauer <eudoxos@xxxxxxxx>
 #pragma once
 #include<time.h>
+#include<boost/python.hpp>
 
 struct TimingInfo{
 	typedef unsigned long long delta;
@@ -8,12 +9,13 @@
 	delta nsec;
 	TimingInfo():nExec(0),nsec(0){}
 	static delta getNow(bool evenIfDisabled=false)
-	{ 
+	{
+		if(!enabled && !evenIfDisabled) return 0L;
 #ifdef __APPLE__
-		std::cerr << "Warning: Time profiling should not be performed." << std::endl;
+		std::cerr << "ERROR: Time profiling (TimingInfo) not implemented on Apples." << std::endl;
 		return 0L;
 #else
-		if(!enabled && !evenIfDisabled) return 0L; struct timespec ts; 
+		struct timespec ts; 
 		clock_gettime(CLOCK_MONOTONIC,&ts); 
 		return delta(1e9*ts.tv_sec+ts.tv_nsec);		
 #endif
@@ -40,4 +42,10 @@
 			data[i].nExec+=1; data[i].nsec+=now-last; last=now; i++;
 		}
 		void reset(){ data.clear(); labels.clear(); }
+		// python access
+		boost::python::list pyData(){
+			boost::python::list ret;
+			for(size_t i=0; i<data.size(); i++){ ret.append(boost::python::make_tuple(labels[i],data[i].nsec,data[i].nExec));}
+			return ret;
+		}
 };

=== modified file 'core/main/main.py.in'
--- core/main/main.py.in	2009-12-13 22:51:22 +0000
+++ core/main/main.py.in	2009-12-18 20:55:02 +0000
@@ -9,6 +9,12 @@
 # duplicate some items from yade.config here, so that we can increase verbosity when the c++ part is booting
 debug,features,version=bool(${debug}),'${features}'.split(','),'${realVersion}'
 
+## python2.5 relative module imports workaround
+v=sys.version_info
+if v[0]==2 and v[1]<=5:
+	for submodule in ('yade','gts','yade/tests'):
+		sys.path.append(os.path.join(prefix,'lib','yade'+suffix,'py',submodule))
+
 # handle command-line options first
 import optparse
 par=optparse.OptionParser(usage='%prog [options] [ simulation.xml[.bz2] | script.py [script options]]',prog='Yade',version='%s (%s)'%(version,','.join(features)))

=== modified file 'pkg/common/Engine/Dispatcher/InteractionGeometryDispatcher.cpp'
--- pkg/common/Engine/Dispatcher/InteractionGeometryDispatcher.cpp	2009-12-17 07:13:18 +0000
+++ pkg/common/Engine/Dispatcher/InteractionGeometryDispatcher.cpp	2009-12-18 20:55:02 +0000
@@ -74,6 +74,7 @@
 				geomCreated=operator()(b1->shape, b2->shape, *b1->state, *b2->state, Vector3r::ZERO, /*force*/ false, I);
 			} else{
 				Vector3r shift2(I->cellDist[0]*cellSize[0],I->cellDist[1]*cellSize[1],I->cellDist[2]*cellSize[2]); // add periodicity to the position of the 2nd body
+				shift2=scene->cell.shearPt(shift2);
 				geomCreated=operator()(b1->shape, b2->shape, *b1->state, *b2->state, shift2, /*force*/ false, I);
 			}
 			// reset && erase interaction that existed but now has no geometry anymore

=== modified file 'pkg/common/Engine/ParallelEngine.cpp'
--- pkg/common/Engine/ParallelEngine.cpp	2009-12-04 23:07:34 +0000
+++ pkg/common/Engine/ParallelEngine.cpp	2009-12-18 20:55:02 +0000
@@ -1,4 +1,6 @@
 #include"ParallelEngine.hpp"
+#include<boost/python.hpp>
+using namespace boost;
 //#include<omp.h> // needed for omp_get_thread_num() (debugging)
 YADE_PLUGIN((ParallelEngine));
 
@@ -17,3 +19,27 @@
 		}
 	}
 }
+
+void ParallelEngine::slaves_set(const python::list& slaves2){
+	int len=python::len(slaves2);
+	slaves.clear();
+	for(int i=0; i<len; i++){
+		python::extract<std::vector<shared_ptr<Engine> > > serialGroup(slaves2[i]);
+		if (serialGroup.check()){ slaves.push_back(serialGroup()); continue; }
+		python::extract<shared_ptr<Engine> > serialAlone(slaves2[i]);
+		if (serialAlone.check()){ vector<shared_ptr<Engine> > aloneWrap; aloneWrap.push_back(serialAlone()); slaves.push_back(aloneWrap); continue; }
+		PyErr_SetString(PyExc_TypeError,"List elements must be either\n (a) sequences of engines to be executed one after another\n(b) alone engines.");
+		python::throw_error_already_set();
+	}
+}
+
+python::list ParallelEngine::slaves_get(){
+	python::list ret;
+	FOREACH(vector<shared_ptr<Engine > >& grp, slaves){
+		if(grp.size()==1) ret.append(python::object(grp[0]));
+		else ret.append(python::object(grp));
+	}
+	return ret;
+}
+
+

=== modified file 'pkg/common/Engine/ParallelEngine.hpp'
--- pkg/common/Engine/ParallelEngine.hpp	2009-12-09 17:11:51 +0000
+++ pkg/common/Engine/ParallelEngine.hpp	2009-12-18 20:55:02 +0000
@@ -1,5 +1,6 @@
 #pragma once
 #include<yade/core/GlobalEngine.hpp>
+#include<boost/python.hpp>
 class ParallelEngine: public Engine {
 	public:
 		typedef vector<vector<shared_ptr<Engine> > > slaveContainer;
@@ -8,6 +9,9 @@
 		virtual ~ParallelEngine(){};
 		virtual void action(Scene*);
 		virtual bool isActivated(Scene*){return true;}
+		// python access
+		boost::python::list slaves_get();
+		void slaves_set(const boost::python::list& slaves);
 	REGISTER_ATTRIBUTES(Engine,(slaves));
 	REGISTER_CLASS_NAME(ParallelEngine);
 	REGISTER_BASE_CLASS_NAME(Engine);

=== modified file 'pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp'
--- pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp	2009-12-18 17:50:07 +0000
+++ pkg/common/RenderingEngine/OpenGLRenderingEngine.cpp	2009-12-18 20:55:02 +0000
@@ -70,6 +70,8 @@
 void OpenGLRenderingEngine::setBodiesRefSe3(const shared_ptr<Scene>& scene){
 	LOG_DEBUG("(re)initializing reference positions and orientations.");
 	FOREACH(const shared_ptr<Body>& b, *scene->bodies) if(b && b->state) { b->state->refPos=b->state->pos; b->state->refOri=b->state->ori; }
+	scene->cell.refSize=scene->cell.size;
+	scene->cell.refShear=scene->cell.shear;
 }
 
 
@@ -106,17 +108,6 @@
 	return false;
 }
 
-/* mostly copied from PeriodicInsertionSortCollider
- 	FIXME: common implementation somewhere */
-
-Real OpenGLRenderingEngine::wrapCell(const Real x, const Real x1){
-	Real xNorm=(x)/(x1);
-	return (xNorm-floor(xNorm))*(x1);
-}
-Vector3r OpenGLRenderingEngine::wrapCellPt(const Vector3r& pt, Scene* rb){
-	if(!rb->isPeriodic) return pt;
-	return Vector3r(wrapCell(pt[0],rb->cell.size[0]),wrapCell(pt[1],rb->cell.size[1]),wrapCell(pt[2],rb->cell.size[2]));
-}
 
 void OpenGLRenderingEngine::setBodiesDispInfo(){
 	if(scene->bodies->size()!=bodyDisp.size()) bodyDisp.resize(scene->bodies->size());
@@ -125,12 +116,13 @@
 		size_t id=b->getId();
 		const Vector3r& pos=b->state->pos; const Vector3r& refPos=b->state->refPos;
 		const Quaternionr& ori=b->state->ori; const Quaternionr& refOri=b->state->refOri;
-		Vector3r posCell=(!scene->isPeriodic ? pos : scene->cell.wrapShearedPt(pos));
-		bodyDisp[id].isDisplayed=!pointClipped(posCell);	
+		Vector3r cellPos=(!scene->isPeriodic ? pos : scene->cell.wrapShearedPt(pos)); // inside the cell if periodic, same as pos otherwise
+		bodyDisp[id].isDisplayed=!pointClipped(cellPos);	
 		// if no scaling and no periodic, return quickly
 		if(!(scaleDisplacements||scaleRotations||scene->isPeriodic)){ bodyDisp[id].pos=pos; bodyDisp[id].ori=ori; continue; }
 		// apply scaling
-		bodyDisp[id].pos=(scaleDisplacements ? diagMult(displacementScale,pos-refPos)+wrapCellPt(refPos,scene.get()) : posCell );
+		bodyDisp[id].pos=cellPos; // point of reference (inside the cell for periodic)
+		if(scaleDisplacements) bodyDisp[id].pos+=diagMult(displacementScale,pos-refPos); // add scaled translation to the point of reference
 		if(!scaleRotations) bodyDisp[id].ori=ori;
 		else{
 			Quaternionr relRot=refOri.Conjugate()*ori;
@@ -147,6 +139,8 @@
 	glColor3v(Vector3r(1,1,0));
 	glPushMatrix();
 		// order matters
+		Vector3r size=scene->cell.size;
+		if(scaleDisplacements) size=diagMult(displacementScale,size);
 		glTranslatev(scene->cell._shearTrsf*(.5*scene->cell.size)); // shear center (moves when sheared)
 		glMultMatrixd(scene->cell._glShearMatrix);
 		glScalev(scene->cell.size);

=== modified file 'pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp'
--- pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp	2009-12-18 17:50:07 +0000
+++ pkg/common/RenderingEngine/OpenGLRenderingEngine.hpp	2009-12-18 20:55:02 +0000
@@ -38,10 +38,6 @@
 		Real normSaw(Real t, Real period){ Real xi=(t-period*((int)(t/period)))/period; /* normalized value, (0-1〉 */ return (xi<.5?2*xi:2-2*xi); }
 		Real normSquare(Real t, Real period){ Real xi=(t-period*((int)(t/period)))/period; /* normalized value, (0-1〉 */ return (xi<.5?0:1); }
 
-		//! wrap number to interval 0…x1
-		Real wrapCell(const Real x, const Real x1);
-		//! wrap point to inside Scene's cell (identity if !Scene::isPeriodic)
-		Vector3r wrapCellPt(const Vector3r& pt, Scene* rb);
 		void drawPeriodicCell();
 
 		void setBodiesRefSe3(const shared_ptr<Scene>& scene);

=== modified file 'py/yadeWrapper/yadeWrapper.cpp'
--- py/yadeWrapper/yadeWrapper.cpp	2009-12-18 17:50:07 +0000
+++ py/yadeWrapper/yadeWrapper.cpp	2009-12-18 20:55:02 +0000
@@ -572,11 +572,6 @@
 	return "<"+self->getClassName()+" instance at "+lexical_cast<string>(self.get())+">";
 }
 
-python::list TimingDeltas_pyData(const shared_ptr<TimingDeltas> self){
-	python::list ret;
-	for(size_t i=0; i<self->data.size(); i++){ ret.append(python::make_tuple(self->labels[i],self->data[i].nsec,self->data[i].nExec));}
-	return ret;
-}
 
 TimingInfo::delta Engine_timingInfo_nsec_get(const shared_ptr<Engine>& e){return e->timingInfo.nsec;}; void Engine_timingInfo_nsec_set(const shared_ptr<Engine>& e, TimingInfo::delta d){ e->timingInfo.nsec=d;}
 long Engine_timingInfo_nExec_get(const shared_ptr<Engine>& e){return e->timingInfo.nExec;}; void Engine_timingInfo_nExec_set(const shared_ptr<Engine>& e, long d){ e->timingInfo.nExec=d;}
@@ -641,35 +636,13 @@
 }
 		
 // ParallelEngine
-void ParallelEngine_slaves_set(shared_ptr<ParallelEngine> self, const python::list& slaves){
-	int len=python::len(slaves);
-	self->slaves=ParallelEngine::slaveContainer(); // empty the container
-	for(int i=0; i<len; i++){
-		python::extract<std::vector<shared_ptr<Engine> > > serialGroup(slaves[i]);
-		if (serialGroup.check()){ self->slaves.push_back(serialGroup()); continue; }
-		python::extract<shared_ptr<Engine> > serialAlone(slaves[i]);
-		if (serialAlone.check()){ vector<shared_ptr<Engine> > aloneWrap; aloneWrap.push_back(serialAlone()); self->slaves.push_back(aloneWrap); continue; }
-		PyErr_SetString(PyExc_TypeError,"List elements must be either\n (a) sequences of engines to be executed one after another\n(b) alone engines.");
-		python::throw_error_already_set();
-	}
-}
-python::list ParallelEngine_slaves_get(shared_ptr<ParallelEngine> self){
-	python::list ret;
-	FOREACH(vector<shared_ptr<Engine > >& grp, self->slaves){
-		if(grp.size()==1) ret.append(python::object(grp[0]));
-		else ret.append(python::object(grp));
-	}
-	return ret;
-}
-shared_ptr<ParallelEngine> ParallelEngine_ctor_list(const python::list& slaves){ shared_ptr<ParallelEngine> instance(new ParallelEngine); ParallelEngine_slaves_set(instance,slaves); return instance; }
+shared_ptr<ParallelEngine> ParallelEngine_ctor_list(const python::list& slaves){ shared_ptr<ParallelEngine> instance(new ParallelEngine); instance->slaves_set(slaves); return instance; }
 
 shared_ptr<Shape> Body_shape_deprec_get(const shared_ptr<Body>& b){ LOG_WARN("Body().mold and Body().geom attributes are deprecated, use 'shape' instead."); return b->shape; }
 void Body_shape_deprec_set(const shared_ptr<Body>& b, shared_ptr<Shape> ig){ LOG_WARN("Body().mold and Body().geom attributes are deprecated, use 'shape' instead."); b->shape=ig; }
 
-
 long Interaction_getId1(const shared_ptr<Interaction>& i){ return (long)i->getId1(); }
 long Interaction_getId2(const shared_ptr<Interaction>& i){ return (long)i->getId2(); }
-python::tuple Interaction_getCellDist(const shared_ptr<Interaction>& i){ return python::make_tuple(i->cellDist[0],i->cellDist[1],i->cellDist[2]); }
 
 void FileGenerator_generate(const shared_ptr<FileGenerator>& fg, string outFile){ fg->setFileName(outFile); fg->setSerializationLibrary("XMLFormatManager"); bool ret=fg->generateAndSave(); LOG_INFO((ret?"SUCCESS:\n":"FAILURE:\n")<<fg->message); if(ret==false) throw runtime_error("Generator reported error: "+fg->message); };
 void FileGenerator_load(const shared_ptr<FileGenerator>& fg){ string xml(Omega::instance().tmpFilename()+".xml.bz2"); LOG_DEBUG("Using temp file "<<xml); FileGenerator_generate(fg,xml); pyOmega().load(xml); }
@@ -819,7 +792,7 @@
 		.def_readonly("timingDeltas",&Functor::timingDeltas)
 		.add_property("bases",&Functor::getFunctorTypes);
 	python::class_<Dispatcher, shared_ptr<Dispatcher>, python::bases<Engine>, noncopyable>("Dispatcher",python::no_init);
-	python::class_<TimingDeltas, shared_ptr<TimingDeltas>, noncopyable >("TimingDeltas").add_property("data",&TimingDeltas_pyData).def("reset",&TimingDeltas::reset);
+	python::class_<TimingDeltas, shared_ptr<TimingDeltas>, noncopyable >("TimingDeltas").add_property("data",&TimingDeltas::pyData).def("reset",&TimingDeltas::reset);
 
 	python::class_<InteractionDispatchers,shared_ptr<InteractionDispatchers>, python::bases<Engine>, noncopyable >("InteractionDispatchers")
 		.def("__init__",python::make_constructor(InteractionDispatchers_ctor_lists))
@@ -828,7 +801,7 @@
 		.def_readonly("lawDispatcher",&InteractionDispatchers::lawDispatcher);
 	python::class_<ParallelEngine,shared_ptr<ParallelEngine>, python::bases<Engine>, noncopyable>("ParallelEngine")
 		.def("__init__",python::make_constructor(ParallelEngine_ctor_list))
-		.add_property("slaves",&ParallelEngine_slaves_get,&ParallelEngine_slaves_set);
+		.add_property("slaves",&ParallelEngine::slaves_get,&ParallelEngine::slaves_set);
 
 	#define EXPOSE_DISPATCHER(DispatcherT) python::class_<DispatcherT, shared_ptr<DispatcherT>, python::bases<Dispatcher>, noncopyable >(#DispatcherT).def("__init__",python::make_constructor(Dispatcher_ctor_list<DispatcherT>)).add_property("functors",&Dispatcher_functors_get<DispatcherT>).def("dispMatrix",&DispatcherT::dump,python::arg("names")=true,"Return dictionary with contents of the dispatch matrix.").def("dispFunctor",&DispatcherT::getFunctor,"Return functor that would be dispatched for given argument(s); None if no dispatch; ambiguous dispatch throws.");
 		EXPOSE_DISPATCHER(BoundDispatcher)
@@ -905,7 +878,7 @@
 		.add_property("id1",&Interaction_getId1)
 		.add_property("id2",&Interaction_getId2)
 		.add_property("isReal",&Interaction::isReal)
-		.add_property("cellDist",&Interaction_getCellDist);
+		.add_property("cellDist",&Interaction::cellDist);
 	EXPOSE_CXX_CLASS_IX(InteractionPhysics);
 	EXPOSE_CXX_CLASS_IX(InteractionGeometry);
 	EXPOSE_CXX_CLASS(FileGenerator)

=== modified file 'scripts/constitutive-law.py'
--- scripts/constitutive-law.py	2009-12-18 09:48:16 +0000
+++ scripts/constitutive-law.py	2009-12-18 20:55:02 +0000
@@ -16,7 +16,7 @@
 ]
 
 for n in range(30):
-	O.bodies.append(utils.sphere([0,n,0],.50001,dynamic=(n>0),color=[1-(n/20.),n/20.,0],young=30e9,poisson=.3,density=2400))
+	O.bodies.append(utils.sphere([0,n,0],.50001,dynamic=(n>0),color=[1-(n/20.),n/20.,0]))
 O.bodies[len(O.bodies)-1]['isDynamic']=False
 #	# looks for metaengine found in Omega() and uses those
 #	if n>0: utils.createInteraction(n-1,n)