1 /* 2 * PlotStream.hpp 3 * 4 * Created by Frederic Hecht on 27/10/08. 5 * Copyright 2008 UPMC. 6 * 7 */ 8 9 #include <cstdio> 10 #ifdef _WIN32 11 #include <fcntl.h> 12 #endif 13 #include "endian.hpp" 14 15 #ifndef PLOT_STREAM_HPP_ 16 #define PLOT_STREAM_HPP_ 17 18 //FFCS visualization stream redirection 19 #include "ffapi.hpp" 20 21 using Fem2D::Mesh; 22 using Fem2D::Mesh3; 23 namespace Fem2D { 24 25 } 26 27 28 class PlotStream 29 { 30 public: 31 32 FILE * TheStream; 33 PlotStream(FILE * Stream)34 PlotStream(FILE *Stream) :TheStream(Stream) { } operator bool() const35 operator bool() const { return TheStream;} 36 // datatype mush be < 0 to have no collistion with arg number. 37 // FFCS: <<PlotStream::datatype>> 38 enum datatype { dt_meshes=-1,dt_plots=-2,dt_endplot=-3,dt_meshes3=-10,dt_endarg=99999,dt_newplot=-5, dt_meshesS=-12, dt_meshesL=-13}; // modif order 39 40 // dt_plots3=-11,dt_plotsS=-13,dt_plots3S=-14 41 //FFCS:need to send control data to FFCS at least once per plot SendNewPlot()42 void SendNewPlot() { ffapi::newplot();write((long )dt_newplot); set_binary_mode(); } SendEndArgPlot()43 void SendEndArgPlot() {write((long )dt_endarg); } 44 //FFCS:redirect visualization stream SendEndPlot()45 void SendEndPlot() { write((long )dt_endplot);ffapi::ff_fflush(TheStream); set_text_mode() ;} SendPlots()46 void SendPlots() { write((long )dt_plots); } SendMeshes()47 void SendMeshes() { write((long )dt_meshes);} SendMeshes3()48 void SendMeshes3() { write((long )dt_meshes3);} SendMeshesS()49 void SendMeshesS() { write((long )dt_meshesS);} SendMeshesL()50 void SendMeshesL() { write((long )dt_meshesL);} 51 //FFCS: divert stream to FFCS write(const void * data,size_t l)52 void write(const void *data,size_t l) {ffapi::ff_fwrite(data,1,l,TheStream);} 53 write(const bool & bb)54 PlotStream& write(const bool& bb) {bool b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(bool));return *this;} write(const long long & bb)55 PlotStream& write(const long long& bb) {long long b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(long long));return *this;} write(const long & bb)56 PlotStream& write(const long& bb) { // always write 8 bits for a long FH. 57 long long ll=bb;ll=w_endian(ll);write(reinterpret_cast<const void *> (&ll),sizeof(long long)); 58 return *this;} write(const int & bb)59 PlotStream& write(const int& bb) {int b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(int));return *this;} write(const double & bb)60 PlotStream& write(const double& bb) {double b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(double));return *this;} write(const complex<double> & bb)61 PlotStream& write(const complex<double>& bb) {return write(bb.real()),write(bb.imag());} write(const Fem2D::R1 & P)62 PlotStream &write(const Fem2D::R1 & P) { return write(P.x);} write(const Fem2D::R2 & P)63 PlotStream &write(const Fem2D::R2 & P) { return write(P.x),write(P.y);} write(const Fem2D::R3 & P)64 PlotStream &write(const Fem2D::R3 & P) { return write(P.x),write(P.y),write(P.z);} 65 write(const string & b)66 PlotStream& write(const string& b) { 67 int l=b.size(); 68 write(l); 69 write(b.data(),l); 70 return *this; 71 } 72 set_text_mode()73 void set_text_mode() 74 { 75 //FFCS:visualization stream redirection 76 ffapi::wintextmode(TheStream); 77 } set_binary_mode()78 void set_binary_mode() 79 { 80 //FFCS:visualization stream redirection 81 ffapi::winbinmode(TheStream); 82 } 83 operator <<(const bool & b)84 PlotStream & operator << (const bool& b) { return write(b); } operator <<(const long & b)85 PlotStream & operator << (const long& b) { return write(b); } operator <<(const long long & b)86 PlotStream & operator << (const long long & b) { return write(b); } operator <<(const int & b)87 PlotStream & operator << (const int& b) { return write(b); } operator <<(const double & b)88 PlotStream & operator << (const double& b) { return write(b); } operator <<(const complex<double> & b)89 PlotStream & operator << (const complex<double>& b) { return write(b); } operator <<(const string & s)90 PlotStream & operator << (const string& s) { return write(s); } operator <<(const string * s)91 PlotStream & operator << (const string* s) { ffassert(s); return write(*s); } 92 template<class T> operator <<(const KN_<T> & b)93 PlotStream & operator << (const KN_<T>& b) 94 { 95 long n=b.N(); 96 write(n); 97 // cout << "PlotStream :<< n " << n << endl; 98 99 for (int i=0;i<n;++i) 100 write(b[i]); 101 return *this; 102 } 103 104 operator <<(const Mesh & Th)105 PlotStream & operator << (const Mesh& Th) { 106 /* 107 Serialize s=Th.serialize(); 108 long n=s.size(); 109 write( n ); 110 write(s,s.size());*/ 111 GSave2(TheStream , Th) ; 112 return *this;} 113 operator <<(const Fem2D::Mesh2 & Th)114 PlotStream & operator << (const Fem2D::Mesh2& Th) { Th.GSave(TheStream); return *this;} operator <<(const Fem2D::Mesh3 & Th)115 PlotStream & operator << (const Fem2D::Mesh3& Th) { Th.GSave(TheStream); return *this;} operator <<(const Fem2D::MeshS & Th)116 PlotStream & operator << (const Fem2D::MeshS& Th) { Th.GSave(TheStream); return *this;} operator <<(const Fem2D::MeshL & Th)117 PlotStream & operator << (const Fem2D::MeshL& Th) { Th.GSave(TheStream); return *this;} 118 read(void * data,size_t l)119 void read( void *data,size_t l) { 120 char * p= (char*)data; 121 for(int i=0;i<l;++i) 122 *p++ = (char) getc(TheStream); 123 // fread( p++,1,1,TheStream); 124 // read(data,l); 125 } 126 //FFCS:visualization stream redirection good() const127 bool good() const {return ffapi::ff_ferror(TheStream)==0;} GetNewPlot()128 void GetNewPlot() { get(dt_newplot) ; set_binary_mode();} GetEndArgPlot()129 void GetEndArgPlot() {get(dt_endarg); } GetEndPlot()130 void GetEndPlot() {get(dt_endplot); set_text_mode();} GetPlots()131 void GetPlots() { get(dt_plots); } GetMeshes()132 void GetMeshes() { get(dt_meshes);} 133 //void GetMeshesS() { get(dt_meshesS);} 134 /* bool GetMeshes3() { long tt; read(tt); 135 if(tt== dt_meshes3) return true; 136 else if (tt== dt_plots) return false; 137 cout << " Error Check : get " << tt << " == wait for "<< dt_meshes3 << " or "<< dt_plots << endl; 138 ffassert(0); 139 }*/ 140 GetMeshes3()141 inline int GetMeshes3() { long tt; read(tt); 142 143 if(tt== dt_meshes3) return 0; 144 else if (tt== dt_meshesS) return 1; 145 else if (tt== dt_meshesL) return 2; 146 else if (tt== dt_plots) return 3; /// modif dans ff gl 147 148 cout << " Error Check : get " << tt << " == wait for "<< dt_meshes3 << " or "<< dt_meshesS << " or "<< dt_meshesL << " or " << dt_plots << endl; 149 ffassert(0); 150 } 151 get(datatype t)152 void get(datatype t) { long tt; read(tt); 153 if( tt !=(long) t) 154 cout << " Error Check : get " << tt << " == wait for "<< t << endl; 155 ffassert(tt==(long) t);} 156 //FFCS:visualization stream redirection eof()157 bool eof() {return ffapi::ff_feof(TheStream);} read(bool & b)158 PlotStream& read( bool& b) {read(reinterpret_cast< void *> (&b),sizeof(bool)); b=r_endian(b);return *this;} read(long long & b)159 PlotStream& read( long long& b) {read(reinterpret_cast< void *> (&b),sizeof(long long)); b=r_endian(b);return *this;} read(long & b)160 PlotStream& read( long& b) { long long l; 161 read(reinterpret_cast< void *> (&l),sizeof(long long)); 162 l=r_endian(l); 163 b=(long) l; 164 if(( b-(long) l) !=0) 165 { cout << " err err read long : error " << b << " != " << l << endl; 166 assert( (b-(long) l)==0);} 167 return *this;} read(int & b)168 PlotStream& read( int& b) {read(reinterpret_cast< void *> (&b),sizeof(int)); b=r_endian(b);return *this;} read(double & b)169 PlotStream& read( double& b) {read(reinterpret_cast< void *> (&b),sizeof(double)); b=r_endian(b);return *this;} read(complex<double> & C)170 PlotStream &read( complex<double> &C) { double re,im; read(re); read(im); C=complex<double>(re,im); return 171 *this;} read(Fem2D::R1 & P)172 PlotStream &read( Fem2D::R1 & P) { return read(P.x);} read(Fem2D::R2 & P)173 PlotStream &read( Fem2D::R2 & P) { return read(P.x),read(P.y);} read(Fem2D::R3 & P)174 PlotStream &read( Fem2D::R3 & P) { return read(P.x),read(P.y),read(P.z);} read(string & b)175 PlotStream& read( string& b) { 176 int l; 177 read(l); 178 b.resize(l); 179 read(& (b[0]),l); 180 return *this; 181 } 182 183 184 operator >>(bool & b)185 PlotStream & operator >> ( bool& b) { return read(b); } operator >>(long & b)186 PlotStream & operator >> ( long& b) { return read(b); } operator >>(long long & b)187 PlotStream & operator >> ( long long& b) { return read(b); } operator >>(int & b)188 PlotStream & operator >> ( int& b) { return read(b); } operator >>(double & b)189 PlotStream & operator >> ( double& b) { return read(b); } operator >>(complex<double> & b)190 PlotStream & operator >> ( complex<double>& b) { return read(b); } operator >>(string & s)191 PlotStream & operator >> ( string& s) { return read(s); } operator >>(string * & s)192 PlotStream & operator >> ( string *& s) 193 { if(!s) s= new string(); return read(*s); 194 // cout << " fread string " << s <<endl; 195 } 196 operator >>(Mesh * & Th)197 PlotStream & operator >> ( Mesh *& Th) 198 { 199 long n; 200 read(n); 201 Serialize s(n,Mesh::magicmesh); 202 read(s,n ); 203 Th= new Mesh(s); 204 return *this; 205 } 206 207 208 template<class T> operator >>(KN<T> & b)209 PlotStream & operator >> ( KN<T>& b) 210 { 211 long n; 212 read(n); 213 // cout << "PlotStream >> : n " << n << endl; 214 // read empty array .... (n ==0) 215 if( ! b.N() && n) b.init(n);// if n ==0 do nothing .. Add FH nov. 2016 216 ffassert( b.N()==n); 217 for (int i=0;i<n;++i) 218 read(b[i]); 219 return *this; 220 } 221 // PlotStream & operator << (const Mesh3& Th); 222 // PlotStream & operator >> ( Mesh3 *& Th); operator >>(Fem2D::Mesh3 * & Th)223 PlotStream & operator >> ( Fem2D::Mesh3 *& Th) { Th= new Fem2D::Mesh3(TheStream); return *this;} operator >>(Fem2D::Mesh2 * & Th)224 PlotStream & operator >> ( Fem2D::Mesh2 *& Th) { Th= new Fem2D::Mesh2(TheStream); return *this;} operator >>(Fem2D::MeshS * & Th)225 PlotStream & operator >> ( Fem2D::MeshS *& Th) { Th= new Fem2D::MeshS(TheStream); return *this;} operator >>(Fem2D::MeshL * & Th)226 PlotStream & operator >> ( Fem2D::MeshL *& Th) { Th= new Fem2D::MeshL(TheStream); return *this;} 227 228 // --- I also write the type .. to skip data if we need to skip data 229 // just change >> and << by : <= and >= operator <=(const bool & b)230 PlotStream & operator <= (const bool& b) { return write(1),write(b); } operator <=(const long & b)231 PlotStream & operator <= (const long& b) { return write(2),write(b); } operator <=(const long long & b)232 PlotStream & operator <= (const long long & b) { return write(3),write(b); } operator <=(const int & b)233 PlotStream & operator <= (const int& b) { return write(4),write(b); } operator <=(const double & b)234 PlotStream & operator <= (const double& b) { return write(5),write(b); } operator <=(const string & s)235 PlotStream & operator <= (const string& s) { return write(6),write(s); } operator <=(const string * s)236 PlotStream & operator <= (const string* s) { return write(6),write(*s); } 237 template<class T> operator <=(const KN_<T> & b)238 PlotStream & operator <= (const KN_<T>& b) { return write(10),write((int) sizeof(T)),operator<<(b);} 239 operator >=(bool & b)240 PlotStream & operator >= ( bool& b) { return readc(1)>>b; } operator >=(long & b)241 PlotStream & operator >= ( long& b) { return readc(2)>>b; } operator >=(long long & b)242 PlotStream & operator >= ( long long & b) {return readc(3)>>b; } operator >=(int & b)243 PlotStream & operator >= ( int& b) { return readc(4)>>b; } operator >=(double & b)244 PlotStream & operator >= ( double& b) { return readc(5)>>b; } operator >=(string & s)245 PlotStream & operator >= ( string& s) { return readc(6)>>s; } operator >=(string * & s)246 PlotStream & operator >= ( string*& s) { return readc(6)>>s; } 247 template<class T> operator >=(KN<T> & b)248 PlotStream & operator >= ( KN<T>& b) { return readc(10), readc(sizeof(T)), operator>>(b);} readc(int cc)249 PlotStream & readc(int cc) { int c; read(c); assert(c==cc); return *this;} 250 SkipData()251 void SkipData() { int c; read(c); 252 bool b; 253 int i; 254 long l,n; 255 long long ll; 256 string s; 257 double d; 258 char buf[100]; 259 switch (c) { 260 case 1: read(b);break; 261 case 2: read(l);break; 262 case 3: read(ll);break; 263 case 4: read(i);break; 264 case 5: read(d);break; 265 case 6: read(s);break; 266 case 10: read(l); assert(l>0 && l <100); 267 read(n); 268 for(int i=0;i<n;++n) read(buf,l); 269 default: 270 break; 271 } 272 273 } 274 275 }; 276 277 #endif //PLOT_STREAM_HPP 278