www.cemf.ir
Field.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 template<template<class, class> class VectorField, class T, class PropType>
23 (
24  iIstream& is,
25  size_t len,
26  bool readLength
27 )
28 {
29  size_t flen = 0;
30  if( readLength )
31  {
32  is>>flen;
33 
34  if(is.bad() || is.eof() )
35  {
36  ioErrorInFile( is.name(), is.lineNumber() ) <<
37  "expected integer value to specify field length \n";
38  return false;
39  }
40  }
41  else
42  {
43  flen = len;
44  }
45 
46  // create a vector with one element and read it in
47  T bF;
48 
49  is >> bF;
50 
51  VectorType::assign(flen, bF);
52 
53  // read end statement char ;
54  is.readEndStatement("readField");
55 
56  return true;
57 }
58 
59 
60 template<template<class, class> class VectorField, class T, class PropType>
62 (
63  iIstream& is,
64  size_t len
65 )
66 {
67  size_t flen = 0;
68 
69 
70  is >> flen;
71  if(is.bad() || is.eof() )
72  {
73  ioErrorInFile( is.name(), is.lineNumber() ) <<
74  " expected integer value to specify field length \n";
75  return false;
76  }
77 
78  if( len!=0 && flen != len )
79  {
80  ioErrorInFile( is.name(), is.lineNumber() ) <<
81  " expected "<< len <<" as the field length but found "<< flen <<" \n";
82  return false;
83  }
84 
85  VectorType::readVector(is, flen);
86  is.readEndStatement("readField");
87  if( this->size() != flen )
88  {
89  ioErrorInFile( is.name(), is.lineNumber() ) <<
90  " expected " << flen << " elements, but supplied "<<
91  this->size() << " elements in file "<< is.name() <<endl;
92  return false;
93  }
94 
95 
96  return true;
97 }
98 
99 
100 template<template<class, class> class VectorField, class T, class PropType>
102 (
103  iIstream& is,
104  const size_t len,
105  bool resume,
106  bool readLength
107 )
108 {
109 
110  bool tokenFound;
111  if( resume )
112  tokenFound = is.findTokenResume(fieldKey_);
113  else
114  tokenFound = is.findToken(fieldKey_);
115 
116  if( !tokenFound )
117  {
118  ioErrorInFile( is.name(), is.lineNumber() ) <<
119  " error in searching for filedkey " << fieldKey_<<endl;
120  return false;
121  }
122 
123  word fieldU;
124 
125  is >> fieldU;
126 
127  if(is.bad() || is.eof())
128  {
129  ioErrorInFile( is.name(), is.lineNumber()) <<
130  " error in reading keyword from file.";
131  return false;
132  }
133 
134  if( fieldU == uniform__ )
135  {
136 
137  return readUniform(is, len, readLength);
138 
139  }
140  else if( fieldU == nonUniform__ )
141  {
142 
143  return readNonUniform(is, len);
144 
145  }
146  else
147  {
148  ioErrorInFile( is.name(), is.lineNumber() ) <<
149  "unknown keyword, expected uniform/nonUniform, but found " <<
150  fieldU << endl;
151  return false;
152  }
153 
154  return true;
155 }
156 
157 
158 template<template<class, class> class VectorField, class T, class PropType>
160 (
161  iIstream& is,
162  bool resume
163 )
164 {
165  return readField(is, 0, resume ,true);
166 }
167 
168 
169 template<template<class, class> class VectorField, class T, class PropType>
171 {
172 
173  os.writeWordKeyword(fieldKey_) << nonUniform__<<endl;
174  os<< this->size()<<endl;
175 
176  VectorType::write(os);
177 
178  os.endEntry();
179 
180  return true;
181 }
182 
pFlow::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.hpp:190
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::iIstream::readEndStatement
char readEndStatement(const char *funcName)
Definition: iIstream.cpp:332
pFlow::uniform__
const char * uniform__
Definition: vocabs.hpp:52
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:320
pFlow::nonUniform__
const char * nonUniform__
Definition: vocabs.hpp:53
pFlow::iIstream::findTokenResume
virtual bool findTokenResume(const word &w)
search for all tokesn after the current file position and find the first word token tbat matchs w
Definition: iIstream.cpp:66
pFlow::Field::readNonUniform
bool readNonUniform(iIstream &is, size_t len)
Definition: Field.cpp:62
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.hpp:202
pFlow::Field::readField
bool readField(iIstream &is, const size_t len, bool resume, bool readLength=true)
Definition: Field.cpp:102
pFlow::iOstream::endEntry
virtual iOstream & endEntry()
Write end entry (';') followed by newline.
Definition: iOstream.cpp:97
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::Field::readUniform
bool readUniform(iIstream &is, size_t len, bool readLength=true)
Definition: Field.cpp:23
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:221
pFlow::iIstream::findToken
virtual bool findToken(const word &w)
Definition: iIstream.cpp:60
pFlow::Field::writeField
bool writeField(iOstream &os) const
Definition: Field.cpp:170
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::iOstream::writeWordKeyword
virtual iOstream & writeWordKeyword(const word &kw)
Write the keyword followed by an appropriate indentation.
Definition: iOstream.cpp:41