Commit 7e92296e authored by COULAUD Olivier's avatar COULAUD Olivier

Data/UTest/DirectDouble.bfma

parent ff60ca7b
/*
* genarateDistributions.cpp
*
* Created on: 23 mars 2014
* Author: Olivier Coulaud
*/
#include <iostream>
#include <fstream>
//#include <sstream>
#include <string>
#include "Utils/FGlobal.hpp"
#include "Utils/FPoint.hpp"
#include "Files/FFmaGenericLoader.hpp"
#include "Utils/FParameters.hpp"
#include "Utils/FGenerateDistribution.hpp"
//
/// \file changeFormat.cpp
//!
//! \brief changeFormat: Driver to transform a FMA format and to build a visualization file
//!
//!
//! <b> General arguments:</b>
//! \param -help (-h) to see the parameters available in this driver
//! \param -fin name: file name to convert (with extension .fma (ascii) or bfma (binary)
//!
//! \param -fout name: generic name for files (without extension) and save data
//! with following format in name.fma or name.bfma if -bin is set"
//! \param -bin save output in binary mode (name file name.bfma
//! \param -visufmt format for the visu file (vtk, vtp, cvs or cosmo). vtp is the default
//!
//!
//! \b examples
//!
//! Transform an ascii file in a binary file
//!
//! changeFormat -filename unitCubeXYZQ100.fma -outfilename unitCubeXYZQ100 -bin
//
//
void genDistusage() {
std::cout << "Driver to change the format of the input file"
<< std::endl;
std::cout << "Options "<< std::endl
<< " -help to see the parameters " << std::endl
<< " -fin name: file name to convert (with extension .fma (ascii) or bfma (binary) " <<std::endl
<< " Output " << std::endl
<< " -fout name: generic name for files (without extension) and save data" <<std::endl
<< " with following format in name.fma or name.bfma if -bin is set" <<std::endl
<< " -bin save output in binary mode (name file name.bfma)" <<std::endl
<< " -visufmt vtk, vtp, cosmo or cvs format for visualization " <<std::endl;
}
int main(int argc, char ** argv){
//
if(FParameters::existParameter(argc, argv, "-h")||FParameters::existParameter(argc, argv, "-help")|| (argc < 3 )){
genDistusage() ;
exit(-1);
}
const std::string filename(FParameters::getStr(argc,argv,"-fin", "data.fma"));
FFmaGenericLoader loader(filename);
//
// Allocation
//
long int NbPoints = loader.getNumberOfParticles();
const unsigned int nbData = loader.getNbRecordPerline() ;
const unsigned int arraySize =nbData*NbPoints;
FReal * particles ;
particles = new FReal[arraySize] ;
memset(particles,0,arraySize*sizeof(FReal));
//
// Read Data
int j = 0, k=0 ;
for(int idxPart = 0 ; idxPart < NbPoints ;++idxPart, j+=nbData){
// //
loader.fillParticle(&particles[j],nbData);
// std::cout << "idxPart "<< idxPart << " ";
// for (int jj= 0 ; jj<nbData ; ++jj, ++k){
// std::cout << particles[k] << " ";
// }
// std::cout << std::endl;
}
//
/////////////////////////////////////////////////////////////////////////
// Save data
/////////////////////////////////////////////////////////////////////////
//
// Generate file for ScalFMM Loader
//
bool binaryMode = false;
std::string outfilename(FParameters::getStr(argc,argv,"-fout", "output"));
if( FParameters::existParameter(argc, argv, "-bin")){
binaryMode = true;
outfilename += ".bfma";
}
else {
outfilename += ".fma";
}
FFmaGenericWriter writer(outfilename,binaryMode) ;
writer.writeHeader( loader.getCenterOfBox(), loader.getBoxWidth() , NbPoints, sizeof(FReal), nbData) ;
writer.writeArrayOfReal(particles, nbData, NbPoints);
//
//
//
if(FParameters::existParameter(argc, argv, "-visufmt")){
std::string visufile(""), fmt(FParameters::getStr(argc,argv,"-visufmt", "vtp"));
if( fmt == "vtp" ){
visufile = outfilename + ".vtp" ;
}
else if( fmt == "vtk" ){
visufile = outfilename + ".vtk" ;
}
else if( fmt == "cosmo" ){
visufile = outfilename + ".cosmo" ;
}
else {
visufile = outfilename + ".csv" ;
}
std::ofstream file( visufile, std::ofstream::out);
if(!file) {
std::cout << "Cannot open file."<< std::endl;
exit(-1) ;
} //
//
// Export data in cvs format
//
if( fmt == "vtp" ){
std::cout << "Writes in XML VTP format (visualization) in file "<< visufile <<std::endl ;
exportVTKxml( file, NbPoints, particles) ;
}
else if( fmt == "vtk" ){
std::cout << "Writes in VTK format (visualization) in file "<< visufile <<std::endl ;
exportVTK( file, NbPoints, particles) ;
}
else if( fmt == "cosmo" ){
std::cout << "Writes in COSMO format (visualization) in file "<< visufile <<std::endl ;
exportCOSMOS( file, NbPoints, particles) ;
}
else {
std::cout << "Writes in CVS format (visualization) in file "<<visufile<<std::endl ;
exportCVS( file, NbPoints, particles) ;
}
}
//
delete particles ;
//
return 1;
}
// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================
/**
*@author Cyrille Piacibello
*
* This exec will be used to store the result of direct computation in
* Binary Files. Then, a kernel (Chebyshev) is called on the same file, and
* results are compared.
*
* Format of result file :
* Each data is a FReal : posX,posY,posZ,physicalValue,forceX,forceY,forceZ,potential
*/
// ==== CMAKE =====
// // @FUSE_BLAS
// // ================
//
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "ScalFmmConfig.h"
#include "../../Src/Utils/FTic.hpp"
#include "../../Src/Utils/FParameters.hpp"
#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Components/FSimpleLeaf.hpp"
#include "../../Src/Kernels/P2P/FP2P.hpp"
#include "../../Src/Kernels/P2P/FP2PParticleContainerIndexed.hpp"
#include "../../Src/Files/FFmaGenericLoader.hpp"
#include "../../Src/Files/FFmaBinLoaderResult.hpp"
#include "../../Src/Utils/FTic.hpp"
#include "../../Src/Kernels/Chebyshev/FChebCell.hpp"
#include "../../Src/Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "../../Src/Kernels/Chebyshev/FChebKernel.hpp"
#include "../../Src/Kernels/Chebyshev/FChebSymKernel.hpp"
#include "../../Src/Core/FFmmAlgorithm.hpp"
int main(int argc, char* argv[])
{
// get info from commandline
const char* const defaultFilename = (sizeof(FReal) == sizeof(float))?
"../Data/test20k.bin.fma.single":
"../Data/test20k.bin.fma.double";
const char* const filename = FParameters::getStr(argc,argv,"-f", defaultFilename);
const char* const fileresult = FParameters::getStr(argc,argv,"-fr","../Data/test20k.bin.fma.double.result");;
//For Fmm Computation
const unsigned int TreeHeight = FParameters::getValue(argc, argv, "-h", 5);
const unsigned int SubTreeHeight = FParameters::getValue(argc, argv, "-sh", 2);
FTic time;
struct TestParticle{
FPoint position;
FReal forces[3];
FReal physicalValue;
FReal potential;
};
printf("Input is %s, \n Results will be stored in %s \n",filename,fileresult);
{
//First Part
//Direct Computation and Storage of result
//open particle file
FFmaGenericLoader loader(filename,true);
if(!loader.isOpen()){
std::cout << "Loader Error, " << filename << " is missing\n";
return 1;
}
time.tic();
TestParticle* const particles = new TestParticle[loader.getNumberOfParticles()];
for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
FPoint position;
FReal physicalValue = 0.0;
loader.fillParticle(&position,&physicalValue);
// get copy
particles[idxPart].position = position;
particles[idxPart].physicalValue = physicalValue;
particles[idxPart].potential = 0.0;
particles[idxPart].forces[0] = 0.0;
particles[idxPart].forces[1] = 0.0;
particles[idxPart].forces[2] = 0.0;
}
time.tac();
printf("Elapsed Time for Loading %lld particles from File: \t %f\n",loader.getNumberOfParticles(),time.elapsed());
//Direct Computation
time.tic();
for(int idxTarget = 0 ; idxTarget < loader.getNumberOfParticles() ; ++idxTarget){
for(int idxOther = idxTarget + 1 ; idxOther < loader.getNumberOfParticles() ; ++idxOther){
FP2P::MutualParticles(particles[idxTarget].position.getX(), particles[idxTarget].position.getY(),
particles[idxTarget].position.getZ(),particles[idxTarget].physicalValue,
&particles[idxTarget].forces[0],&particles[idxTarget].forces[1],
&particles[idxTarget].forces[2],&particles[idxTarget].potential,
particles[idxOther].position.getX(), particles[idxOther].position.getY(),
particles[idxOther].position.getZ(),particles[idxOther].physicalValue,
&particles[idxOther].forces[0],&particles[idxOther].forces[1],
&particles[idxOther].forces[2],&particles[idxOther].potential);
}
}
time.tac();
printf("Elapsed Time for Direct Computation: \t %f\n",time.elapsed());
//Write results in output file
FILE * fd = fopen(fileresult,"w");
//Size of elements to be written
int realSize = sizeof(FReal);
FSize nbPart = loader.getNumberOfParticles();
FReal boxWidth = loader.getBoxWidth() / 2;
FReal centerX = loader.getCenterOfBox().getX();
FReal centerY = loader.getCenterOfBox().getY();
FReal centerZ = loader.getCenterOfBox().getZ();
fwrite(&realSize, sizeof(int), 1, fd);
fwrite(&nbPart, sizeof(FSize), 1, fd);
fwrite(&boxWidth, sizeof(FReal), 1, fd);
fwrite(&centerX, sizeof(FReal), 1, fd);
fwrite(&centerY, sizeof(FReal), 1, fd);
fwrite(&centerZ, sizeof(FReal), 1, fd);
for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart)
{
fwrite( particles[idxPart].position.getDataValue(),realSize,3,fd);
fwrite(&particles[idxPart].physicalValue, realSize,1,fd);
fwrite( particles[idxPart].forces, realSize,3,fd);
fwrite(&particles[idxPart].potential, realSize,1,fd);
}
delete [] particles;
fclose(fd);
}
//Second Part
{
//Fmm Computation and comparison to Stored results
// begin Chebyshev kernel
// accuracy
const unsigned int ORDER = 7;
// typedefs
typedef FP2PParticleContainerIndexed<> ContainerClass;
typedef FSimpleLeaf< ContainerClass > LeafClass;
typedef FInterpMatrixKernelR MatrixKernelClass;
typedef FChebCell<ORDER> CellClass;
typedef FOctree<CellClass,ContainerClass,LeafClass> OctreeClass;
typedef FChebSymKernel<CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
time.tic();
//Open result file to fill octree for Chebyshev kernel AND compare results
FFmaBinLoaderResult loader2(fileresult);
// init oct-tree
OctreeClass tree(TreeHeight, SubTreeHeight, loader2.getBoxWidth(), loader2.getCenterOfBox());
TestParticle* const particles2 = new TestParticle[loader2.getNumberOfParticles()];
for(int idxPart = 0 ; idxPart < loader2.getNumberOfParticles() ; ++idxPart)
{
loader2.fillParticle(&particles2[idxPart].position,
&particles2[idxPart].physicalValue,
&particles2[idxPart].forces[0],
&particles2[idxPart].forces[1],
&particles2[idxPart].forces[2],
&particles2[idxPart].potential);
tree.insert(FPoint(particles2[idxPart].position),idxPart,particles2[idxPart].physicalValue);
}
time.tac();
printf("Elapsed Time for Reading File: \t %f\n",time.elapsed());
time.tic();
KernelClass kernels(TreeHeight, loader2.getBoxWidth(), loader2.getCenterOfBox());
FmmClass algorithm(&tree, &kernels);
algorithm.execute();
time.tac();
printf("Elapsed Time for Fmm Computation: \t %f\n",time.elapsed());
FMath::FAccurater potentialDiff;
FMath::FAccurater fx, fy, fz;
//Compare the kernel to the stored elements
{
tree.forEachLeaf([&](LeafClass* leaf){
const FReal*const potentials = leaf->getTargets()->getPotentials();
const FReal*const forcesX = leaf->getTargets()->getForcesX();
const FReal*const forcesY = leaf->getTargets()->getForcesY();
const FReal*const forcesZ = leaf->getTargets()->getForcesZ();
const int nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
const FVector<int>& indexes = leaf->getTargets()->getIndexes();
for(int idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
const int indexPartOrig = indexes[idxPart];
potentialDiff.add(particles2[indexPartOrig].potential,potentials[idxPart]);
fx.add(particles2[indexPartOrig].forces[0],forcesX[idxPart]);
fy.add(particles2[indexPartOrig].forces[1],forcesY[idxPart]);
fz.add(particles2[indexPartOrig].forces[2],forcesZ[idxPart]);
}
});
// Print for information
std::cout << "Potential " << potentialDiff << std::endl;
std::cout << "Fx " << fx << std::endl;
std::cout << "Fy " << fy << std::endl;
std::cout << "Fz " << fz << std::endl;
}
}
return 0;
}
// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
// ===================================================================================
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include "../../Src/Utils/FParameters.hpp"
#include "../../Src/Utils/FTic.hpp"
#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Containers/FVector.hpp"
#include "../../Src/Utils/FAssert.hpp"
#include "../../Src/Utils/FPoint.hpp"
#include "../../Src/Components/FBasicCell.hpp"
#include "../../Src/Components/FSimpleLeaf.hpp"
#include "../../Src/Files/FFmaBinLoaderResult.hpp"
#include "../../Src/Components/FBasicParticleContainer.hpp"
/**
* In this file we show an example of FBasicLoader use
* Inserting 2000000 particles ...
* Done (5.77996).
* Deleting particles ...
* Done (0.171918).
*/
int main(int argc, char ** argv ){
typedef FBasicParticleContainer<1> ContainerClass;
typedef FSimpleLeaf< ContainerClass > LeafClass;
typedef FOctree< FBasicCell, ContainerClass , LeafClass > OctreeClass;
///////////////////////What we do/////////////////////////////
std::cout << ">> This executable is useless to execute.\n";
std::cout << ">> It is only interesting to wath the code to understand\n";
std::cout << ">> how to use the FMA loader\n";
//////////////////////////////////////////////////////////////
// Use testLoaderFMABinCreate.exe to create this file
FTic counter;
const char* const filename = FParameters::getStr(argc,argv,"-f", "../Data/test20k.bin.fma.double.res");
std::cout << "Opening : " << filename << "\n";
// open basic result particles loader
FFmaBinLoaderResult loader(filename);
if(!loader.isOpen()){
std::cout << "Loader Error, " << filename << "is missing\n";
return 1;
}
{
// octree
OctreeClass tree(FParameters::getValue(argc,argv,"-h", 5), FParameters::getValue(argc,argv,"-sh", 3),
loader.getBoxWidth(), loader.getCenterOfBox());
// -----------------------------------------------------
std::cout << "Inserting " << loader.getNumberOfParticles() << " particles ..." << std::endl;
counter.tic();
FPoint particlePosition;
FReal physicalValue = 0.0;
FReal forces[3];
FReal potential;
for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
loader.fillParticle(&particlePosition,&physicalValue,forces,&forces[1],&forces[2],&potential);
tree.insert(particlePosition,physicalValue);
}
counter.tac();
std::cout << "Done " << "(" << counter.elapsed() << ")." << std::endl;
// -----------------------------------------------------
std::cout << "Deleting particles ..." << std::endl;
counter.tic();
}
counter.tac();
std::cout << "Done " << "(" << counter.elapsed() << ")." << std::endl;
// -----------------------------------------------------
return 0;
}
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