Commit 5935a6a7 authored by Anton Gladky's avatar Anton Gladky

Tiny code refactoring

parent b0f00cf3
......@@ -18,9 +18,6 @@
#include<lib/serialization/Serializable.hpp>
#include<lib/multimethods/Indexable.hpp>
class Scene;
class Interaction;
......@@ -39,7 +36,6 @@ class Body: public Serializable{
//! 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
......
......@@ -8,7 +8,6 @@
#include<omp.h>
#endif
CREATE_LOGGER(BodyContainer);
void BodyContainer::clear(){
......
......@@ -100,8 +100,6 @@ boost::python::list Indexable_getClassIndices(const shared_ptr<TopIndexable> i,
}
}
//! 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 @@ shared_ptr<DispatcherT> Dispatcher_ctor_list(const std::vector<shared_ptr<typena
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 @@ class Dispatcher1D : public Dispatcher,
, autoSymmetry
>
{
public :
typedef typename FunctorType::DispatchType1 baseClass;
typedef baseClass argType1;
......@@ -172,18 +163,13 @@ class Dispatcher1D : public Dispatcher,
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 @@ class Dispatcher2D : public Dispatcher,
REGISTER_ATTRIBUTES(Dispatcher,);
REGISTER_CLASS_AND_BASE(Dispatcher2D,Dispatcher DynLibDispatcher);
};
......@@ -37,8 +37,6 @@ class Functor: public Serializable
};
REGISTER_SERIALIZABLE(Functor);
template
<
class _DispatchType1,
......@@ -59,7 +57,6 @@ class Functor1D: public Functor,
/* do not REGISTER_ATTRIBUTES here, since we are template; derived classes should call REGISTER_ATTRIBUTES(Functor,(their)(own)(attributes)), bypassing Functor1D */
};
template
<
class _DispatchType1,
......
......@@ -36,10 +36,9 @@ class Interaction: public Serializable{
// 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)
......
......@@ -38,28 +38,25 @@ bool InteractionContainer::insert(const shared_ptr<Interaction>& i){
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 @@ bool InteractionContainer::erase(Body::id_t id1,Body::id_t id2, int linPos){
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 @@ bool InteractionContainer::erase(Body::id_t id1,Body::id_t id2, int linPos){
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 @@ const shared_ptr<Interaction>& InteractionContainer::find(Body::id_t id1,Body::i
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 @@ struct compPtrInteraction{
};
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::preSave(InteractionContainer&){
}
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 @@ void InteractionContainer::postLoad__calledFromScene(const shared_ptr<BodyContai
}
interaction.clear();
}
......@@ -56,9 +56,6 @@ class InteractionContainer: public Serializable{
// 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 @@ class InteractionContainer: public Serializable{
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 @@ class InteractionContainer: public Serializable{
}
#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&);
......
......@@ -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 map<string,DynlibDescriptor>& Omega::getDynlibsDescriptor(){return dynlibs
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 @@ void Omega::switchToScene(int i) {
currentSceneNb=i;
}
Real Omega::getRealTime(){
return (boost::posix_time::microsec_clock::local_time()-startupLocalTime).total_milliseconds()/1e3;
}
......@@ -67,7 +63,6 @@ boost::posix_time::time_duration Omega::getRealTime_duration(){
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::run(){
}
}
void Omega::pause(){
if (simulationLoop && simulationLoop->looping()){
simulationLoop->stop();
......@@ -196,7 +190,6 @@ void Omega::buildDynlibDatabase(const vector<string>& dynlibsList){
}
}
bool Omega::isInheritingFrom(const string& className, const string& baseClassName){
return (dynlibs[className].baseClasses.find(baseClassName)!=dynlibs[className].baseClasses.end());
}
......@@ -237,9 +230,7 @@ void Omega::loadSimulation(const string& f, bool quiet){
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 @@ void Omega::loadSimulation(const string& f, bool quiet){
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 @@ void Omega::saveSimulation(const string& f, bool quiet){
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;
}
......@@ -21,7 +21,6 @@
#include <lib/factory/ClassFactory.hpp>
#include <lib/base/Singleton.hpp>
#include "SimulationFlow.hpp"
......@@ -79,7 +78,6 @@ class Omega: public Singleton<Omega>{
* 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();
......
......@@ -17,7 +17,10 @@ void SimulationFlow::singleAction()
{
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);
......
......@@ -10,8 +10,7 @@
#include "ThreadWorker.hpp"
class SimulationFlow // FIXME ; bad name
: public ThreadWorker
class SimulationFlow : public ThreadWorker
{
public:
virtual void singleAction();
......
......@@ -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
}
}
......@@ -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 @@ class State: public Serializable, public Indexable{
//! 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;}
......
......@@ -78,11 +78,8 @@ void ThreadRunner::start()
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;
}
......
......@@ -8,8 +8,6 @@
#pragma once
#include <list>
#include <vector>
#include "Interaction.hpp"
#include "GlobalEngine.hpp"
#include "Scene.hpp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment