Commit 5d7ba439 by Francois Gygi

Added Function3d class and its XML handler

parent d62b0ca7
//////////////////////////////////////////////////////////////////////////////// //
// Copyright (c) 2018 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// Function3d.C
//
////////////////////////////////////////////////////////////////////////////////
#include "Function3d.h"
#include "Timer.h"
#include "Function3dHandler.h"
#include "Base64Transcoder.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <sys/stat.h>
using namespace std;
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
using namespace xercesc;
////////////////////////////////////////////////////////////////////////////////
void Function3d::read(string filename)
{
Timer tm;
struct stat statbuf;
bool filestat = !stat(filename.c_str(),&statbuf);
if ( !filestat )
{
cout << "Function3d::read: could not stat file " << filename << endl;
return;
}
FILE* infile;
infile = fopen(filename.c_str(),"r");
if ( !infile )
{
cout << "Function3d::read: could not open file " << filename
<< " for reading" << endl;
return;
}
// determine size
off_t sz = statbuf.st_size;
// use contiguous read buffer, to be copied later to a string
char *rdbuf = new char[sz];
cout << "Function3d::read: file size: " << sz << endl;
tm.start();
size_t items_read = fread(rdbuf,sizeof(char),sz,infile);
assert(items_read==sz);
fclose(infile);
tm.stop();
cout << "Function3d::read: fread time: " << tm.real() << endl;
cout << "Function3d::read: read rate: "
<< sz/(tm.real()*1024*1024) << " MB/s" << endl;
string xmlcontent;
xmlcontent.assign(rdbuf,sz);
delete [] rdbuf;
tm.reset();
tm.start();
SAX2XMLReader::ValSchemes valScheme;
valScheme = SAX2XMLReader::Val_Never;
//alternate choices of validation schemes
//valScheme = SAX2XMLReader::Val_Always;
//valScheme = SAX2XMLReader::Val_Never;
bool doNamespaces = true;
bool doSchema = false;
bool schemaFullChecking = false;
bool namespacePrefixes = false;
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
cout << "Function3d::read: Error during XML initialization :\n"
<< StrX(toCatch.getMessage()) << endl;
return;
}
cout << "Function3d::read: xmlcontent.size(): " << xmlcontent.size() << endl;
MemBufInputSource* memBufIS = new MemBufInputSource
( (const XMLByte*) &xmlcontent[0], xmlcontent.size(), "buf_id", false);
// parse xmlcontent
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
if (valScheme == SAX2XMLReader::Val_Auto)
{
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgXercesDynamic, true);
}
if (valScheme == SAX2XMLReader::Val_Never)
{
parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
}
if (valScheme == SAX2XMLReader::Val_Always)
{
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgXercesDynamic, false);
}
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
parser->setFeature(XMLUni::fgXercesSchema, doSchema);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes);
Function3dHandler* f_handler = new Function3dHandler(*this);
try
{
parser->setContentHandler(f_handler);
parser->setErrorHandler(f_handler);
cout << "Function3d::read: Starting XML parsing" << endl;
parser->parse(*memBufIS);
cout << "Function3d::read: XML parsing done" << endl;
}
catch (const XMLException& toCatch)
{
cout << "\nAn error occurred\n Error: "
<< StrX(toCatch.getMessage())
<< "\n" << endl;
}
catch (const SAXParseException& e)
{
cout << "\na SAXParseException occurred in file "
<< StrX(e.getSystemId())
<< ", line " << e.getLineNumber()
<< ", char " << e.getColumnNumber()
<< "\n Message: " << StrX(e.getMessage()) << endl;
}
delete f_handler;
delete parser;
delete memBufIS;
XMLPlatformUtils::Terminate();
tm.stop();
cout << "Function3d::read: parse time: " << tm.real() << endl;
tm.reset();
tm.start();
val_.resize(gnx_*gny_*gnz_);
cout << "Function3d::read: grid size: "
<< gnx_ << " " << gny_ << " " << gnz_ << endl;
cout << "Function3d::read: str_ size: " << str_.size() << endl;
Base64Transcoder xcdr;
size_t nbytes = xcdr.decode(str_.size(),str_.data(),(byte*)&val_[0]);
tm.stop();
assert(nbytes==val_.size()*sizeof(double));
cout << "Function3d::read: base64 transcode time: " << tm.real() << endl;
str_.clear();
}
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2018 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// Function3d.h
//
////////////////////////////////////////////////////////////////////////////////
#ifndef FUNCTION3D_H
#define FUNCTION3D_H
#include "D3vector.h"
#include <vector>
#include <string>
#include <map>
class Function3d
{
public:
D3vector a_, b_, c_;
int gnx_, gny_, gnz_, nx_, ny_, nz_, x0_, y0_, z0_;
std::string str_;
std::string name_;
std::vector<double> val_;
void byteswap_double(size_t n, double* x);
const D3vector a(void) const { return a_; }
const D3vector b(void) const { return b_; }
const D3vector c(void) const { return c_; }
int nx(void) const { return nx_; }
int ny(void) const { return ny_; }
int nz(void) const { return nz_; }
int size(void) const { return val_.size(); }
std::vector<double>& val(void) { return val_; }
void read(std::string filename);
void write(std::string filename, std::string encoding) const;
};
#endif
//////////////////////////////////////////////////////////////////////////////// //
// Copyright (c) 2018 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// Function3dHandler.C
//
////////////////////////////////////////////////////////////////////////////////
#include "Function3d.h"
#include "Function3dHandler.h"
#include "StrX.h"
#include "Timer.h"
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/sax2/Attributes.hpp>
using namespace xercesc;
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
Function3dHandler::Function3dHandler(Function3d& f) : f_(f) {}
////////////////////////////////////////////////////////////////////////////////
Function3dHandler::~Function3dHandler(void) {}
////////////////////////////////////////////////////////////////////////////////
void Function3dHandler::startElement(const XMLCh* const uri,
const XMLCh* const localname, const XMLCh* const qname,
const Attributes& attributes)
{
string locname = StrX(localname).localForm();
if ( locname == "function3d" )
{
unsigned int len = attributes.getLength();
for ( unsigned int index = 0; index < len; index++ )
{
string attrname = StrX(attributes.getLocalName(index)).localForm();
if ( attrname == "name" )
{
f_.name_ = StrX(attributes.getValue(index)).localForm();
}
}
}
else if ( locname == "domain" )
{
unsigned int len = attributes.getLength();
for ( unsigned int index = 0; index < len; index++ )
{
string attrname = StrX(attributes.getLocalName(index)).localForm();
if ( attrname == "a" )
{
istringstream stst(StrX(attributes.getValue(index)).localForm());
stst >> f_.a_;
}
if ( attrname == "b" )
{
istringstream stst(StrX(attributes.getValue(index)).localForm());
stst >> f_.b_;
}
if ( attrname == "c" )
{
istringstream stst(StrX(attributes.getValue(index)).localForm());
stst >> f_.c_;
}
}
}
else if ( locname == "grid" )
{
unsigned int len = attributes.getLength();
for ( unsigned int index = 0; index < len; index++ )
{
string attrname = StrX(attributes.getLocalName(index)).localForm();
if ( attrname == "nx" )
{
f_.gnx_ = atoi(StrX(attributes.getValue(index)).localForm());
}
if ( attrname == "ny" )
{
f_.gny_ = atoi(StrX(attributes.getValue(index)).localForm());
}
if ( attrname == "nz" )
{
f_.gnz_ = atoi(StrX(attributes.getValue(index)).localForm());
}
}
}
else if ( locname == "grid_function" )
{
unsigned int len = attributes.getLength();
for ( unsigned int index = 0; index < len; index++ )
{
string attrname = StrX(attributes.getLocalName(index)).localForm();
if ( attrname == "type" )
{
string type = StrX(attributes.getValue(index)).localForm();
if ( type != "double" )
{
cerr << "Function3dHandler: grid_function type must be double"
<< endl;
assert(!"Function3dHandler: incorrect grid_function type");
}
}
if ( attrname == "nx" )
{
f_.nx_ = atoi(StrX(attributes.getValue(index)).localForm());
}
if ( attrname == "ny" )
{
f_.ny_ = atoi(StrX(attributes.getValue(index)).localForm());
}
if ( attrname == "nz" )
{
f_.nz_ = atoi(StrX(attributes.getValue(index)).localForm());
}
if ( attrname == "encoding" )
{
string type = StrX(attributes.getValue(index)).localForm();
if ( type != "base64" )
{
cerr << "Function3dHandler: encoding must be base64"
<< endl;
assert(!"Function3dHandler: incorrect encoding");
}
}
}
buf = "";
}
}
////////////////////////////////////////////////////////////////////////////////
#ifndef XERCES_VERSION_MAJOR
#error "XERCES_VERSION_MAJOR not defined"
#endif
#if XERCES_VERSION_MAJOR < 3
#error Xerces-C version should be at least 3
#endif
void Function3dHandler::characters(const XMLCh* const chars,
const XMLSize_t length)
{
#if TIMING
Timer tm;
tm.start();
#endif
char *str = XMLString::transcode(chars);
buf += str;
XMLString::release(&str);
#if TIMING
tm.stop();
cout << " Function3dHandler::characters: time: " << tm.real() << endl;
#endif
}
////////////////////////////////////////////////////////////////////////////////
void Function3dHandler::endElement(const XMLCh* const uri,
const XMLCh* const localname, const XMLCh* const qname)
{
string locname = StrX(localname).localForm();
if ( locname == "grid_function" )
{
f_.str_ = buf;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2018 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// Function3dHandler.h
//
////////////////////////////////////////////////////////////////////////////////
#ifndef FUNCTION3DHANDLER_H
#define FUNCTION3DHANDLER_H
#include "Function3d.h"
#include <xercesc/sax2/DefaultHandler.hpp>
#include "StrX.h"
#include <string>
class Function3dHandler: public DefaultHandler
{
public:
std::string buf;
Function3d& f_;
Function3dHandler(Function3d& f);
virtual ~Function3dHandler(void);
// -----------------------------------------------------------------------
// Implementations of the SAX DocumentHandler interface
// -----------------------------------------------------------------------
void startElement(const XMLCh* const uri,const XMLCh* const localname,
const XMLCh* const qname, const Attributes& attributes);
#ifndef XERCES_VERSION_MAJOR
#error "XERCES_VERSION_MAJOR not defined"
#endif
#if XERCES_VERSION_MAJOR > 2
void characters(const XMLCh* const chars, const XMLSize_t length);
#else
void characters(const XMLCh* const chars, const unsigned int length);
#endif
void endElement(const XMLCh* const uri, const XMLCh* const localname,
const XMLCh* const qname);
};
#endif
......@@ -17,7 +17,8 @@ include $(TARGET).mk
EXEC=qb
TESTEXECS=testMatrix testBasis \
testSlaterDet testEnergyFunctional testSample \
testChargeDensity testFourierTransform testSpecies testContext
testChargeDensity testFourierTransform testSpecies testContext \
testFunction3d
OBJECTS=qb.o AtomSet.o Atom.o Species.o \
Wavefunction.o SlaterDet.o \
......@@ -131,6 +132,9 @@ CXXFLAGS += -DTARGET='"$(TARGET)"'
$(LD) $(DFLAGS) -o $@ $^ $(LDFLAGS)
test_fftw: test_fftw.o $(PLTOBJECTS)
$(LD) $(DFLAGS) -o $@ $^ $(LDFLAGS)
testFunction3d: testFunction3d.o Function3d.o Function3dHandler.o \
Base64Transcoder.o $(PLTOBJECTS)
$(LD) $(DFLAGS) -o $@ $^ $(LDFLAGS)
#------------------------------------------------------------------------------
# generate dependencies in makefile: use -Y to avoid library header files
# that are likely to be different on other platforms.
......@@ -289,10 +293,10 @@ CellStepper.o: ExtForceSet.h Wavefunction.h Control.h
CellStepper.o: Sample.h AtomSet.h Context.h blacs.h Atom.h D3vector.h
CellStepper.o: UnitCell.h D3tensor.h blas.h ConstraintSet.h ExtForceSet.h
CellStepper.o: Wavefunction.h Control.h
ChargeDensity.o: ChargeDensity.h Timer.h Context.h blacs.h Basis.h D3vector.h
ChargeDensity.o: ChargeDensity.h Timer.h Context.h blacs.h D3vector.h Basis.h
ChargeDensity.o: UnitCell.h Wavefunction.h FourierTransform.h SlaterDet.h
ChargeDensity.o: Matrix.h
ChargeDensity.o: Timer.h Context.h blacs.h
ChargeDensity.o: Timer.h Context.h blacs.h D3vector.h
ChargeMixCoeff.o: Sample.h AtomSet.h Context.h blacs.h Atom.h D3vector.h
ChargeMixCoeff.o: UnitCell.h D3tensor.h blas.h ConstraintSet.h ExtForceSet.h
ChargeMixCoeff.o: Wavefunction.h Control.h
......@@ -399,6 +403,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: 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
......@@ -412,6 +417,12 @@ 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: D3vector.h
Function3dHandler.o: Function3d.h D3vector.h Function3dHandler.h StrX.h
Function3dHandler.o: 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
GlobalExtForce.o: ExtForce.h D3vector.h
......@@ -567,7 +578,8 @@ ResponseCmd.o: blacs.h D3vector.h Wavefunction.h UnitCell.h SlaterDet.h
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
ResponseCmd.o: UserInterface.h release.h isodate.h Species.h
ResponseCmd.o: Base64Transcoder.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
......@@ -786,6 +798,7 @@ testEnergyFunctional.o: ElectricEnthalpy.h Matrix.h SlaterDet.h Basis.h
testEnergyFunctional.o: Timer.h
testFourierTransform.o: Basis.h D3vector.h UnitCell.h FourierTransform.h
testFourierTransform.o: Timer.h
testFunction3d.o: Function3d.h D3vector.h
testLDAFunctional.o: LDAFunctional.h XCFunctional.h
testLineMinimizer.o: LineMinimizer.h
testMatrix.o: Timer.h Context.h blacs.h Matrix.h
......
#include<iostream>
#include "Function3d.h"
using namespace std;
int main()
{
Function3d f;
f.read("test.xml");
cout << "function name: " << f.name_ << endl;
cout << "function base64 size: " << f.str_.size() << endl;
cout << "function array size: " << f.val_.size() << endl;
double sum = 0.0;
for ( int i = 0; i < f.val_.size(); i++ )
{
sum += f.val_[i]*f.val_[i];
}
cout << "function norm2: " << sum / f.val_.size() << endl;
}
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