changeFmaFormat.cpp 5.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * genarateDistributions.cpp
 *
 *  Created on: 23 mars 2014
 *      Author: Olivier Coulaud
 */


#include <iostream>
#include <fstream>
#include <string>
COULAUD Olivier's avatar
COULAUD Olivier committed
12 13
#include <cstdlib>
//
14 15 16
#include "Utils/FGlobal.hpp"
#include "Utils/FPoint.hpp"
#include "Files/FFmaGenericLoader.hpp"
17
#include "Files/FDlpolyLoader.hpp"
18 19 20
#include "Utils/FParameters.hpp"
#include "Utils/FGenerateDistribution.hpp"

21 22
#include "../Src/Utils/FParameterNames.hpp"

23
//
COULAUD Olivier's avatar
COULAUD Olivier committed
24
/// \file  changeFmaFormat.cpp
25 26 27
//!
//! \brief changeFormat: Driver to transform a FMA format and to build a visualization file
//!
COULAUD Olivier's avatar
COULAUD Olivier committed
28 29
//!  Driver to transform a FMA format and/or to build a visualization file<br>
//! For a description of the FMA format see FFmaGenericLoader<br>
30 31 32
//!  <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)
33
//!       \param  -fdlpoly name  file coming from a DLpoly simulation
34 35 36 37 38 39 40 41 42 43 44
//!
//!     \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
//!
45
//!   changeFormat -fin unitCubeXYZQ100.fma  -fout unitCubeXYZQ100 -bin
46 47 48


int main(int argc, char ** argv){
49 50 51 52 53 54
    FHelpDescribeAndExit(argc, argv,
                         "Driver to change the format of the input file. "
                         "fdlpoly is not supported for now.",
                         FParameterDefinitions::InputFile, FParameterDefinitions::OutputFile,
                         FParameterDefinitions::OutputVisuFile, FParameterDefinitions::OutputBinFormat);

55 56 57 58 59
    FSize NbPoints;
    FReal	 * particles = nullptr ;
    FFmaGenericLoader * loader;
    unsigned int nbData;

60
    const std::string filename(FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options,   "data.fma"));
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    loader = new FFmaGenericLoader(filename);
    //
    // Allocation
    //
    NbPoints                              = loader->getNumberOfParticles();
    nbData   = loader->getNbRecordPerline() ;
    const unsigned int arraySize =nbData*NbPoints;
    //
    particles = new FReal[arraySize] ;
    std::memset(particles,0,arraySize*sizeof(FReal));
    //
    // Read Data
    int j = 0 ;
    for(int idxPart = 0 ; idxPart < NbPoints ;++idxPart, j+=nbData){
        loader->fillParticle(&particles[j],nbData);
77
    }
78

79

80 81 82 83 84 85
    /////////////////////////////////////////////////////////////////////////
    //                                           Save data
    /////////////////////////////////////////////////////////////////////////
    //
    //  Generate file for ScalFMM FMAGenericLoader
    //
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    if(FParameters::existParameter(argc, argv, FParameterDefinitions::OutputFile.options)){
        std::string name(FParameters::getStr(argc,argv,FParameterDefinitions::OutputFile.options,   "output"));
        std::string ext(".");
        if(name.find(ext) !=std::string::npos) {
            std::cout << "No file with extension permitted for output name : " << name << std::endl;
            exit(-1);
        }
        if(  FParameters::existParameter(argc, argv, FParameterDefinitions::OutputBinFormat.options)){
            name += ".bfma";
        }
        else {
            name += ".fma";
        }
        FFmaGenericWriter writer(name) ;
        writer.writeHeader( loader->getCenterOfBox(), loader->getBoxWidth() , NbPoints, sizeof(FReal), nbData) ;
        writer.writeArrayOfReal(particles, nbData, NbPoints);
102 103 104 105
    }
    //
    //   Generate file for visualization purpose
    //
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    if(FParameters::existParameter(argc, argv, FParameterDefinitions::OutputVisuFile.options)){
        std::string outfilename(FParameters::getStr(argc,argv,FParameterDefinitions::OutputFile.options,   "output"));
        std::string visufile(""), fmt(FParameters::getStr(argc,argv,FParameterDefinitions::OutputVisuFile.options,   "vtp"));
        if( fmt == "vtp" ){
            visufile = outfilename + ".vtp" ;
        }
        else	if( fmt == "vtk" ){
            visufile = outfilename + ".vtk" ;
        }
        else if( fmt == "cosmo" ){
            if(nbData !=4) {
                std::cerr << "Cosmos export accept only 4 data per particles. here: "<<nbData<<std::endl;
                std::exit(EXIT_FAILURE);
            }
            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 ;
            if(nbData==4){
                exportVTKxml( file,  particles, NbPoints)   ;
            }
            else {
                exportVTKxml( file,  particles, NbPoints,nbData)   ;
            }
        }
        else		if( fmt == "vtk" ){
            std::cout << "Writes in VTK format  (visualization) in file "<< visufile <<std::endl ;
            exportVTK( file, particles, NbPoints,nbData)  ;
        }
        else if( fmt == "cosmo" ){
            std::cout << "Writes in COSMO format  (visualization) in file "<< visufile <<std::endl ;
            exportCOSMOS( file,  particles, NbPoints)   ;
        }
        else {
            std::cout << "Writes in CVS format  (visualization) in file "<<visufile<<std::endl ;
            exportCVS( file,  particles, NbPoints,nbData)  ;
        }
154 155 156 157 158 159 160
    }
    //
    delete particles ;

    //
    return 1;
}