diff --git a/.gitignore b/.gitignore index 66ccf5dc2012792b3537a635eba8766fa94abf06..4ad7ade5028bb4082f72bedf0c67326d96d96211 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ Doc/html/ tmp/ *.tmp *.temp +*.fma +*.FMA *~ *TAGS *#*# diff --git a/Sources/Files/FFMALoader.hpp b/Sources/Files/FFMALoader.hpp new file mode 100644 index 0000000000000000000000000000000000000000..99dffb1fcdb6d96d5946140f458399f8b745765b --- /dev/null +++ b/Sources/Files/FFMALoader.hpp @@ -0,0 +1,119 @@ +#ifndef FFMALOADER_HPP +#define FFMALOADER_HPP +// /!\ Please, you must read the license at the bottom of this page + +#include +#include + +#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 +* .... +* +* FFMALoader loader("../FMB++/Tests/particules.basic.txt");
+* if(!loader.isValide()){
+* std::cout << "Loader Error\n";
+* return 1;
+* }
+*
+* FOctree tree(loader.getBoxWidth(),loader.getCenterOfBox());
+*
+* for(int idx = 0 ; idx < loader.getNumberOfParticules() ; ++idx){
+* FBasicParticule* const part = new FBasicParticule();
+* loader.fillParticule(part);
+* tree.insert(part);
+* }
+*
+*/ +template +class FFMALoader : public FAbstractLoader { +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--] diff --git a/Tests/testLoaderFMA.cpp b/Tests/testLoaderFMA.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1621cabbc8b8b71d770e5a55c78c58ff7ba2369e --- /dev/null +++ b/Tests/testLoaderFMA.cpp @@ -0,0 +1,78 @@ +// /!\ Please, you must read the license at the bottom of this page + +#include + +#include +#include +#include + +#include + +#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 particules; + // Use testLoaderCreate.exe to create this file + const char* const filename = "testLoaderFMA.fma"; + + // open basic particules loader + FFMALoader loader(filename); + if(!loader.isValide()){ + std::cout << "Loader Error, " << filename << "is missing\n"; + return 1; + } + + // otree + FOctree 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--] diff --git a/Tests/testLoaderFMACreate.cpp b/Tests/testLoaderFMACreate.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2ec795d6e895841c8fe479cc824fa39f9554d7b --- /dev/null +++ b/Tests/testLoaderFMACreate.cpp @@ -0,0 +1,57 @@ +// /!\ Please, you must read the license at the bottom of this page + +#include +#include + +#include +#include +#include + +// 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--]