changeFmaFormat.cpp 6.69 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 21
#include "Utils/FParameters.hpp"
#include "Utils/FGenerateDistribution.hpp"

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


//
//
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
53
			<<     " Input:  only one option is allowed"  << std::endl
54
			<<     "   -fin name:  file name  to convert (with extension .fma (ascii) or bfma (binary) " <<std::endl
55
			<<     "   -fdlpoly name:  file name  to convert with extension (.bin if binary file) " <<std::endl
56 57 58 59 60 61 62 63 64 65 66 67 68
			<<     "     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);
	}
69 70 71 72
	FSize NbPoints;
	FReal	 * particles = nullptr ;
	if (FParameters::existParameter(argc, argv, "-fin")) {
		const std::string filename(FParameters::getStr(argc,argv,"-fin",   "data.fma"));
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		FFmaGenericLoader loader(filename);
		//
		// Allocation
		//
		 NbPoints                              = loader.getNumberOfParticles();
		const unsigned int 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);
			//		std::cout << "idxPart "<< idxPart << "  ";
			//		for (int jj= 0 ; jj<nbData ; ++jj, ++k){
			//			std::cout << particles[k] << "    ";
			//		}
			//		std::cout << std::endl;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
95
		}
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		else if(FParameters::existParameter(argc, argv, "-fdlpoly")){
			FDlpolyLoader  *loader = nullptr ;
//			if(FParameters::existParameter(argc, argv, "-bin")){
//				loader  = new FDlpolyBinLoader(filenameEwaldIn.c_str());
//			}
//			else {
//				loader  = new FDlpolyAsciiLoader(filenameEwaldIn.c_str());
//			}
			//				NbPoints = loader->getNumberOfParticles()  ;
			//				particles = new FReal[arraySize] ;
			//				std::memset(particles,0,arraySize*sizeof(FReal));
			//				for(int idxPart = 0 ; idxPart < NbPoints ; ++idxPart){
				//
			//					int index ;
			//				FPoint P ; FReal t[3];
///	/				loader->fillParticle(&P, t, &physicalValue,&index);
			//				particles[(index-1)*]
				//
//				totalCharge += physicalValue ;
				}
COULAUD Olivier's avatar
COULAUD Olivier committed
116 117
		}
		else {
118
			genDistusage() ;
COULAUD Olivier's avatar
COULAUD Olivier committed
119
		}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		FReal totalPhysical Value ;

		//
		/////////////////////////////////////////////////////////////////////////
		//                                           Save data
		/////////////////////////////////////////////////////////////////////////
		//
		//  Generate file for ScalFMM FMAGenericLoader
		//
		if(FParameters::existParameter(argc, argv, "-fout")){
			std::string name(FParameters::getStr(argc,argv,"-fout",   "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);
COULAUD Olivier's avatar
COULAUD Olivier committed
135
			}
136 137 138 139 140 141 142 143 144
			if(  FParameters::existParameter(argc, argv, "-bin")){
				name += ".bfma";
			}
			else {
				name += ".fma";
			}
			FFmaGenericWriter writer(name) ;
			writer.writeHeader( loader.getCenterOfBox(), loader.getBoxWidth() , NbPoints, sizeof(FReal), nbData) ;
			writer.writeArrayOfReal(particles, nbData, NbPoints);
145 146
		}
		//
147
		//   Generate file for visualization purpose
148
		//
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		if(FParameters::existParameter(argc, argv, "-visufmt")){
			std::string outfilename(FParameters::getStr(argc,argv,"-fout",   "output"));
			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" ){
				if(nbData !=4) {
					std::cerr << "Cosmos export accept only 4 data per particles. here: "<<nbData<<std::endl;
					std::exit(EXIT_FAILURE);
				}
				visufile = outfilename + ".cosmo" ;
COULAUD Olivier's avatar
COULAUD Olivier committed
164 165
			}
			else {
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
				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)  ;
COULAUD Olivier's avatar
COULAUD Olivier committed
196
			}
197
		}
198 199
		//
		delete particles ;
200

201 202 203
		//
		return 1;
	}