JDFTx  1.2.0
ScalarFieldIO.h
Go to the documentation of this file.
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 
25 
30 #include <core/ScalarField.h>
31 #include <core/vector3.h>
32 #include <core/Util.h>
33 
34 #define Tptr std::shared_ptr<T>
35 
37 template<typename T> void saveRawBinary(const Tptr& X, FILE* fp)
38 { int nWrote = fwriteLE(X->data(), sizeof(typename T::DataType), X->nElem, fp);
39  if(nWrote < X->nElem) die("Write failed after %d of %d records.\n", nWrote, X->nElem)
40 }
42 template<typename T> void saveRawBinary(const Tptr& X, const char* filename)
43 { FILE* fp = fopen(filename, "wb");
44  if(!fp) die("Could not open '%s' for writing.\n", filename)
45  saveRawBinary(X, fp);
46  fclose(fp);
47 }
48 
50 template<typename T> void loadRawBinary(Tptr& X, FILE* fp)
51 { int nRead = freadLE(X->data(), sizeof(typename T::DataType), X->nElem, fp);
52  if(nRead < X->nElem) die("Read failed after %d of %d records.\n", nRead, X->nElem)
53 }
55 template<typename T> void loadRawBinary(Tptr& X, const char* filename)
56 { FILE* fp = fopen(filename, "rb");
57  if(!fp) die("Could not open '%s' for reading.\n", filename)
58 
59  off_t fLen = fileSize(filename);
60  off_t expectedLen = sizeof(typename T::DataType) * X->nElem;
61  if(fLen != expectedLen)
62  { die("\nLength of '%s' was %ld instead of the expected %ld bytes.\n"
63  "Hint: Are you really reading the correct file?\n\n",
64  filename, (unsigned long)fLen, (unsigned long)expectedLen);
65  }
66 
67  loadRawBinary(X, fp);
68  fclose(fp);
69 }
70 
71 #undef Tptr
72 
76 void saveDX(const ScalarField&, const char* filenamePrefix);
77 
85 std::vector< std::vector<double> > sphericalize(const ScalarField* dataR, int nColumns, double drFac=1.0, vector3<>* center=0);
86 
94 void saveSphericalized(const ScalarField* dataR, int nColumns, const char* filename, double drFac=1.0, vector3<>* center=0);
95 
96 
103 void saveSphericalized(const ScalarFieldTilde* dataG, int nColumns, const char* filename, double dGFac=1.0);
104 
106 #endif // JDFTX_CORE_SCALARFIELDIO_H
void loadRawBinary(Tptr &X, FILE *fp)
Load the data in raw binary format from stream.
Definition: ScalarFieldIO.h:50
std::shared_ptr< ScalarFieldTildeData > ScalarFieldTilde
A smart reference-counting pointer to ScalarFieldTildeData.
Definition: ScalarField.h:45
off_t fileSize(const char *filename)
Get the size of a file.
size_t freadLE(void *ptr, size_t size, size_t nmemb, FILE *fp)
Read from a little-endian binary file, regardless of operating endianness.
Real and complex scalar fields in real and reciprocal space.
void saveSphericalized(const ScalarField *dataR, int nColumns, const char *filename, double drFac=1.0, vector3<> *center=0)
size_t fwriteLE(const void *ptr, size_t size, size_t nmemb, FILE *fp)
Write to a little-endian binary file, regardless of operating endianness.
void saveDX(const ScalarField &, const char *filenamePrefix)
void saveRawBinary(const Tptr &X, FILE *fp)
Save the data in raw binary format to stream.
Definition: ScalarFieldIO.h:37
#define die(...)
Quit with an error message (formatted using printf()). Must be called from all processes.
Definition: Util.h:114
Miscellaneous utilities.
std::shared_ptr< ScalarFieldData > ScalarField
A smart reference-counting pointer to ScalarFieldData.
Definition: ScalarField.h:41
std::vector< std::vector< double > > sphericalize(const ScalarField *dataR, int nColumns, double drFac=1.0, vector3<> *center=0)