moleculeIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "molecule.H"
30 #include "IOstreams.H"
31 #include "moleculeCloud.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const std::size_t Foam::molecule::sizeofFields
36 (
37  offsetof(molecule, siteForces_) - offsetof(molecule, Q_)
38 );
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
44 (
45  const polyMesh& mesh,
46  Istream& is,
47  bool readFields,
48  bool newFormat
49 )
50 :
51  particle(mesh, is, readFields, newFormat),
52  Q_(Zero),
53  v_(Zero),
54  a_(Zero),
55  pi_(Zero),
56  tau_(Zero),
57  specialPosition_(Zero),
58  potentialEnergy_(0.0),
59  rf_(Zero),
60  special_(0),
61  id_(0),
62  siteForces_(),
63  sitePositions_()
64 {
65  if (readFields)
66  {
67  if (is.format() == IOstreamOption::ASCII)
68  {
69  is >> Q_
70  >> v_
71  >> a_
72  >> pi_
73  >> tau_
74  >> specialPosition_
75  >> potentialEnergy_
76  >> rf_
77  >> special_
78  >> id_;
79  }
80  else if (!is.checkLabelSize<>() || !is.checkScalarSize<>())
81  {
82  // Non-native label or scalar size
83 
84  is.beginRawRead();
85 
86  readRawScalar(is, Q_.data(), tensor::nComponents);
87  readRawScalar(is, v_.data(), vector::nComponents);
88  readRawScalar(is, a_.data(), vector::nComponents);
89  readRawScalar(is, pi_.data(), vector::nComponents);
90  readRawScalar(is, tau_.data(), vector::nComponents);
91  readRawScalar(is, specialPosition_.data(), vector::nComponents);
92  readRawScalar(is, &potentialEnergy_);
93  readRawScalar(is, rf_.data(), tensor::nComponents);
94  readRawLabel(is, &special_);
95  readRawLabel(is, &id_);
96 
97  is.endRawRead();
98  }
99  else
100  {
101  is.read(reinterpret_cast<char*>(&Q_), sizeofFields);
102  }
103 
104  is >> siteForces_ >> sitePositions_;
105  }
106 
107  is.check(FUNCTION_NAME);
108 }
109 
110 
111 void Foam::molecule::readFields(Cloud<molecule>& mC)
112 {
113  const bool readOnProc = mC.size();
114 
116 
117  IOField<tensor> Q(mC.fieldIOobject("Q", IOobject::MUST_READ), readOnProc);
118  mC.checkFieldIOobject(mC, Q);
119 
120  IOField<vector> v(mC.fieldIOobject("v", IOobject::MUST_READ), readOnProc);
121  mC.checkFieldIOobject(mC, v);
122 
123  IOField<vector> a(mC.fieldIOobject("a", IOobject::MUST_READ), readOnProc);
124  mC.checkFieldIOobject(mC, a);
125 
126  IOField<vector> pi(mC.fieldIOobject("pi", IOobject::MUST_READ), readOnProc);
127  mC.checkFieldIOobject(mC, pi);
128 
129  IOField<vector> tau
130  (
131  mC.fieldIOobject("tau", IOobject::MUST_READ),
132  readOnProc
133  );
134  mC.checkFieldIOobject(mC, tau);
135 
136  IOField<vector> specialPosition
137  (
138  mC.fieldIOobject("specialPosition", IOobject::MUST_READ),
139  readOnProc
140  );
141  mC.checkFieldIOobject(mC, specialPosition);
142 
143  IOField<label> special
144  (
145  mC.fieldIOobject("special", IOobject::MUST_READ),
146  readOnProc
147  );
148  mC.checkFieldIOobject(mC, special);
149 
150  IOField<label> id(mC.fieldIOobject("id", IOobject::MUST_READ), readOnProc);
151  mC.checkFieldIOobject(mC, id);
152 
153  label i = 0;
154  for (molecule& mol : mC)
155  {
156  mol.Q_ = Q[i];
157  mol.v_ = v[i];
158  mol.a_ = a[i];
159  mol.pi_ = pi[i];
160  mol.tau_ = tau[i];
161  mol.specialPosition_ = specialPosition[i];
162  mol.special_ = special[i];
163  mol.id_ = id[i];
164 
165  ++i;
166  }
167 }
168 
169 
170 void Foam::molecule::writeFields(const Cloud<molecule>& mC)
171 {
173 
174  const label np = mC.size();
175  const bool writeOnProc = mC.size();
176 
177  IOField<tensor> Q(mC.fieldIOobject("Q", IOobject::NO_READ), np);
178  IOField<vector> v(mC.fieldIOobject("v", IOobject::NO_READ), np);
179  IOField<vector> a(mC.fieldIOobject("a", IOobject::NO_READ), np);
180  IOField<vector> pi(mC.fieldIOobject("pi", IOobject::NO_READ), np);
181  IOField<vector> tau(mC.fieldIOobject("tau", IOobject::NO_READ), np);
182  IOField<vector> specialPosition
183  (
184  mC.fieldIOobject("specialPosition", IOobject::NO_READ),
185  np
186  );
187  IOField<label> special(mC.fieldIOobject("special", IOobject::NO_READ), np);
188  IOField<label> id(mC.fieldIOobject("id", IOobject::NO_READ), np);
189 
190  // Post processing fields
191 
192  IOField<vector> piGlobal
193  (
194  mC.fieldIOobject("piGlobal", IOobject::NO_READ),
195  np
196  );
197 
198  IOField<vector> tauGlobal
199  (
200  mC.fieldIOobject("tauGlobal", IOobject::NO_READ),
201  np
202  );
203 
204  IOField<vector> orientation1
205  (
206  mC.fieldIOobject("orientation1", IOobject::NO_READ),
207  np
208  );
209 
210  IOField<vector> orientation2
211  (
212  mC.fieldIOobject("orientation2", IOobject::NO_READ),
213  np
214  );
215 
216  IOField<vector> orientation3
217  (
218  mC.fieldIOobject("orientation3", IOobject::NO_READ),
219  np
220  );
221 
222  label i = 0;
223  for (const molecule& mol : mC)
224  {
225  Q[i] = mol.Q_;
226  v[i] = mol.v_;
227  a[i] = mol.a_;
228  pi[i] = mol.pi_;
229  tau[i] = mol.tau_;
230  specialPosition[i] = mol.specialPosition_;
231  special[i] = mol.special_;
232  id[i] = mol.id_;
233 
234  piGlobal[i] = mol.Q_ & mol.pi_;
235  tauGlobal[i] = mol.Q_ & mol.tau_;
236 
237  orientation1[i] = mol.Q_ & vector(1,0,0);
238  orientation2[i] = mol.Q_ & vector(0,1,0);
239  orientation3[i] = mol.Q_ & vector(0,0,1);
240 
241  ++i;
242  }
243 
244  Q.write(writeOnProc);
245  v.write(writeOnProc);
246  a.write(writeOnProc);
247  pi.write(writeOnProc);
248  tau.write(writeOnProc);
249  specialPosition.write(writeOnProc);
250  special.write(writeOnProc);
251  id.write(writeOnProc);
252 
253  piGlobal.write(writeOnProc);
254  tauGlobal.write(writeOnProc);
255 
256  orientation1.write(writeOnProc);
257  orientation2.write(writeOnProc);
258  orientation3.write(writeOnProc);
259 
260  Info<< "writeFields " << mC.name() << endl;
261 
262  if (isA<moleculeCloud>(mC))
263  {
264  const moleculeCloud& m = dynamic_cast<const moleculeCloud&>(mC);
265 
266  m.writeXYZ
267  (
268  m.mesh().time().timePath()/cloud::prefix/"moleculeCloud.xmol"
269  );
270  }
271 }
272 
273 
274 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
275 
276 Foam::Ostream& Foam::operator<<(Ostream& os, const molecule& mol)
277 {
279  {
280  os << token::SPACE << static_cast<const particle&>(mol)
281  << token::SPACE << mol.Q_
282  << token::SPACE << mol.v_
283  << token::SPACE << mol.a_
284  << token::SPACE << mol.pi_
285  << token::SPACE << mol.tau_
286  << token::SPACE << mol.specialPosition_
287  << token::SPACE << mol.potentialEnergy_
288  << token::SPACE << mol.rf_
289  << token::SPACE << mol.special_
290  << token::SPACE << mol.id_
291  << token::SPACE << mol.siteForces_
292  << token::SPACE << mol.sitePositions_;
293  }
294  else
295  {
296  os << static_cast<const particle&>(mol);
297  os.write
298  (
299  reinterpret_cast<const char*>(&mol.Q_),
301  );
302  os << mol.siteForces_ << mol.sitePositions_;
303  }
304 
306  return os;
307 }
308 
309 
310 // ************************************************************************* //
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const noexcept
Check if the scalar byte-size associated with the stream is the same as the given type...
Definition: IOstream.H:379
virtual Ostream & write(const char c) override
Write character.
Definition: OBJstream.C:69
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
"ascii" (normal default)
static const std::size_t sizeofFields
Size in bytes of the fields.
Definition: molecule.H:72
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual bool endRawRead()=0
End of low-level raw binary read.
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
static void writeFields(const Cloud< molecule > &mC)
Definition: moleculeIO.C:163
static void readFields(Cloud< molecule > &mC)
Definition: moleculeIO.C:104
Base particle class.
Definition: particle.H:69
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual Istream & read(token &)=0
Return next token from stream.
dynamicFvMesh & mesh
molecule(const polyMesh &mesh, const barycentric &coordinates, const label celli, const label tetFacei, const label tetPti, const tensor &Q, const vector &v, const vector &a, const vector &pi, const vector &tau, const vector &specialPosition, const constantProperties &constProps, const label special, const label id)
Construct from components.
Definition: moleculeI.H:219
void readFields(const typename GeoFieldType::Mesh &mesh, const IOobjectList &objects, const NameMatchPredicate &selectedFields, DynamicList< regIOobject *> &storedObjects)
Read the selected GeometricFields of the templated type and store on the objectRegistry.
Space [isspace].
Definition: token.H:131
constexpr scalar pi(M_PI)
Vector< scalar > vector
Definition: vector.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:39
static void readFields(TrackCloudType &c)
Read the fields associated with the owner cloud.
virtual bool beginRawRead()=0
Start of low-level raw binary read.
static void writeFields(const TrackCloudType &c)
Write the fields associated with the owner cloud.
Nothing to be read.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::molecule.
Definition: molecule.H:63
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const noexcept
Check if the label byte-size associated with the stream is the same as the given type.
Definition: IOstream.H:368
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
streamFormat format() const noexcept
Get the current stream format.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
static const word prefix
The prefix to local: lagrangian.
Definition: cloud.H:79