testLoaderFMABinCreate.cpp 3.67 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// 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".
15
// ===================================================================================
16 17 18 19 20

#include <iostream>
#include <fstream>

#include <cstdio>
21
#include <cstdlib>
22 23
#include <time.h>

24 25
#include "../../Src/Utils/FGlobal.hpp"
#include "../../Src/Utils/FParameters.hpp"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

// This file can generate basic particles files in the FMA format

int main(int argc, char ** argv){
    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable can create a FMA particles files\n";
    std::cout << ">> You can pass a filename in parameter else the program will use\n";
    std::cout << ">> a default filename.\n";
    std::cout << ">> The format of the file is : \n";
    std::cout << ">> [number of particles] \n";
    std::cout << ">> [boxe width] [boxe x center] [boxe y center] [boxe z center]\n";
    std::cout << ">> [x] [y] [z] [physical value]...\n";
    //////////////////////////////////////////////////////////////

    // Nb of particles
41
    const FSize NbParticles = FParameters::getValue(argc,argv,"-nb", FSize(20000));
42
    const FReal physicalValue = FParameters::getValue(argc,argv,"-pv", FReal(0.1));
43 44 45 46 47 48 49 50
    // Center of the box
    const FReal XCenter = 0.5;
    const FReal YCenter = 0.5;
    const FReal ZCenter = 0.5;

    // Box width
    const FReal BoxWidth = 1.0/2;
    // Output file please let .temp extension
51 52 53 54
    const char* const defaultFilename = (sizeof(FReal) == sizeof(float))?
                                    "../../Data/test20k.bin.fma.single":
                                    "../../Data/test20k.bin.fma.double";
    const char* const Output = FParameters::getStr(argc,argv,"-f", defaultFilename);
55
    std::cout << "Creating : " << Output << "\n";
56 57 58 59 60 61 62 63 64 65 66 67 68 69

    // Create file
    FILE* const myfile = fopen(Output, "wb");
    if(!myfile){
        std::cout << "Cannot create " << Output << "\n";
        return 1;
    }

    std::cout << "Generating " << NbParticles << " in " << Output << "\n";
    std::cout << "Working...\n";

    // System properties
    const int sizeOfFreal = int(sizeof(FReal));

70 71
    fwrite(&sizeOfFreal, sizeof(int),   1, myfile);
    fwrite(&NbParticles, sizeof(FSize), 1, myfile);
72

73 74 75 76
    fwrite(&BoxWidth,   sizeof(FReal), 1, myfile);
    fwrite(&XCenter,    sizeof(FReal), 1, myfile);
    fwrite(&YCenter,    sizeof(FReal), 1, myfile);
    fwrite(&ZCenter,    sizeof(FReal), 1, myfile);
77

78 79 80
    const FReal Limite = FReal(0.00001);
    const FReal LimitedBoxWidth = BoxWidth - Limite * 2;

81
    FReal data[4];
82
    data[3] = physicalValue;
83
    // Generate particles
84
    for( FSize idx = 0 ; idx < NbParticles ; ++idx ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
85 86 87
        data[0] = ((FReal(drand48())) * LimitedBoxWidth * 2) + XCenter - BoxWidth + Limite;
        data[1] = ((FReal(drand48())) * LimitedBoxWidth * 2) + YCenter - BoxWidth + Limite;
        data[2] = ((FReal(drand48())) * LimitedBoxWidth * 2) + ZCenter - BoxWidth + Limite;
88 89 90 91 92 93 94 95 96 97 98 99

        fwrite(&data, sizeof(FReal), 4, myfile);
    }

    fclose(myfile);

    std::cout << "Done\n";

    return 0;
}


100