#define MAX_NAMELEN 128 #include "gridpp.hh" #include "gridfun.hh" /**#define DUMP_RARR*/ namespace TSOpt { using RVL::OCDC; using RVL::ConstContainer; using RVL::STRING_PAIR; using RVL::RVLException; using RVL::ScalarFieldTraits; using RVL::DataContainer; using RVL::ContentPackage; using RVL::PackageContainer; using RVL::DataContainerFactory; using RVL::PackageContainerFactory; using RVL::getDataSize; using RVL::Writeable; using RVL::StdSpace; using RVL::LinearAlgebraPackage; using RVL::RVLLinearAlgebraPackage; using RVL::parse; void GridDC::open_p() const { /* first task: file registration, including creation of temp files */ char * name = NULL; // temporary file case: signalled by metadata filename // not initialized if (this->getFilename() == "") { // First, deal with header file #ifdef FRUITCAKE outfile<<"GridDC::open_p: open new temp hdr file \n"; outfile<<" proto = "<setFilename(sname); userfree_(name); #ifdef FRUITCAKE outfile<<" open_p: temp hdr file "<getFilename()<<" opened on file ptr "<getFilename()<< // " opened on file ptr "<getFilename().size()+1)*sizeof(char)); strcpy(name,this->getFilename().c_str()); // first try old-file open fph=iwave_fopen(&name,"r+",protohdr.c_str(),stderr); // new archival file - does not exist if (!fph) { #ifdef FRUITCAKE outfile<<"GridDC::open_p: open new archival hdr file \n"; outfile<<" proto = "<getFilename() <<" for write with prototype "<getFilename()<<" opened on file ptr "<getFilename()<<" opened on file ptr "<getFilename()<<" into PARARRAY\n"; throw e; } grid gtest; init_grid(>est,protog.dim,protog.gdim); par_grid(>est,*par,stderr); if (compare_grid(protog,gtest)) { RVLException e; e<<"Error: GridDC::open_p\n"; e<<"input RSF file "<getFilename()<<"incompatible with\n"; e<<"prototype grid defined in "<getFilename()<<" failed parse key=in\n"; throw e; } // done with partest ps_delete(&par); // open data file - remains open for lifetime of object, // other accesses to data file return same FILE* #ifdef FRUITCAKE outfile<<"GridDC::open_p: open existing archival file for data\n"; outfile<<" proto = "<getFilename()<<"\n"; throw e; } userfree_(name); #ifdef FRUITCAKE outfile<<" open_p: archival data file "< & GridDC::get(bool & more) { try { #ifdef FRUITCAKE outfile<<"GridDC::get (mutable)\n"; outfile<<" hdr = "<getFilename()<<" more = "<= panelnum) { RVLException e; e<<"Error: GridDC::get (mutable)\n"; e<<"reading file = "<getFilename()<<"\n"; e<<"panelindex = "<= panelnum) more=false; #ifdef FRUITCAKE outfile<<"GridDC::get (mutable) - finish\n"; outfile<<" hdr = "<getFilename()<<" more = "< const & GridDC::get(bool & more) const { try { #ifdef FRUITCAKE outfile<<"GridDC::get (const)\n"; outfile<<" hdr = "<getFilename()<<" more = "<= panelnum) { RVLException e; e<<"Error: GridDC::get (const) \n"; e<<"reading file = "<getFilename()<<"\n"; e<<"panelindex = "<= panelnum) more=false; #ifdef FRUITCAKE outfile<<"GridDC::get (const) - finish\n"; outfile<<" hdr = "<getFilename()<<" more = "<(_outfile), protohdr(_protohdr), protodata(_protodata), protog(_protog), data_format(_data_format), outfile(_outfile), fph(NULL), fp(NULL), panelindex(0), panelnum(1), rd(false) { // NB: no sanity tests here, compatibility presumed // inherited from DCF. #ifdef FRUITCAKE outfile<<"GridDC constructor:\n"; outfile<<"metadata = "<MAX_NAMELEN-10) { RVLException e; e<<"Error: GridDCF constructor\n"; e<<"proto header filename = "< "<0) scfac *= 10; if (scale<0) scfac *= 0.1; } // cerr<<"clean up\n"; delete [] buf; ps_delete(&par); } GridDCF::GridDCF(GridDCF const & f) : protohdr(f.protohdr), protodata(f.protodata), outfile(f.outfile), protog(f.protog), scfac(f.scfac), incore(f.incore), vol(f.vol) {} GridDCF::~GridDCF() {} PackageContainerFactory * GridDCF::clone() const { return new GridDCF(*this); } grid const & GridDCF::getGrid() const { return protog; } ireal GridDCF::getCellVol() const { return vol; } ireal GridDCF::getScaleFactor() const { return scfac; } string GridDCF::getFilename() const { return protohdr; } bool GridDCF::compare( DataContainerFactory const & dcf) const { GridDCF const * f = NULL; f = dynamic_cast< GridDCF const *>(&dcf); if ((f) && (this->protohdr == f->protohdr) && (this->incore == f->incore)) return true; return false; } // compatible if either DFC incore flag is unset (whether data is incore or not) // or DFC incore flag is set and DC data is incore for any reason (i.e. internal // grid has dim=gdim). bool GridDCF::isCompatible( DataContainer const & dc ) const { GridDC const * gc = NULL; gc = dynamic_cast(&dc); if (gc && (gc->getProtohdr()==this->protohdr)) if ((this->incore && gc->isInCore()) || !(this->incore)) return true; return false; } bool GridDCF::isIncore() const { return incore; } ostream & GridDCF::write(ostream & str) const { str<<"GridDCF: factory class for GridDC\n"; str<<"Prototype RSF filename = "<0) { fwrite(x,sizeof(char),n1*sizeof(ireal),fp); nt-=n1; } userfree_(name); userfree_(x); if (ftello(fp) != (off_t) (get_global_datasize_grid(g)*sizeof(ireal))) { RVLException e; e<<"Error: GridSpace constructor from grid \n"; e<<"failed to initialize data file\n"; throw e; } fseek(fp,0L,SEEK_SET); STRING_PAIR pr; pr.key=tag; pr.val=thdr; return pr; } GridSpace::GridSpace(string hdr, string dtype, bool incore, ostream & outfile) : StdSpace(), ConstContainer(STRING_PAIR(dtype,hdr)), f(hdr,incore,outfile), l(f.getCellVol()*f.getScaleFactor()*f.getScaleFactor()) { } GridSpace::GridSpace(grid const & g, std::string tag, std::string thdr, std::string fmt, bool incore, ostream & outfile) : StdSpace(), ConstContainer(getPTblEntry(g,tag,thdr,fmt)), f(thdr,incore,outfile), l(f.getCellVol()*f.getScaleFactor()*f.getScaleFactor()) {} GridSpace::GridSpace(GridSpace const & sp) : StdSpace(), ConstContainer(sp.get()), // f(sp.get().val,sp.outfile), f(sp.f), l(f.getCellVol()*f.getScaleFactor()*f.getScaleFactor()) {} GridSpace::~GridSpace() {} LinearAlgebraPackage const & GridSpace::getLAP() const { return l; } DataContainerFactory const & GridSpace::getDCF() const { return f; } grid const & GridSpace::getGrid() const { return f.getGrid(); } bool GridSpace::isIncore() const { return f.isIncore(); } ostream & GridSpace::write(ostream & str) const { str<<"GridSpace: StdSpace based on RSF grid data\n"; str<<" - data type = "<get().key<<"\n"; str<<" - header file = "<get().val<<"\n"; return str; } }