Commit 1500ae91 by Francois Gygi

Added write function to Function3d

parent 70ede943
......@@ -17,9 +17,12 @@
#include "Function3d.h"
#include "Function3dHandler.h"
#include "Base64Transcoder.h"
#include "qbox_xmlns.h"
#include "Timer.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <cstdio>
using namespace std;
......@@ -164,3 +167,41 @@ void Function3d::read(string filename)
<< nx << " " << ny << " " << nz << endl;
cout << "Function3d::read: val size: " << val.size() << endl;
}
////////////////////////////////////////////////////////////////////////////////
void Function3d::write(std::string filename) const
{
Base64Transcoder xcdr;
#if PLT_BIG_ENDIAN
xcdr.byteswap_double(val.size(),&val[0]);
#endif
// transform val to base64 encoding
int nbytes = val.size() * sizeof(double);
int nchars = xcdr.nchars(nbytes);
char *wbuf = new char[nchars];
xcdr.encode(nbytes, (byte *) &val[0], wbuf);
ofstream os(filename.c_str());
os <<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
<<"<fpmd:function3d xmlns:fpmd=\""
<< qbox_xmlns()
<< "\"\n"
<<" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
<<" xsi:schemaLocation=\""
<< qbox_xmlns() << " function3d.xsd\"\n"
<< " name=\"" << name << "\">"
<< endl;
os << "<domain a=\"" << a << "\"" << endl;
os << " b=\"" << b << "\"" << endl;
os << " c=\"" << c << "\"/>" << endl;
os << "<grid nx=\"" << nx << "\" ny=\"" << ny
<< "\" nz=\"" << nz << "\"/>" << endl;
os << "<grid_function type=\"double\" nx=\"" << nx
<< "\" ny=\"" << ny << "\" nz=\"" << nz
<< "\" encoding=\"base64\">" << endl;
xcdr.print(nchars,wbuf,os);
os << "</grid_function>" << endl;
os << "</fpmd:function3d>" << endl;
os.close();
delete [] wbuf;
}
......@@ -33,6 +33,6 @@ class Function3d
std::string name;
std::vector<double> val;
void read(std::string filename);
void write(std::string filename, std::string encoding) const;
void write(std::string filename) const;
};
#endif
......@@ -134,7 +134,7 @@ CXXFLAGS += -DTARGET='"$(TARGET)"'
test_fftw: test_fftw.o $(PLTOBJECTS)
$(LD) $(DFLAGS) -o $@ $^ $(LDFLAGS)
testFunction3d: testFunction3d.o Function3d.o Function3dHandler.o \
Base64Transcoder.o $(PLTOBJECTS)
Base64Transcoder.o qbox_xmlns.o $(PLTOBJECTS)
$(LD) $(DFLAGS) -o $@ $^ $(LDFLAGS)
#------------------------------------------------------------------------------
# generate dependencies in makefile: use -Y to avoid library header files
......@@ -404,7 +404,7 @@ ExternalPotential.o: Basis.h D3vector.h UnitCell.h ExternalPotential.h
ExternalPotential.o: Sample.h AtomSet.h Context.h blacs.h Atom.h D3tensor.h
ExternalPotential.o: blas.h ConstraintSet.h ExtForceSet.h Wavefunction.h
ExternalPotential.o: Control.h ChargeDensity.h Timer.h FourierTransform.h
ExternalPotential.o: Base64Transcoder.h
ExternalPotential.o: Function3d.h Base64Transcoder.h
ExternalPotential.o: Sample.h AtomSet.h Context.h blacs.h Atom.h D3vector.h
ExternalPotential.o: UnitCell.h D3tensor.h blas.h ConstraintSet.h
ExternalPotential.o: ExtForceSet.h Wavefunction.h Control.h ChargeDensity.h
......@@ -418,11 +418,11 @@ FoldInWsCmd.o: ExtForceSet.h Wavefunction.h Control.h
FourierTransform.o: FourierTransform.h Timer.h Basis.h D3vector.h UnitCell.h
FourierTransform.o: blas.h
FourierTransform.o: Timer.h
Function3d.o: Function3d.h D3vector.h Timer.h Function3dHandler.h StrX.h
Function3d.o: Base64Transcoder.h
Function3d.o: Function3d.h D3vector.h Function3dHandler.h StrX.h
Function3d.o: Base64Transcoder.h qbox_xmlns.h Timer.h
Function3d.o: D3vector.h
Function3dHandler.o: Function3d.h D3vector.h Function3dHandler.h StrX.h
Function3dHandler.o: Timer.h
Function3dHandler.o: Base64Transcoder.h Timer.h
Function3dHandler.o: Function3d.h D3vector.h StrX.h
GlobalExtForce.o: GlobalExtForce.h ExtForce.h D3vector.h AtomSet.h Context.h
GlobalExtForce.o: blacs.h Atom.h UnitCell.h D3tensor.h blas.h Species.h
......@@ -580,7 +580,7 @@ ResponseCmd.o: Basis.h Sample.h AtomSet.h Atom.h D3tensor.h blas.h
ResponseCmd.o: ConstraintSet.h ExtForceSet.h Control.h ChargeDensity.h
ResponseCmd.o: ExternalPotential.h FourierTransform.h ResponseCmd.h
ResponseCmd.o: UserInterface.h release.h isodate.h Species.h
ResponseCmd.o: Base64Transcoder.h
ResponseCmd.o: Base64Transcoder.h qbox_xmlns.h
ResponseCmd.o: UserInterface.h
RseedCmd.o: UserInterface.h Sample.h AtomSet.h Context.h blacs.h Atom.h
RseedCmd.o: D3vector.h UnitCell.h D3tensor.h blas.h ConstraintSet.h
......
......@@ -41,4 +41,13 @@ int main(int argc, char** argv)
sum += f.val[i]*f.val[i];
}
cout << "function norm2: " << sum / f.val.size() << endl;
// modify function
f.name = "output";
for ( int i = 0; i < f.val.size(); i++ )
{
f.val[i] *= 0.5;
}
f.write("output.xml");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment