diff --git a/Src/Files/FEwalLoader.hpp b/Src/Files/FEwalLoader.hpp index e63e5207ab4a1c470fb5f3514e972f86b4396ec6..aa667d969148c01b401f30918fc8f029bad1cc6d 100644 --- a/Src/Files/FEwalLoader.hpp +++ b/Src/Files/FEwalLoader.hpp @@ -34,10 +34,11 @@ public: private: Type type; //< current type int index; //< current index in array + int indexInFile; //< current index in array public: // Basic constructor - FEwalParticle() : type(Undefined), index(-1) { + FEwalParticle() : type(Undefined), index(-1), indexInFile(-1) { } Type getType() const{ @@ -55,6 +56,14 @@ public: void setIndex( const int inIndex ){ index = inIndex; } + + int getIndexInFile() const{ + return indexInFile; + } + + void setIndexInFile( const int inIndex ){ + indexInFile = inIndex; + } }; @@ -189,7 +198,7 @@ public: inParticle.setPosition(x,y,z); inParticle.setForces(fx,fy,fz); //inParticle.setForces(vx,vy,vz); - inParticle.setIndex(index-1); + inParticle.setIndexInFile(index); if( strncmp(type, "OW", 2) == 0){ inParticle.setPhysicalValue(FReal(-0.82)); @@ -204,6 +213,143 @@ public: }; +template <class ParticleClass> +class FEwalBinLoader : public FAbstractLoader<ParticleClass> { +protected: + FILE* const file; //< The file to read + FPoint centerOfBox; //< The center of box read from file + double boxWidth; //< the box width read from file + int nbParticles; //< the number of particles read from file + double energy; + int removeWarning; + + template<class Type> + Type readValue(){ + int sizeBefore, sizeAfter; + Type value; + removeWarning = fread(&sizeBefore, sizeof(int), 1, file); + removeWarning = fread(&value, sizeof(Type), 1, file); + removeWarning = fread(&sizeAfter, sizeof(int), 1, file); + if( sizeBefore != sizeof(Type) ) printf("Error in loader ewal Size before %d should be %d\n", sizeBefore, sizeof(Type)); + if( sizeAfter != sizeof(Type) ) printf("Error in loader ewal Size after %d should be %d\n", sizeAfter, sizeof(Type)); + return value; + } + + template<class Type> + Type* readArray(Type array[], const int size){ + int sizeBefore, sizeAfter; + removeWarning = fread(&sizeBefore, sizeof(int), 1, file); + removeWarning = fread(array, sizeof(Type), size, file); + removeWarning = fread(&sizeAfter, sizeof(int), 1, file); + if( sizeBefore != int(sizeof(Type) * size) ) printf("Error in loader ewal Size before %d should be %d\n", sizeBefore, size*sizeof(Type)); + if( sizeAfter != int(sizeof(Type) * size) ) printf("Error in loader ewal Size after %d should be %d\n", sizeAfter, size*sizeof(Type)); + return array; + } + +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 hasNotFinished() + energy box size nb particles + [index charge x y z fx fy fz] + int double double ... + */ + FEwalBinLoader(const char* const filename): file(fopen(filename, "rb")) { + // test if open + if(this->file != NULL){ + energy = readValue<double>(); + double boxDim[3]; + boxWidth = readArray<double>(boxDim,3)[0]; + nbParticles = readValue<int>(); + + centerOfBox.setPosition(0.0,0.0,0.0); + } + else { + this->boxWidth = 0; + this->nbParticles = 0; + } + } + /** + * Default destructor, simply close the file + */ + virtual ~FEwalBinLoader(){ + fclose(file); + } + + /** + * To know if file is open and ready to read + * @return true if loader can work + */ + bool isOpen() const{ + return this->file != NULL; + } + + /** + * To get the number of particles from this loader + * @param the number of particles the loader can fill + */ + FSize getNumberOfParticles() const{ + return FSize(this->nbParticles); + } + + /** + * The center of the box from the simulation file opened by the loader + * @return box center + */ + FPoint getCenterOfBox() const{ + return this->centerOfBox; + } + + /** + * The box width from the simulation file opened by the loader + * @return box width + */ + FReal getBoxWidth() const{ + return this->boxWidth; + } + + FReal getEnergy() const{ + return this->energy; + } + + /** + * Fill a particle + * @warning to work with the loader, particles has to expose a setPosition method + * @param the particle to fill + [index charge x y z fx fy fz] + */ + void fillParticle(ParticleClass& inParticle){ + double x, y, z, fx, fy, fz, charge; + int index; + + int size; + removeWarning = fread(&size, sizeof(int), 1, file); + if(size != 60) printf("Error in loader ewal Size %d should be %d\n", size, 60); + + removeWarning = fread(&index, sizeof(int), 1, file); + removeWarning = fread(&charge, sizeof(double), 1, file); + + removeWarning = fread(&x, sizeof(double), 1, file); + removeWarning = fread(&y, sizeof(double), 1, file); + removeWarning = fread(&z, sizeof(double), 1, file); + + removeWarning = fread(&fx, sizeof(double), 1, file); + removeWarning = fread(&fy, sizeof(double), 1, file); + removeWarning = fread(&fz, sizeof(double), 1, file); + + removeWarning = fread(&size, sizeof(int), 1, file); + if(size != 60) printf("Error in loader ewal Size %d should be %d\n", size, 60); + + inParticle.setPosition(x,y,z); + inParticle.setForces(fx,fy,fz); + inParticle.setIndexInFile(index); + inParticle.setPhysicalValue(charge); + } + +}; + + #endif //FEwalLoader_HPP