sphereShape.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 #include "sphereShape.hpp"
22 #include "dictionary.hpp"
23 #include "vocabs.hpp"
24 #include "streams.hpp"
25 
26 
28 (
29  const wordVector& names
30 )
31 {
32  names_.clear();
33  uint32 i=0;
34  for(const auto& nm:names)
35  {
36  if(!names_.insertIf(nm, i))
37  {
39  " repeated name in the list of sphere names: " << names;
40  return false;
41  }
42  i++;
43  }
44  names_.rehash(0);
45 
46  numShapes_ = names_.size();
47 
48  return true;
49 }
50 
51 
53 (
54  const dictionary& dict
55 )
56 {
57  diameters_ = dict.getVal<realVector>("diameters");
58  materials_ = dict.getVal<wordVector>("materials");
59  auto names = dict.getVal<wordVector>("names");
60 
61  if(diameters_.size() != materials_.size() )
62  {
64  " number of elements in diameters and properties are not the same in "<< dict.globalName()<<endl;
65  return false;
66  }
67  else if(diameters_.size() != names.size() )
68  {
70  " number of elements in diameters and names are not the same in "<< dict.globalName()<<endl;
71  return false;
72  }
73 
74  if( !insertNames(names) )
75  {
77  " error in reading dictionary "<< dict.globalName();
78  return false;
79  }
80 
81  return true;
82 }
83 
85 (
86  dictionary& dict
87 )const
88 {
89 
90  if( !dict.add("diamters", diameters_) )
91  {
93  " Error in writing diameters to dictionary "<< dict.globalName()<<endl;
94  return false;
95  }
96 
97  if( !dict.add("properties", materials_) )
98  {
100  " Error in writing properties to dictionary "<< dict.globalName()<<endl;
101  return false;
102  }
103 
104  size_t n = names_.size();
105  wordVector names(n);
106  names.clear();
107  word nm;
108 
109  for(label i=0; i<n; i++)
110  {
111  indexToName(i, nm);
112  names.push_back(nm);
113  }
114 
115  if( !dict.add("names", names) )
116  {
118  " Error in writing names to dictionary "<< dict.globalName()<<endl;
119  return false;
120  }
121 
122  return true;
123 }
124 
125 
126 
128 (
129  const realVector& diameter,
130  const wordVector& property,
131  const wordVector& name
132 )
133 :
134  diameters_(diameter),
135  materials_(property)
136 {
137  if( !insertNames( name) )
138  {
139  fatalExit;
140  }
141 }
142 
144 (
145  wordVector& names,
146  realVector& diams
147 )const
148 {
149  diams.clear();
150  uint32 idx;
151  for(const auto& nm:names)
152  {
153  if(!nameToIndex(nm, idx))
154  {
156  " invalid shape name requested "<< nm <<endl;
157  return false;
158  }
159  diams.push_back(diameters_[idx]);
160  }
161 
162  return true;
163 }
164 
165 
167 {
168 
169  dictionary sphereDict(sphereShapeFile__, true);
170 
171  if( !sphereDict.read(is) )
172  {
173  ioErrorInFile(is.name(), is.lineNumber()) <<
174  " error in reading dictionray " << sphereShapeFile__ <<" from file. \n";
175  return false;
176  }
177 
178  if( !readDictionary(sphereDict) )
179  {
180  return false;
181  }
182 
183  return true;
184 }
185 
187 {
188 
189  dictionary sphereDict(sphereShapeFile__, true);
190 
191  if( !writeDictionary(sphereDict))
192  {
193  return false;
194  }
195 
196  if( !sphereDict.write(os) )
197  {
198  ioErrorInFile( os.name(), os.lineNumber() )<<
199  " error in writing dictionray to file. \n";
200  return false;
201  }
202 
203  return true;
204 }
pFlow::sphereShapeFile__
const char * sphereShapeFile__
Definition: vocabs.hpp:41
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::dictionary::write
virtual bool write(iOstream &os) const
Definition: dictionary.cpp:780
pFlow::dictionary::read
virtual bool read(iIstream &is)
Definition: dictionary.cpp:759
pFlow::sphereShape::write
bool write(iOstream &os) const
Definition: sphereShape.cpp:186
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::dictionary::globalName
virtual word globalName() const
Definition: dictionary.cpp:349
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::dictionary::add
bool add(const word &keyword, const float &v)
Definition: dictionary.cpp:422
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::sphereShape::read
bool read(iIstream &is)
Definition: sphereShape.cpp:166
sphereShape.hpp
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::iIstream
Definition: iIstream.hpp:33
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::sphereShape::sphereShape
sphereShape()
Definition: sphereShape.hpp:60
dictionary.hpp
pFlow::sphereShape::writeDictionary
bool writeDictionary(dictionary &dict) const
Definition: sphereShape.cpp:85
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
streams.hpp
pFlow::dictionary::getVal
T getVal(const word &keyword) const
Definition: dictionary.hpp:309
pFlow::property
property holds the pure properties of materials.
Definition: property.hpp:40
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::sphereShape::insertNames
bool insertNames(const wordVector &names)
Definition: sphereShape.cpp:28
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::sphereShape::readDictionary
bool readDictionary(const dictionary &dict)
Definition: sphereShape.cpp:53
vocabs.hpp
pFlow::Vector< word >
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::sphereShape::shapeToDiameter
bool shapeToDiameter(wordVector &names, realVector &diams) const
Definition: sphereShape.cpp:144
pFlow::dictionary
Definition: dictionary.hpp:38