mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-11 14:40:46 +01:00
Utilities for reading and writing "pair" objects.
This commit is contained in:
parent
f510002a62
commit
b7cd1a19e3
@ -36,22 +36,6 @@ See the full license in the file "LICENSE" in the top level distribution directo
|
|||||||
#include <Grid/Hadrons/Module.hpp>
|
#include <Grid/Hadrons/Module.hpp>
|
||||||
#include <Grid/Hadrons/ModuleFactory.hpp>
|
#include <Grid/Hadrons/ModuleFactory.hpp>
|
||||||
|
|
||||||
namespace Grid {
|
|
||||||
// Overload >> to extract gamma pair from "<g1 g2>" string.
|
|
||||||
template <typename T1, typename T2>
|
|
||||||
inline std::istringstream &operator>>(std::istringstream &sstr,
|
|
||||||
std::pair<T1, T2> &buf)
|
|
||||||
{
|
|
||||||
unsigned int buf1;
|
|
||||||
unsigned int buf2;
|
|
||||||
char c;
|
|
||||||
sstr >> c >> buf1 >> buf2 >> c;
|
|
||||||
sstr.peek();
|
|
||||||
buf = std::make_pair((T1)buf1, (T2)buf2);
|
|
||||||
return sstr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BEGIN_HADRONS_NAMESPACE
|
BEGIN_HADRONS_NAMESPACE
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -63,8 +47,8 @@ BEGIN_HADRONS_NAMESPACE
|
|||||||
- q1: input propagator 1 (string)
|
- q1: input propagator 1 (string)
|
||||||
- q2: input propagator 2 (string)
|
- q2: input propagator 2 (string)
|
||||||
- gammas: gamma products to insert at sink & source, pairs of gamma matrices
|
- gammas: gamma products to insert at sink & source, pairs of gamma matrices
|
||||||
(space-separated integers) in square brackets (i.e. [g_sink g_src]),
|
(space-separated strings) in angled brackets (i.e. <g_sink g_src>),
|
||||||
in a sequence (e.g. "[15 7][7 15][7 7]").
|
in a sequence (e.g. "<Gamma5 Gamma5><Gamma5 GammaT>").
|
||||||
|
|
||||||
Special values: "all" - perform all possible contractions.
|
Special values: "all" - perform all possible contractions.
|
||||||
- mom: momentum insertion, space-separated float sequence (e.g ".1 .2 1. 0."),
|
- mom: momentum insertion, space-separated float sequence (e.g ".1 .2 1. 0."),
|
||||||
|
@ -138,6 +138,54 @@ namespace Grid {
|
|||||||
unsigned int dimInd_{0};
|
unsigned int dimInd_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Pair IO utilities /////////////////////////////////////////////////////////
|
||||||
|
// helper function to parse input in the format "<obj1 obj2>"
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
inline std::istream & operator>>(std::istream &is, std::pair<T1, T2> &buf)
|
||||||
|
{
|
||||||
|
T1 buf1;
|
||||||
|
T2 buf2;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
// Search for "pair" delimiters.
|
||||||
|
do
|
||||||
|
{
|
||||||
|
is.get(c);
|
||||||
|
} while (c != '<' && !is.eof());
|
||||||
|
if (c == '<')
|
||||||
|
{
|
||||||
|
int start = is.tellg();
|
||||||
|
do
|
||||||
|
{
|
||||||
|
is.get(c);
|
||||||
|
} while (c != '>' && !is.eof());
|
||||||
|
if (c == '>')
|
||||||
|
{
|
||||||
|
int end = is.tellg();
|
||||||
|
int psize = end - start - 1;
|
||||||
|
|
||||||
|
// Only read data between pair limiters.
|
||||||
|
is.seekg(start);
|
||||||
|
std::string tmpstr(psize, ' ');
|
||||||
|
is.read(&tmpstr[0], psize);
|
||||||
|
std::istringstream temp(tmpstr);
|
||||||
|
temp >> buf1 >> buf2;
|
||||||
|
buf = std::make_pair(buf1, buf2);
|
||||||
|
is.seekg(end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is.peek();
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
// output to streams for pairs
|
||||||
|
template <class T1, class T2>
|
||||||
|
inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p)
|
||||||
|
{
|
||||||
|
os << "<" << p.first << " " << p.second << ">";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
// Abstract writer/reader classes ////////////////////////////////////////////
|
// Abstract writer/reader classes ////////////////////////////////////////////
|
||||||
// static polymorphism implemented using CRTP idiom
|
// static polymorphism implemented using CRTP idiom
|
||||||
class Serializable;
|
class Serializable;
|
||||||
|
@ -113,6 +113,7 @@ int main(int argc,char **argv)
|
|||||||
// test serializable class writing
|
// test serializable class writing
|
||||||
myclass obj(1234); // non-trivial constructor
|
myclass obj(1234); // non-trivial constructor
|
||||||
std::vector<myclass> vec;
|
std::vector<myclass> vec;
|
||||||
|
std::pair<myenum, myenum> pair;
|
||||||
|
|
||||||
std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl;
|
std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl;
|
||||||
write(WR,"obj",obj);
|
write(WR,"obj",obj);
|
||||||
@ -120,6 +121,8 @@ int main(int argc,char **argv)
|
|||||||
vec.push_back(myclass(1234));
|
vec.push_back(myclass(1234));
|
||||||
vec.push_back(myclass(5678));
|
vec.push_back(myclass(5678));
|
||||||
vec.push_back(myclass(3838));
|
vec.push_back(myclass(3838));
|
||||||
|
pair = std::make_pair(myenum::red, myenum::blue);
|
||||||
|
|
||||||
write(WR, "objvec", vec);
|
write(WR, "objvec", vec);
|
||||||
std::cout << "-- serialisable class writing to std::cout:" << std::endl;
|
std::cout << "-- serialisable class writing to std::cout:" << std::endl;
|
||||||
std::cout << obj << std::endl;
|
std::cout << obj << std::endl;
|
||||||
@ -127,21 +130,30 @@ int main(int argc,char **argv)
|
|||||||
std::cout << "vec[0] == obj: " << ((vec[0] == obj) ? "true" : "false") << std::endl;
|
std::cout << "vec[0] == obj: " << ((vec[0] == obj) ? "true" : "false") << std::endl;
|
||||||
std::cout << "vec[1] == obj: " << ((vec[1] == obj) ? "true" : "false") << std::endl;
|
std::cout << "vec[1] == obj: " << ((vec[1] == obj) ? "true" : "false") << std::endl;
|
||||||
|
|
||||||
|
write(WR, "objpair", pair);
|
||||||
|
std::cout << "-- pair writing to std::cout:" << std::endl;
|
||||||
|
std::cout << pair << std::endl;
|
||||||
|
|
||||||
// read tests
|
// read tests
|
||||||
std::cout << "\n==== IO self-consistency tests" << std::endl;
|
std::cout << "\n==== IO self-consistency tests" << std::endl;
|
||||||
//// XML
|
//// XML
|
||||||
ioTest<XmlWriter, XmlReader>("iotest.xml", obj, "XML (object) ");
|
ioTest<XmlWriter, XmlReader>("iotest.xml", obj, "XML (object) ");
|
||||||
ioTest<XmlWriter, XmlReader>("iotest.xml", vec, "XML (vector of objects)");
|
ioTest<XmlWriter, XmlReader>("iotest.xml", vec, "XML (vector of objects)");
|
||||||
|
ioTest<XmlWriter, XmlReader>("iotest.xml", pair, "XML (pair of objects)");
|
||||||
//// binary
|
//// binary
|
||||||
ioTest<BinaryWriter, BinaryReader>("iotest.bin", obj, "binary (object) ");
|
ioTest<BinaryWriter, BinaryReader>("iotest.bin", obj, "binary (object) ");
|
||||||
ioTest<BinaryWriter, BinaryReader>("iotest.bin", vec, "binary (vector of objects)");
|
ioTest<BinaryWriter, BinaryReader>("iotest.bin", vec, "binary (vector of objects)");
|
||||||
|
ioTest<BinaryWriter, BinaryReader>("iotest.bin", pair, "binary (pair of objects)");
|
||||||
//// text
|
//// text
|
||||||
ioTest<TextWriter, TextReader>("iotest.dat", obj, "text (object) ");
|
ioTest<TextWriter, TextReader>("iotest.dat", obj, "text (object) ");
|
||||||
ioTest<TextWriter, TextReader>("iotest.dat", vec, "text (vector of objects)");
|
ioTest<TextWriter, TextReader>("iotest.dat", vec, "text (vector of objects)");
|
||||||
|
ioTest<TextWriter, TextReader>("iotest.dat", pair, "text (pair of objects)");
|
||||||
//// HDF5
|
//// HDF5
|
||||||
|
#undef HAVE_HDF5
|
||||||
#ifdef HAVE_HDF5
|
#ifdef HAVE_HDF5
|
||||||
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", obj, "HDF5 (object) ");
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", obj, "HDF5 (object) ");
|
||||||
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", vec, "HDF5 (vector of objects)");
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", vec, "HDF5 (vector of objects)");
|
||||||
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", pair, "HDF5 (pair of objects)");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::cout << "\n==== vector flattening/reconstruction" << std::endl;
|
std::cout << "\n==== vector flattening/reconstruction" << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user