yade-dev team mailing list archive
-
yade-dev team
-
Mailing list archive
-
Message #12431
[Branch ~yade-pkg/yade/git-trunk] Rev 3739: Tiny code refactoring
------------------------------------------------------------
revno: 3739
committer: Anton Gladky <gladky.anton@xxxxxxxxx>
timestamp: Wed 2015-11-18 08:06:16 +0100
message:
Tiny code refactoring
modified:
core/Body.hpp
core/BodyContainer.cpp
core/Dispatcher.hpp
core/Functor.hpp
core/Interaction.hpp
core/InteractionContainer.cpp
core/InteractionContainer.hpp
core/Omega.cpp
core/Omega.hpp
core/SimulationFlow.cpp
core/SimulationFlow.hpp
core/State.cpp
core/State.hpp
core/ThreadRunner.cpp
core/TimeStepper.hpp
--
lp:yade
https://code.launchpad.net/~yade-pkg/yade/git-trunk
Your team Yade developers is subscribed to branch lp:yade.
To unsubscribe from this branch go to https://code.launchpad.net/~yade-pkg/yade/git-trunk/+edit-subscription
=== modified file 'core/Body.hpp'
--- core/Body.hpp 2014-11-17 09:21:14 +0000
+++ core/Body.hpp 2015-11-18 07:06:16 +0000
@@ -18,9 +18,6 @@
#include<lib/serialization/Serializable.hpp>
#include<lib/multimethods/Indexable.hpp>
-
-
-
class Scene;
class Interaction;
@@ -39,7 +36,6 @@
//! get Body pointer given its id.
static const shared_ptr<Body>& byId(Body::id_t _id,Scene* rb=NULL);
static const shared_ptr<Body>& byId(Body::id_t _id,shared_ptr<Scene> rb);
-
//! Whether this Body is a Clump.
//! @note The following is always true: \code (Body::isClump() XOR Body::isClumpMember() XOR Body::isStandalone()) \endcode
=== modified file 'core/BodyContainer.cpp'
--- core/BodyContainer.cpp 2014-10-09 20:31:35 +0000
+++ core/BodyContainer.cpp 2015-11-18 07:06:16 +0000
@@ -8,7 +8,6 @@
#include<omp.h>
#endif
-
CREATE_LOGGER(BodyContainer);
void BodyContainer::clear(){
=== modified file 'core/Dispatcher.hpp'
--- core/Dispatcher.hpp 2014-10-15 06:44:01 +0000
+++ core/Dispatcher.hpp 2015-11-18 07:06:16 +0000
@@ -100,8 +100,6 @@
}
}
-
-
//! Return functors of this dispatcher, as list of functors of appropriate type
template<typename DispatcherT>
std::vector<shared_ptr<typename DispatcherT::functorType> > Dispatcher_functors_get(shared_ptr<DispatcherT> self){
@@ -125,13 +123,7 @@
return instance;
}
-
-
-template
-<
- class FunctorType,
- bool autoSymmetry=true
->
+template < class FunctorType, bool autoSymmetry=true>
class Dispatcher1D : public Dispatcher,
public DynLibDispatcher
< TYPELIST_1(typename FunctorType::DispatchType1) // base classes for dispatch
@@ -141,7 +133,6 @@
, autoSymmetry
>
{
-
public :
typedef typename FunctorType::DispatchType1 baseClass;
typedef baseClass argType1;
@@ -172,18 +163,13 @@
else return "";
}
-
public:
REGISTER_ATTRIBUTES(Dispatcher,);
REGISTER_CLASS_AND_BASE(Dispatcher1D,Dispatcher DynLibDispatcher);
};
-template
-<
- class FunctorType,
- bool autoSymmetry=true
->
+template < class FunctorType, bool autoSymmetry=true>
class Dispatcher2D : public Dispatcher,
public DynLibDispatcher
< TYPELIST_2(typename FunctorType::DispatchType1,typename FunctorType::DispatchType2) // base classes for dispatch
@@ -226,4 +212,3 @@
REGISTER_ATTRIBUTES(Dispatcher,);
REGISTER_CLASS_AND_BASE(Dispatcher2D,Dispatcher DynLibDispatcher);
};
-
=== modified file 'core/Functor.hpp'
--- core/Functor.hpp 2014-10-15 06:44:01 +0000
+++ core/Functor.hpp 2015-11-18 07:06:16 +0000
@@ -37,8 +37,6 @@
};
REGISTER_SERIALIZABLE(Functor);
-
-
template
<
class _DispatchType1,
@@ -59,7 +57,6 @@
/* do not REGISTER_ATTRIBUTES here, since we are template; derived classes should call REGISTER_ATTRIBUTES(Functor,(their)(own)(attributes)), bypassing Functor1D */
};
-
template
<
class _DispatchType1,
=== modified file 'core/Interaction.hpp'
--- core/Interaction.hpp 2014-10-15 06:44:01 +0000
+++ core/Interaction.hpp 2015-11-18 07:06:16 +0000
@@ -36,10 +36,9 @@
// Whether geometry dispatcher exists at all; this is different from !geom, since that can mean we haven't populated the cache yet.
// Therefore, geomExists must be initialized to true first (done in Interaction::reset() called from ctor).
bool geomExists;
- // shared_ptr's are initialized to NULLs automagically
- shared_ptr<IGeomFunctor> geom;
- shared_ptr<IPhysFunctor> phys;
- shared_ptr<LawFunctor> constLaw;
+ shared_ptr<IGeomFunctor> geom = nullptr;
+ shared_ptr<IPhysFunctor> phys = nullptr;
+ shared_ptr<LawFunctor> constLaw = nullptr;
} functorCache;
//! Reset interaction to the intial state (keep only body ids)
=== modified file 'core/InteractionContainer.cpp'
--- core/InteractionContainer.cpp 2015-04-30 16:53:30 +0000
+++ core/InteractionContainer.cpp 2015-11-18 07:06:16 +0000
@@ -38,28 +38,25 @@
return true;
}
-
void InteractionContainer::clear(){
assert(bodies);
boost::mutex::scoped_lock lock(drawloopmutex);
FOREACH(const shared_ptr<Body>& b, *bodies) {
- if (b) b->intrs.clear(); // delete interactions from bodies
+ if (b) b->intrs.clear();
}
- linIntrs.clear(); // clear the linear container
+ linIntrs.clear();
currSize=0;
dirty=true;
}
-
bool InteractionContainer::erase(Body::id_t id1,Body::id_t id2, int linPos){
assert(bodies);
boost::mutex::scoped_lock lock(drawloopmutex);
if (id1>id2) swap(id1,id2);
- if(id2>=(Body::id_t)bodies->size()) return false; // no such interaction
+ if(id2>=(Body::id_t)bodies->size()) return false;
const shared_ptr<Body>& b1((*bodies)[id1]);
const shared_ptr<Body>& b2((*bodies)[id2]);
-// LOG_DEBUG("InteractionContainer erase intrs id1=" << id1 << " id2=" << id2);
int linIx=-1;
if(!b1) linIx=linPos;
else {
@@ -67,7 +64,6 @@
if(I==b1->intrs.end()) linIx=linPos;
else {
linIx=I->second->linIx;
-// LOG_DEBUG("InteractionContainer linIx=" << linIx << " linPos=" << linPos);
assert(linIx==linPos);
//erase from body, we also erase from linIntrs below
b1->intrs.erase(I);
@@ -92,7 +88,6 @@
return true;
}
-
const shared_ptr<Interaction>& InteractionContainer::find(Body::id_t id1,Body::id_t id2){
assert(bodies);
if (id1>id2) swap(id1,id2);
@@ -105,17 +100,12 @@
else { empty=shared_ptr<Interaction>(); return empty; }
}
-// end internal functions
-
-// the rest uses internal functions to access data structures, and does not have to be modified if they change
-
bool InteractionContainer::insert(Body::id_t id1,Body::id_t id2)
{
shared_ptr<Interaction> i(new Interaction(id1,id2) );
return insert(i);
}
-
void InteractionContainer::requestErase(Body::id_t id1, Body::id_t id2){
const shared_ptr<Interaction> I=find(id1,id2); if(!I) return;
I->reset();
@@ -141,7 +131,7 @@
};
void InteractionContainer::preSave(InteractionContainer&){
- FOREACH(const shared_ptr<Interaction>& I, *this){
+ for(const shared_ptr<Interaction>& I : *this) {
if(I->geom || I->phys) interaction.push_back(I);
// since requestErase'd interactions have no interaction physics/geom, they are not saved
}
@@ -149,11 +139,10 @@
}
void InteractionContainer::postSave(InteractionContainer&){ interaction.clear(); }
-
void InteractionContainer::preLoad(InteractionContainer&){ interaction.clear(); }
void InteractionContainer::postLoad__calledFromScene(const shared_ptr<BodyContainer>& bb){
- bodies=&bb->body; // update the internal pointer
+ bodies=&bb->body;
clear();
FOREACH(const shared_ptr<Interaction>& I, interaction){
Body::id_t id1=I->getId1(), id2=I->getId2();
@@ -165,4 +154,3 @@
}
interaction.clear();
}
-
=== modified file 'core/InteractionContainer.hpp'
--- core/InteractionContainer.hpp 2015-07-30 16:08:08 +0000
+++ core/InteractionContainer.hpp 2015-11-18 07:06:16 +0000
@@ -56,9 +56,6 @@
// required by the class factory... :-|
InteractionContainer(): currSize(0),dirty(false),serializeSorted(false),iterColliderLastRun(-1){
bodies=NULL;
-// #ifdef YADE_OPENMP
-// threadsPendingErase.resize(omp_get_max_threads());
-// #endif
}
void clear();
// iterators
@@ -73,12 +70,20 @@
bool insert(const shared_ptr<Interaction>& i);
//3rd parameter is used to remove I from linIntrs (in conditionalyEraseNonReal()) when body b1 has been removed
bool erase(Body::id_t id1,Body::id_t id2,int linPos);
+
const shared_ptr<Interaction>& find(Body::id_t id1,Body::id_t id2);
-// bool found(Body::id_t id1,Body::id_t id2);
inline bool found(const Body::id_t& id1,const Body::id_t& id2){
assert(bodies);
- if(id2>=(Body::id_t)bodies->size()) return false;
- return (id1>id2)?(*bodies)[id2]->intrs.count(id1):(*bodies)[id1]->intrs.count(id2);}
+ if(id2>=(Body::id_t)bodies->size() or (id1 == id2)) {
+ return false;
+ } else {
+ if (id1>id2) {
+ return (*bodies)[id2]->intrs.count(id1);
+ } else {
+ return (*bodies)[id1]->intrs.count(id2);
+ }
+ }
+ }
// index access
shared_ptr<Interaction>& operator[](size_t id){return linIntrs[id];}
const shared_ptr<Interaction>& operator[](size_t id) const { return linIntrs[id];}
@@ -135,7 +140,6 @@
}
#endif
}
-
// we must call Scene's ctor (and from Scene::postLoad), since we depend on the existing BodyContainer at that point.
void postLoad__calledFromScene(const shared_ptr<BodyContainer>&);
void preLoad(InteractionContainer&);
=== modified file 'core/Omega.cpp'
--- core/Omega.cpp 2015-11-06 20:00:30 +0000
+++ core/Omega.cpp 2015-11-18 07:06:16 +0000
@@ -8,27 +8,23 @@
* GNU General Public License v2 or later. See file LICENSE for details. *
*************************************************************************/
-#include"Omega.hpp"
-#include"Scene.hpp"
-#include"TimeStepper.hpp"
-#include"ThreadRunner.hpp"
-#include<lib/base/Math.hpp>
-#include<lib/multimethods/FunctorWrapper.hpp>
-#include<lib/multimethods/Indexable.hpp>
-#include<boost/algorithm/string.hpp>
-#include<boost/thread/mutex.hpp>
+#include "Omega.hpp"
+#include "Scene.hpp"
+#include "TimeStepper.hpp"
+#include "ThreadRunner.hpp"
+#include <lib/base/Math.hpp>
+#include <lib/multimethods/FunctorWrapper.hpp>
+#include <lib/multimethods/Indexable.hpp>
+#include <lib/serialization/ObjectIO.hpp>
+#include <boost/algorithm/string.hpp>
+#include <boost/thread/mutex.hpp>
#include <boost/filesystem.hpp>
-#include<lib/serialization/ObjectIO.hpp>
-
-
-#include<cxxabi.h>
-
class RenderMutexLock: public boost::mutex::scoped_lock{
public:
- RenderMutexLock(): boost::mutex::scoped_lock(Omega::instance().renderMutex){/* cerr<<"Lock renderMutex"<<endl; */}
- ~RenderMutexLock(){/* cerr<<"Unlock renderMutex"<<endl;*/ }
+ RenderMutexLock(): boost::mutex::scoped_lock(Omega::instance().renderMutex){}
+ ~RenderMutexLock(){}
};
CREATE_LOGGER(Omega);
@@ -38,17 +34,19 @@
const shared_ptr<Scene>& Omega::getScene(){return scenes.at(currentSceneNb);}
void Omega::resetCurrentScene(){ RenderMutexLock lock; scenes.at(currentSceneNb) = shared_ptr<Scene>(new Scene);}
-void Omega::resetScene(){ resetCurrentScene(); }//RenderMutexLock lock; scene = shared_ptr<Scene>(new Scene);}
+void Omega::resetScene(){ resetCurrentScene(); }
void Omega::resetAllScenes(){
RenderMutexLock lock;
scenes.resize(1);
scenes[0] = shared_ptr<Scene>(new Scene);
currentSceneNb=0;
}
+
int Omega::addScene(){
scenes.push_back(shared_ptr<Scene>(new Scene));
return scenes.size()-1;
}
+
void Omega::switchToScene(int i) {
if (i<0 || i>=int(scenes.size())) {
LOG_ERROR("Scene "<<i<<" has not been created yet, no switch.");
@@ -57,8 +55,6 @@
currentSceneNb=i;
}
-
-
Real Omega::getRealTime(){
return (boost::posix_time::microsec_clock::local_time()-startupLocalTime).total_milliseconds()/1e3;
}
@@ -67,7 +63,6 @@
return boost::posix_time::microsec_clock::local_time()-startupLocalTime;
}
-
void Omega::initTemps(){
char dirTemplate[]="/tmp/yade-XXXXXX";
tmpFileDir=mkdtemp(dirTemplate);
@@ -125,7 +120,6 @@
}
}
-
void Omega::pause(){
if (simulationLoop && simulationLoop->looping()){
simulationLoop->stop();
@@ -196,7 +190,6 @@
}
}
-
bool Omega::isInheritingFrom(const string& className, const string& baseClassName){
return (dynlibs[className].baseClasses.find(baseClassName)!=dynlibs[className].baseClasses.end());
}
@@ -237,9 +230,7 @@
if(isMem && memSavedSimulations.count(f)==0) throw runtime_error("Cannot load nonexistent memory-saved simulation "+f);
if(!quiet) LOG_INFO("Loading file "+f);
- //shared_ptr<Scene> scene = getScene();
shared_ptr<Scene>& scene = scenes[currentSceneNb];
- //shared_ptr<Scene>& scene = getScene();
{
stop(); // stop current simulation if running
resetScene();
@@ -257,14 +248,10 @@
if(!quiet) LOG_DEBUG("Simulation loaded");
}
-
-
void Omega::saveSimulation(const string& f, bool quiet){
if(f.size()==0) throw runtime_error("f of file to save has zero length.");
if(!quiet) LOG_INFO("Saving file " << f);
- //shared_ptr<Scene> scene = getScene();
shared_ptr<Scene>& scene = scenes[currentSceneNb];
- //shared_ptr<Scene>& scene = getScene();
if(boost::algorithm::starts_with(f,":memory:")){
if(memSavedSimulations.count(f)>0 && !quiet) LOG_INFO("Overwriting in-memory saved simulation "<<f);
ostringstream oss;
@@ -272,12 +259,7 @@
memSavedSimulations[f]=oss.str();
}
else {
- // handles automatically the XML/binary distinction as well as gz/bz2 compression
yade::ObjectIO::save(f,"scene",scene);
}
sceneFile=f;
}
-
-
-
-
=== modified file 'core/Omega.hpp'
--- core/Omega.hpp 2014-10-15 06:44:01 +0000
+++ core/Omega.hpp 2015-11-18 07:06:16 +0000
@@ -21,7 +21,6 @@
#include <lib/factory/ClassFactory.hpp>
#include <lib/base/Singleton.hpp>
-
#include "SimulationFlow.hpp"
@@ -79,7 +78,6 @@
* 3. Omega when substantial changes to the scene are being made (bodies being deleted, simulation loaded etc) so that GL doesn't access those and crash */
boost::try_mutex renderMutex;
-
void run();
void pause();
void step();
=== modified file 'core/SimulationFlow.cpp'
--- core/SimulationFlow.cpp 2014-10-15 06:44:01 +0000
+++ core/SimulationFlow.cpp 2015-11-18 07:06:16 +0000
@@ -17,7 +17,10 @@
{
Scene* scene=Omega::instance().getScene().get();
if (!scene) throw logic_error("SimulationFlow::singleAction: no Scene object?!");
- if(scene->subStepping) { LOG_INFO("Sub-stepping disabled when running simulation continuously."); scene->subStepping=false; }
+ if(scene->subStepping) {
+ LOG_INFO("Sub-stepping disabled when running simulation continuously.");
+ scene->subStepping=false;
+ }
scene->moveToNextTimeStep();
if(scene->stopAtIter>0 && scene->iter==scene->stopAtIter) setTerminate(true);
if(scene->stopAtTime>0 && scene->time==scene->stopAtTime) setTerminate(true);
=== modified file 'core/SimulationFlow.hpp'
--- core/SimulationFlow.hpp 2010-08-26 16:56:07 +0000
+++ core/SimulationFlow.hpp 2015-11-18 07:06:16 +0000
@@ -10,8 +10,7 @@
#include "ThreadWorker.hpp"
-class SimulationFlow // FIXME ; bad name
- : public ThreadWorker
+class SimulationFlow : public ThreadWorker
{
public:
virtual void singleAction();
=== modified file 'core/State.cpp'
--- core/State.cpp 2014-10-15 06:44:01 +0000
+++ core/State.cpp 2015-11-18 07:06:16 +0000
@@ -4,24 +4,38 @@
CREATE_LOGGER(State);
void State::setDOFfromVector3r(Vector3r disp,Vector3r rot){
- blockedDOFs=((disp[0]==1.0)?DOF_X :0)|((disp[1]==1.0)?DOF_Y :0)|((disp[2]==1.0)?DOF_Z :0)|
- ((rot [0]==1.0)?DOF_RX:0)|((rot [1]==1.0)?DOF_RY:0)|((rot [2]==1.0)?DOF_RZ:0);
+ blockedDOFs=((disp[0]==1.0)?DOF_X :0)|
+ ((disp[1]==1.0)?DOF_Y :0)|
+ ((disp[2]==1.0)?DOF_Z :0)|
+ ((rot [0]==1.0)?DOF_RX:0)|
+ ((rot [1]==1.0)?DOF_RY:0)|
+ ((rot [2]==1.0)?DOF_RZ:0);
}
std::string State::blockedDOFs_vec_get() const {
- std::string ret;
- #define _SET_DOF(DOF_ANY,ch) if((blockedDOFs & State::DOF_ANY)!=0) ret.push_back(ch);
- _SET_DOF(DOF_X,'x'); _SET_DOF(DOF_Y,'y'); _SET_DOF(DOF_Z,'z'); _SET_DOF(DOF_RX,'X'); _SET_DOF(DOF_RY,'Y'); _SET_DOF(DOF_RZ,'Z');
- #undef _SET_DOF
- return ret;
+ std::string ret;
+ #define _SET_DOF(DOF_ANY,ch) if((blockedDOFs & State::DOF_ANY)!=0) ret.push_back(ch);
+ _SET_DOF(DOF_X,'x');
+ _SET_DOF(DOF_Y,'y');
+ _SET_DOF(DOF_Z,'z');
+
+ _SET_DOF(DOF_RX,'X');
+ _SET_DOF(DOF_RY,'Y');
+ _SET_DOF(DOF_RZ,'Z');
+ #undef _SET_DOF
+ return ret;
}
void State::blockedDOFs_vec_set(const std::string& dofs){
- blockedDOFs=0;
- FOREACH(char c, dofs){
- #define _GET_DOF(DOF_ANY,ch) if(c==ch) { blockedDOFs|=State::DOF_ANY; continue; }
- _GET_DOF(DOF_X,'x'); _GET_DOF(DOF_Y,'y'); _GET_DOF(DOF_Z,'z'); _GET_DOF(DOF_RX,'X'); _GET_DOF(DOF_RY,'Y'); _GET_DOF(DOF_RZ,'Z');
- #undef _GET_DOF
- throw std::invalid_argument("Invalid DOF specification `"+boost::lexical_cast<string>(c)+"' in '"+dofs+"', characters must be ∈{x,y,z,X,Y,Z}.");
- }
+ blockedDOFs=0;
+ for(char c : dofs) {
+ #define _GET_DOF(DOF_ANY,ch) if(c==ch) { blockedDOFs|=State::DOF_ANY; continue; }
+ _GET_DOF(DOF_X,'x');
+ _GET_DOF(DOF_Y,'y');
+ _GET_DOF(DOF_Z,'z');
+ _GET_DOF(DOF_RX,'X');
+ _GET_DOF(DOF_RY,'Y');
+ _GET_DOF(DOF_RZ,'Z');
+ #undef _GET_DOF
+ }
}
=== modified file 'core/State.hpp'
--- core/State.hpp 2014-11-17 09:21:14 +0000
+++ core/State.hpp 2015-11-18 07:06:16 +0000
@@ -4,9 +4,6 @@
#include<lib/multimethods/Indexable.hpp>
#include<core/Dispatcher.hpp>
-// delete later and remove relevant code, to not support old State.blockedDOFs=['x','y','rz'] syntax anymore
-//#define YADE_DEPREC_DOF_LIST
-
class State: public Serializable, public Indexable{
public:
/// linear motion (references to inside se3)
@@ -33,11 +30,7 @@
//! Getter of blockedDOFs for list of strings (e.g. DOF_X | DOR_RX | DOF_RZ → 'xXZ')
std::string blockedDOFs_vec_get() const;
//! Setter of blockedDOFs from string ('xXZ' → DOF_X | DOR_RX | DOF_RZ)
- #ifdef YADE_DEPREC_DOF_LIST
- void blockedDOFs_vec_set(const python::object&);
- #else
- void blockedDOFs_vec_set(const std::string& dofs);
- #endif
+ void blockedDOFs_vec_set(const std::string& dofs);
//! Return displacement (current-reference position)
Vector3r displ() const {return pos-refPos;}
=== modified file 'core/ThreadRunner.cpp'
--- core/ThreadRunner.cpp 2014-10-15 06:44:01 +0000
+++ core/ThreadRunner.cpp 2015-11-18 07:06:16 +0000
@@ -78,11 +78,8 @@
void ThreadRunner::stop()
{
- //std::cerr<<__FILE__<<":"<<__LINE__<<":"<<__FUNCTION__<<std::endl;
if(!m_looping) return;
- //std::cerr<<__FILE__<<":"<<__LINE__<<":"<<__FUNCTION__<<std::endl;
boost::mutex::scoped_lock lock(m_boolmutex);
- //std::cerr<<__FILE__<<":"<<__LINE__<<":"<<__FUNCTION__<<std::endl;
m_looping=false;
}
=== modified file 'core/TimeStepper.hpp'
--- core/TimeStepper.hpp 2014-07-19 19:52:41 +0000
+++ core/TimeStepper.hpp 2015-11-18 07:06:16 +0000
@@ -8,8 +8,6 @@
#pragma once
-#include <list>
-#include <vector>
#include "Interaction.hpp"
#include "GlobalEngine.hpp"
#include "Scene.hpp"