Commit 98f98ec5 authored by berenger-bramas's avatar berenger-bramas

Enable to load FMA (particules file format from FMM)

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/scalfmm/scalfmm/trunk@6 2616d619-271b-44dc-8df4-d4a8f33a7222
parent 12566236
......@@ -15,6 +15,8 @@ Doc/html/
tmp/
*.tmp
*.temp
*.fma
*.FMA
*~
*TAGS
*#*#
#ifndef FFMALOADER_HPP
#define FFMALOADER_HPP
// /!\ Please, you must read the license at the bottom of this page
#include <iostream>
#include <fstream>
#include "FAbstractLoader.hpp"
#include "../Utils/F3DPosition.hpp"
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFMALoader
* Please read the license
*
* Load a file with a format like :
* NB_particules Box_width Box_X Box_Y Box_Z // init
* X Y Z // one particule by line
* ....
* <code>
* FFMALoader<FBasicParticule> loader("../FMB++/Tests/particules.basic.txt"); <br>
* if(!loader.isValide()){ <br>
* std::cout << "Loader Error\n"; <br>
* return 1; <br>
* } <br>
* <br>
* FOctree<FBasicParticule, TestCell, 10, 3> tree(loader.getBoxWidth(),loader.getCenterOfBox()); <br>
* <br>
* for(int idx = 0 ; idx < loader.getNumberOfParticules() ; ++idx){ <br>
* FBasicParticule* const part = new FBasicParticule(); <br>
* loader.fillParticule(part); <br>
* tree.insert(part); <br>
* } <br>
* </code>
*/
template <class ParticuleClass>
class FFMALoader : public FAbstractLoader<ParticuleClass> {
protected:
std::ifstream file; //< The file to read
F3DPosition centerOfBox; //< The center of box read from file
double boxWidth; //< the box width read from file
int nbParticules; //< the number of particules read from file
public:
/**
* The constructor need the file name
* @param filename the name of the file to open
* you can test if file is successfuly open by calling isValide()
*/
FFMALoader(const char* const filename): file(filename,std::ifstream::in){
// test if open
if(this->file.is_open()){
double x,y,z;
this->file >> this->nbParticules >> this->boxWidth >> x >> y >> z;
this->centerOfBox.setPosition(x,y,z);
this->boxWidth *= 2;
}
else {
this->boxWidth = 0;
this->nbParticules = 0;
}
}
/**
* Default destructor, simply close the file
*/
virtual ~FFMALoader(){
file.close();
}
/**
* To know if file is open and ready to read
* @return true if loader can work
*/
bool isValide() const{
return this->file.is_open() && !this->file.eof();
}
/**
* To get the number of particules from this loader
* @param the number of particules the loader can fill
*/
long getNumberOfParticules() const{
return this->nbParticules;
}
/**
* The center of the box from the simulation file opened by the loader
* @return box center
*/
F3DPosition getCenterOfBox() const{
return this->centerOfBox;
}
/**
* The box width from the simulation file opened by the loader
* @return box width
*/
double getBoxWidth() const{
return this->boxWidth;
}
/**
* Fill a particule
* @warning to work with the loader, particules has to expose a setPosition method
* @param the particule to fill
*/
void fillParticule(ParticuleClass* const inParticule){
double x,y,z,data;
this->file >> x >> y >> z >> data;
inParticule->setPosition(F3DPosition(x,y,z));
}
};
#endif //FFMALoader_HPP
// [--LICENSE--]
// /!\ Please, you must read the license at the bottom of this page
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "../Sources/Containers/FOctree.hpp"
#include "../Sources/Containers/FList.hpp"
#include "../Sources/Utils/FAssertable.hpp"
#include "../Sources/Utils/F3DPosition.hpp"
#include "../Sources/Core/FBasicParticule.hpp"
#include "../Sources/Files/FFMALoader.hpp"
// Compile by : g++ testLoaderFMA.cpp ../Sources/Utils/FAssertable.cpp -O2 -lgomp -fopenmp -o testLoaderFMA.exe
// Fake cell class
class TestCell{
};
/**
* In this file we show an example of BasicParticule and BasicLoader use
* Démarrage de /home/berenger/Dropbox/Personnel/FMB++/FMB++-build-desktop/FMB++...
* Inserting 2000000 particules ...
* Done (5.77996).
* Deleting particules ...
* Done (0.171918).
*/
int main(int , char ** ){
// we store all particules to be able to dealloc
FList<FBasicParticule*> particules;
// Use testLoaderCreate.exe to create this file
const char* const filename = "testLoaderFMA.fma";
// open basic particules loader
FFMALoader<FBasicParticule> loader(filename);
if(!loader.isValide()){
std::cout << "Loader Error, " << filename << "is missing\n";
return 1;
}
// otree
FOctree<FBasicParticule, TestCell, 10, 3> tree(loader.getBoxWidth(),loader.getCenterOfBox());
// -----------------------------------------------------
std::cout << "Inserting " << loader.getNumberOfParticules() << " particules ..." << std::endl;
const double InsertingStartTime = omp_get_wtime();
for(int idx = 0 ; idx < loader.getNumberOfParticules() ; ++idx){
FBasicParticule* const part = new FBasicParticule();
particules.pushFront(part);
loader.fillParticule(part);
tree.insert(part);
}
const double InsertingEndTime = omp_get_wtime();
std::cout << "Done " << "(" << (InsertingEndTime-InsertingStartTime) << ")." << std::endl;
// -----------------------------------------------------
std::cout << "Deleting particules ..." << std::endl;
const double DeletingStartTime = omp_get_wtime();
while(particules.getSize()){
delete particules.popFront();
}
const double DeletingEndTime = omp_get_wtime();
std::cout << "Done " << "(" << (DeletingEndTime-DeletingStartTime) << ")." << std::endl;
// -----------------------------------------------------
return 0;
}
// [--LICENSE--]
// /!\ Please, you must read the license at the bottom of this page
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// This file can generate basic particules files in the FMA format
// g++ testLoaderFMACreate.cpp -o testLoaderFMACreate.exe
int main(int , char ** ){
// Nb of particules
const long NbParticules = 200000;
// Center of the box
const double XCenter = 0.5;
const double YCenter = 0.5;
const double ZCenter = 0.5;
// Box width
const double BoxWidth = 1.0/2;
// Output file please let .temp extension
const char * const Output = "testLoaderFMA.fma";
// Create file
std::ofstream myfile;
myfile.open (Output);
if(!myfile.is_open()){
std::cout << "Cannot create " << Output << "\n";
return 1;
}
std::cout << "Working...\n";
// System properties
myfile << NbParticules << "\n";
myfile << BoxWidth << "\t" << XCenter << "\t" << YCenter << "\t" << ZCenter;
// Generate particules
for( long idx = 0 ; idx < NbParticules ; ++idx ){
const double px = ((double(rand())/RAND_MAX) * BoxWidth * 2) + XCenter - BoxWidth;
const double py = ((double(rand())/RAND_MAX) * BoxWidth * 2) + YCenter - BoxWidth;
const double pz = ((double(rand())/RAND_MAX) * BoxWidth * 2) + ZCenter - BoxWidth;
myfile << "\n" << px << "\t" << py << "\t" << pz << "\t" << (0.01);
}
myfile.close();
std::cout << "Done\n";
return 0;
}
// [--LICENSE--]
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