1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx.  If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_SCALARFIELDIO_H
21 #define JDFTX_CORE_SCALARFIELDIO_H
22 
23 //! @addtogroup Output
24 //! @{
25 
26 //! @file ScalarFieldIO.h I/O utilities for the data arrays
27 
28 #include <core/ScalarField.h>
29 #include <core/vector3.h>
30 #include <core/Util.h>
31 
32 #define Tptr std::shared_ptr<T>
33 
34 //! Save the data in raw binary format to stream
saveRawBinary(const Tptr & X,FILE * fp)35 template<typename T> void saveRawBinary(const Tptr& X, FILE* fp)
36 {	int nWrote = fwriteLE(X->data(), sizeof(typename T::DataType), X->nElem, fp);
37 	if(nWrote < X->nElem) die("Write failed after %d of %d records.\n", nWrote, X->nElem)
38 }
39 //! Save the data in raw binary format to file
saveRawBinary(const Tptr & X,const char * filename)40 template<typename T> void saveRawBinary(const Tptr& X, const char* filename)
41 {	FILE* fp = fopen(filename, "wb");
42 	if(!fp) die("Could not open '%s' for writing.\n", filename)
43 	saveRawBinary(X, fp);
44 	fclose(fp);
45 }
46 
47 //! Load the data in raw binary format from stream
loadRawBinary(Tptr & X,FILE * fp)48 template<typename T> void loadRawBinary(Tptr& X, FILE* fp)
49 {	int nRead = freadLE(X->data(), sizeof(typename T::DataType), X->nElem, fp);
50 	if(nRead < X->nElem) die("Read failed after %d of %d records.\n", nRead, X->nElem)
51 }
52 //! Load the data in raw binary format from file
loadRawBinary(Tptr & X,const char * filename)53 template<typename T> void loadRawBinary(Tptr& X, const char* filename)
54 {	FILE* fp = fopen(filename, "rb");
55 	if(!fp) die("Could not open '%s' for reading.\n", filename)
56 
57 	off_t fLen = fileSize(filename);
58 	off_t expectedLen = sizeof(typename T::DataType) * X->nElem;
59 	if(fLen != expectedLen)
60 	{	die("\nLength of '%s' was %ld instead of the expected %ld bytes.\n"
61 				"Hint: Are you really reading the correct file?\n\n",
62 				filename, (unsigned long)fLen, (unsigned long)expectedLen);
63 	}
64 
65 	loadRawBinary(X, fp);
66 	fclose(fp);
67 }
68 
69 #undef Tptr
70 
71 /** Save data to a raw binary along with a DataExplorer header
72 @param filenamePrefix Binary data is saved to filenamePrefix.bin with DataExplorer header filenamePrefix.dx
73 */
74 void saveDX(const ScalarField&, const char* filenamePrefix);
75 
76 /** Spherically average scalar fields about an arbitrary center (with Wigner-Seitz wrapping)
77 @param dataR The data to sphericalize and save
78 @param nColumns Number of ScalarField's in dataR[]
79 @param drFac is the spacing in radius as a fraction of the diameter of the sample box (R ./ S) (drFac << 1 is likely to give noisy results, particularly close to r=0)
80 @param center The origin for spherical coordinates [default = center of box (if null pointer is passed)]
81 @return The first array contains the radial grid, and the subsequent ones the spherically-averaged results, one for each dataR, and the last column contains the weight of the radial grid point
82 */
83 std::vector< std::vector<double> > sphericalize(const ScalarField* dataR, int nColumns, double drFac=1.0, vector3<>* center=0);
84 
85 /** Saves an array of real space data pointers to a multicolumn 1D 'sphericalized' file (for gnuplot)
86 @param dataR The data to sphericalize and save
87 @param nColumns Number of ScalarField's in dataR[]
88 @param filename Output file in which column 1 will be the radius, column 2 to nColumns+1 would be the sphericalized versions of dataR[0 to nColumns-1]
89 @param drFac is the spacing in radius as a fraction of the diameter of the sample box (R ./ S) (drFac << 1 is likely to give noisy results, particularly close to r=0)
90 @param center The origin for spherical coordinates [default = center of box (if null pointer is passed)]
91 */
92 void saveSphericalized(const ScalarField* dataR, int nColumns, const char* filename, double drFac=1.0, vector3<>* center=0);
93 
94 
95 /** Saves an array of reciprocal space data pointers to a multicolumn 1D 'sphericalized' file (for gnuplot)
96 @param dataG The data to sphericalize (about G=0) and save
97 @param nColumns Number of ScalarFieldTilde's in dataG[]
98 @param filename Output file in which column 1 will be the radius, column 2 to nColumns+1 would be the sphericalized versions of dataG[0 to nColumns-1]
99 @param dGFac is the spacing in radius as a fraction of the diameter of the Brillouin zone (dGFac << 1 is likely to give noisy results, particularly close to G=0)
100 */
101 void saveSphericalized(const ScalarFieldTilde* dataG, int nColumns, const char* filename, double dGFac=1.0);
102 
103 //! @}
104 #endif // JDFTX_CORE_SCALARFIELDIO_H
105