Skip to content

Commit 25d9f41

Browse files
committed
AL: Some cleanup and use of STL algorithms instead of plain loops.
1 parent 56d61bd commit 25d9f41

9 files changed

+104
-118
lines changed

src/Common/MemFunArg.hh

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ namespace COOLFluiD {
1515

1616
//////////////////////////////////////////////////////////////////////////////
1717

18+
template <class InputIterator, class Function, class Predicate>
19+
Function for_each_if(InputIterator first, InputIterator last, Function fn, Predicate pr, bool flag)
20+
{
21+
while (first!=last) {
22+
if (pr(*first) == flag) {
23+
fn (*first);
24+
}
25+
++first;
26+
}
27+
return fn;
28+
}
29+
30+
//////////////////////////////////////////////////////////////////////////////
31+
1832
/// Class to allow STL algorithms to call member functions on this pointer
1933
/// This is similar to mem_fun_t from the STL standard
2034
template < typename TYPE, typename RET , typename ARG >

src/Framework/MultiMethodTuple.hh

+17-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,23 @@ public: // functions
102102
cf_assert(m_mList.size() == m_mMap.size());
103103
return m_mList.size();
104104
}
105-
105+
106+
/// Get the beginning
107+
TMETHOD** begin()
108+
{
109+
cf_assert(m_mList.size() == m_mMap.size());
110+
cf_assert(m_mList.size() > 0);
111+
return &m_mList[0];
112+
}
113+
114+
/// Get the end
115+
TMETHOD** end()
116+
{
117+
cf_assert(m_mList.size() == m_mMap.size());
118+
cf_assert(m_mList.size() > 0);
119+
return &m_mList[0] + m_mList.size();
120+
}
121+
106122
/// Add a pointer to a new method to the list
107123
void addPtr(const std::string& name, Common::SelfRegistPtr<TMETHOD>& met)
108124
{

src/Framework/OnlyMeshSubSystem.cxx

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ void OnlyMeshSubSystem::unsetup()
470470

471471
//////////////////////////////////////////////////////////////////////////////
472472

473-
vector<Method*> OnlyMeshSubSystem::getMethodList() const
473+
vector<Method*> OnlyMeshSubSystem::getMethodList()
474474
{
475475
vector<Method*> mList;
476476

src/Framework/OnlyMeshSubSystem.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ protected: // functions
103103

104104
/// Gets a vector with all the Methods this OnlyMeshSubSystem has
105105
/// @return vector with Method pointers.
106-
virtual std::vector<Framework::Method*> getMethodList() const;
107-
106+
virtual std::vector<Framework::Method*> getMethodList();
107+
108108
/// Set the Method's collaborators.
109109
virtual void setCollaborators();
110110

src/Framework/PrePostProcessingSubSystem.cxx

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void PrePostProcessingSubSystem::unsetup()
157157

158158
//////////////////////////////////////////////////////////////////////////////
159159

160-
vector<Method*> PrePostProcessingSubSystem::getMethodList() const
160+
vector<Method*> PrePostProcessingSubSystem::getMethodList()
161161
{
162162
vector<Method*> mList = StandardSubSystem::getMethodList();
163163
return mList;

src/Framework/PrePostProcessingSubSystem.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ protected: // functions
5252

5353
/// Gets a vector with all the Methods this PrePostProcessingSubSystem has
5454
/// @return vector with Method pointers.
55-
std::vector<Framework::Method*> getMethodList() const;
56-
55+
std::vector<Framework::Method*> getMethodList();
56+
5757
/// Set the Method's collaborators.
5858
void setCollaborators();
5959

src/Framework/StandardSubSystem.cxx

+57-107
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "Common/CFLog.hh"
1010
#include "Common/NullPointerException.hh"
1111
#include "Common/EventHandler.hh"
12+
#include "Common/MemFunArg.hh"
1213

1314
#include "Environment/FileHandlerOutput.hh"
1415
#include "Environment/DirPaths.hh"
@@ -38,6 +39,9 @@
3839
#include "Framework/Framework.hh"
3940
#include "Framework/SimulationStatus.hh"
4041

42+
// #include <boost/mpl/list.hpp>
43+
// #include <boost/mpl/for_each.hpp>
44+
4145
//////////////////////////////////////////////////////////////////////////////
4246

4347
using namespace std;
@@ -309,67 +313,44 @@ void StandardSubSystem::buildMeshData()
309313
// allocate all the mesh data
310314
vector <Common::SafePtr<MeshData> > meshDataVector =
311315
MeshDataStack::getInstance().getAllEntries();
312-
for(CFuint iMesh = 0; iMesh < meshDataVector.size(); iMesh++) {
313-
meshDataVector[iMesh]->reallocate();
314-
}
316+
317+
for_each (meshDataVector.begin(), meshDataVector.end(),
318+
safeptr_mem_fun(&MeshData::reallocate));
315319

316320
CFLog(NOTICE,"-------------------------------------------------------------\n");
317321
CFLog(NOTICE,"Setting up all MeshCreator's\n");
318322
CFLog(NOTICE,"-------------------------------------------------------------\n");
319-
323+
320324
// finally setup the mesh creators
321-
for(CFuint iMC=0; iMC < m_meshCreator.size(); iMC++)
322-
{
323-
if(!m_meshCreator[iMC]->isNonRootMethod())
324-
{
325-
m_meshCreator[iMC]->setMethod();
326-
}
327-
}
328-
329-
/// @TODO Why not FREE the mesh creator here also??????
330-
325+
Common::for_each_if (m_meshCreator.begin(), m_meshCreator.end(),
326+
mem_fun(&MeshCreator::setMethod),
327+
mem_fun(&MeshCreator::isNonRootMethod), false);
328+
331329
CFLog(NOTICE,"-------------------------------------------------------------\n");
332330
CFLog(NOTICE,"Building MeshData's\n");
333331
CFLog(NOTICE,"-------------------------------------------------------------\n");
334-
335-
// Create the CFMeshData for each Namespace
336-
for(CFuint iMC=0; iMC < m_meshCreator.size(); iMC++)
337-
{
338-
if( !m_meshCreator[iMC]->isNonRootMethod() )
339-
{
340-
m_meshCreator[iMC]->generateMeshData();
341-
}
342-
}
343-
332+
333+
Common::for_each_if (m_meshCreator.begin(), m_meshCreator.end(),
334+
mem_fun(&MeshCreator::generateMeshData),
335+
mem_fun(&MeshCreator::isNonRootMethod), false);
336+
344337
// Process the CFMeshData to do:
345338
// - renumbering
346339
// - conversion FVM <-> FEM
347-
for(CFuint iMC=0; iMC < m_meshCreator.size(); iMC++)
348-
{
349-
if(!m_meshCreator[iMC]->isNonRootMethod())
350-
{
351-
m_meshCreator[iMC]->processMeshData();
352-
}
353-
}
354-
340+
Common::for_each_if (m_meshCreator.begin(), m_meshCreator.end(),
341+
mem_fun(&MeshCreator::processMeshData),
342+
mem_fun(&MeshCreator::isNonRootMethod), false);
343+
355344
// Use the CFMeshData to build the mesh
356-
for(CFuint iMC=0; iMC < m_meshCreator.size(); iMC++)
357-
{
358-
if(!m_meshCreator[iMC]->isNonRootMethod())
359-
{
360-
m_meshCreator[iMC]->buildMeshData();
361-
}
362-
}
363-
345+
Common::for_each_if (m_meshCreator.begin(), m_meshCreator.end(),
346+
mem_fun(&MeshCreator::buildMeshData),
347+
mem_fun(&MeshCreator::isNonRootMethod), false);
348+
364349
/// @todo Creating a MeshDataAdapter for the MeshWriter should be temporary
365350
/// get ride of the Adapter, make it work directly with MeshData
366-
for (CFuint i = 0; i < m_outputFormat.size(); ++i)
367-
{
368-
if(!m_outputFormat[i]->isNonRootMethod())
369-
{
370-
m_outputFormat[i]->bindData();
371-
}
372-
}
351+
Common::for_each_if (m_outputFormat.begin(), m_outputFormat.end(),
352+
mem_fun(&OutputFormatter::bindData),
353+
mem_fun(&OutputFormatter::isNonRootMethod), false);
373354

374355
for(CFuint iMeshData = 0; iMeshData < meshDataVector.size(); iMeshData++) {
375356
bool isNonRoot = false;
@@ -668,13 +649,12 @@ void StandardSubSystem::run()
668649
CFLog(VERBOSE, "StandardSubSystem::run() => m_errorEstimatorMethod.apply()\n");
669650
// estimate the error one final time
670651
m_errorEstimatorMethod.apply(mem_fun<void,ErrorEstimatorMethod>(&ErrorEstimatorMethod::estimate));
671-
672-
for(CFuint i = 0; i < subSysStatusVec.size() ; i++)
673-
{
674-
subSysStatusVec[i]->stopWatch();
675-
}
676-
stopTimer.stop ();
677-
652+
653+
std::for_each (subSysStatusVec.begin(), subSysStatusVec.end(),
654+
safeptr_mem_fun(&SubSystemStatus::stopWatch));
655+
656+
stopTimer.stop();
657+
678658
CFLog(NOTICE, "SubSystem WallTime: " << stopTimer << "s\n");
679659
m_duration = subSysStatusVec[0]->readWatchHMS();
680660

@@ -711,7 +691,10 @@ void StandardSubSystem::unsetup()
711691

712692
bool force = true;
713693
writeSolution(force);
714-
694+
695+
// typedef boost::mpl::list<ErrorEstimatorMethod, SpaceMethod> acts;
696+
// boost::mpl::for_each<acts>(do_this_wrapper());
697+
715698
CFLog(VERBOSE, "StandardSubSystem::unsetup() => OutputFormatter\n");
716699
// unset all the methods
717700
m_outputFormat.apply
@@ -759,50 +742,21 @@ void StandardSubSystem::unsetup()
759742

760743
//////////////////////////////////////////////////////////////////////////////
761744

762-
vector<Method*> StandardSubSystem::getMethodList() const
745+
vector<Method*> StandardSubSystem::getMethodList()
763746
{
764747
vector<Method*> mList;
765-
766-
for (CFuint i = 0; i < m_meshCreator.size(); ++i) {
767-
mList.push_back(m_meshCreator[i]);
768-
}
769-
770-
for (CFuint i = 0; i < m_couplerMethod.size(); ++i) {
771-
mList.push_back(m_couplerMethod[i]);
772-
}
773-
774-
for (CFuint i = 0; i < m_meshAdapterMethod.size(); ++i) {
775-
mList.push_back(m_meshAdapterMethod[i]);
776-
}
777-
778-
for (CFuint i = 0; i < m_errorEstimatorMethod.size(); ++i) {
779-
mList.push_back(m_errorEstimatorMethod[i]);
780-
}
781-
782-
for (CFuint i = 0; i < m_linearSystemSolver.size(); ++i) {
783-
mList.push_back(m_linearSystemSolver[i]);
784-
}
785-
786-
for (CFuint i = 0; i < m_convergenceMethod.size(); ++i) {
787-
mList.push_back(m_convergenceMethod[i]);
788-
}
789-
790-
for (CFuint i = 0; i < m_spaceMethod.size(); ++i) {
791-
mList.push_back(m_spaceMethod[i]);
792-
}
793-
794-
for (CFuint i = 0; i < m_dataPreProcessing.size(); ++i) {
795-
mList.push_back(m_dataPreProcessing[i]);
796-
}
797-
798-
for (CFuint i = 0; i < m_dataPostProcessing.size(); ++i) {
799-
mList.push_back(m_dataPostProcessing[i]);
800-
}
801-
802-
for (CFuint i = 0; i < m_outputFormat.size(); ++i) {
803-
mList.push_back(m_outputFormat[i]);
804-
}
805-
748+
749+
copy(m_meshCreator.begin(), m_meshCreator.end(), back_inserter(mList));
750+
copy(m_couplerMethod.begin(), m_couplerMethod.end(), back_inserter(mList));
751+
copy(m_meshAdapterMethod.begin(), m_meshAdapterMethod.end(), back_inserter(mList));
752+
copy(m_errorEstimatorMethod.begin(), m_errorEstimatorMethod.end(), back_inserter(mList));
753+
copy(m_linearSystemSolver.begin(), m_linearSystemSolver.end(), back_inserter(mList));
754+
copy(m_convergenceMethod.begin(), m_convergenceMethod.end(), back_inserter(mList));
755+
copy(m_spaceMethod.begin(), m_spaceMethod.end(), back_inserter(mList));
756+
copy(m_dataPreProcessing.begin(), m_dataPreProcessing.end(), back_inserter(mList));
757+
copy(m_dataPostProcessing.begin(), m_dataPostProcessing.end(), back_inserter(mList));
758+
copy(m_outputFormat.begin(), m_outputFormat.end(), back_inserter(mList));
759+
806760
return mList;
807761
}
808762

@@ -850,27 +804,23 @@ Common::Signal::return_t StandardSubSystem::modifyRestartAction(Common::Signal::
850804
std::string subsystemName = getName();
851805
std::string nspName = m_meshCreator[iMC]->getNamespace();
852806
std::string filename = SimulationStatus::getInstance().getLastOutputFile(subsystemName, nspName).string();
807+
853808
CFLog(NOTICE,"Restarting from file : " << filename << "\n");
854-
855-
// CF_DEBUG_OBJ(Environment::DirPaths::getInstance().getWorkingDir().string());
856-
// CF_DEBUG_OBJ(Environment::DirPaths::getInstance().getResultsDir().string());
857-
858-
DirPaths::getInstance().setWorkingDir(
859-
DirPaths::getInstance().getResultsDir().string()
860-
);
809+
810+
DirPaths::getInstance().setWorkingDir(DirPaths::getInstance().getResultsDir().string());
861811
m_meshCreator[iMC]->modifyFileNameForRestart(filename);
862812
}
863813
}
864-
814+
865815
m_initialTime = SimulationStatus::getInstance().getSimulationTime(getName());
866816
vector< SafePtr<SubSystemStatus> > subSystemStatusVec =
867817
SubSystemStatusStack::getInstance().getAllEntries();
868-
818+
869819
for(CFuint i=0; i<subSystemStatusVec.size(); ++i)
870820
{
871821
subSystemStatusVec[i]->setCurrentTimeDim(m_initialTime);
872822
}
873-
823+
874824
return Common::Signal::return_t ();
875825
}
876826

src/Framework/StandardSubSystem.hh

+8-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ protected: // functions
109109

110110
/// Gets a vector with all the Methods this StandardSubSystem has
111111
/// @return vector with Method pointers.
112-
virtual std::vector<Framework::Method*> getMethodList() const;
113-
112+
virtual std::vector<Framework::Method*> getMethodList();
113+
114114
/// Set the Method's collaborators.
115115
virtual void setCollaborators();
116116

@@ -129,6 +129,12 @@ protected: // functions
129129
/// Dump the states to file
130130
void dumpStates();
131131

132+
// struct do_this_wrapper {
133+
// template<typename U> void operator()(U* uptr) {
134+
// uptr->unsetMethod();
135+
// }
136+
// };
137+
132138
protected: // data
133139

134140
/// Duration of the simulation of this SubSystem

src/Framework/SubSystem.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ protected: // helper functions
202202

203203
/// Gets a vector with all the Methods this SubSystem has
204204
/// @return vector with Method pointers.
205-
virtual std::vector<Framework::Method*> getMethodList() const = 0;
206-
205+
virtual std::vector<Framework::Method*> getMethodList() = 0;
206+
207207
/// Allocates all sockets in all methods of this SubSystem
208208
void allocateSockets();
209209

0 commit comments

Comments
 (0)