initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / lagrangian / molecularDynamics / molecule / molecule / moleculeIO.C
blobf6b50b848e634ff2395693576477a8a5cb776ff5
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "molecule.H"
28 #include "IOstreams.H"
30 #include "moleculeCloud.H"
32 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
34 Foam::molecule::molecule
36     const Cloud<molecule>& cloud,
37     Istream& is,
38     bool readFields
41     Particle<molecule>(cloud, is)
43     if (readFields)
44     {
45         if (is.format() == IOstream::ASCII)
46         {
47             id_ = readLabel(is);
48             mass_ = readScalar(is);
49             is >> U_;
50             is >> A_;
51             is >> potentialEnergy_;
52             is >> rf_;
53             is >> tethered_;
54             is >> tetherPosition_;
55         }
56         else
57         {
58             is.read
59             (
60                 reinterpret_cast<char*>(&mass_),
61                 sizeof(mass_)
62                 + sizeof(U_)
63                 + sizeof(A_)
64                 + sizeof(tetherPosition_)
65                 + sizeof(potentialEnergy_)
66                 + sizeof(rf_)
67                 + sizeof(tethered_)
68                 + sizeof(id_)
69             );
70         }
71     }
73     // Check state of Istream
74     is.check("Foam::molecule::molecule(Foam::Istream&)");
78 namespace Foam
81 void molecule::readFields(moleculeCloud& mC)
83     if (!mC.size())
84     {
85         return;
86     }
88     IOField<label> id(mC.fieldIOobject("id"));
89     mC.checkFieldIOobject(mC, id);
91     IOField<scalar> mass(mC.fieldIOobject("mass"));
92     mC.checkFieldIOobject(mC, mass);
94     IOField<vector> U(mC.fieldIOobject("U"));
95     mC.checkFieldIOobject(mC, U);
97     IOField<vector> A(mC.fieldIOobject("A"));
98     mC.checkFieldIOobject(mC, A);
100     IOField<label> tethered(mC.fieldIOobject("tethered"));
101     mC.checkFieldIOobject(mC, tethered);
103     IOField<vector> tetherPositions(mC.fieldIOobject("tetherPositions"));
104     mC.checkFieldIOobject(mC, tetherPositions);
106     label i = 0;
107     forAllIter(moleculeCloud, mC, iter)
108     {
109         molecule& mol = iter();
111         mol.id_ = id[i];
112         mol.mass_ = mass[i];
113         mol.U_ = U[i];
114         mol.A_ = A[i];
115         mol.potentialEnergy_ = 0.0;
116         mol.rf_ = tensor::zero;
117         mol.tethered_ = tethered[i];
118         mol.tetherPosition_ = tetherPositions[i];
119         i++;
120     }
124 void molecule::writeFields(const moleculeCloud& mC)
126     Particle<molecule>::writeFields(mC);
128     label np =  mC.size();
130     IOField<label> id(mC.fieldIOobject("id"), np);
131     IOField<scalar> mass(mC.fieldIOobject("mass"), np);
132     IOField<vector> U(mC.fieldIOobject("U"), np);
133     IOField<vector> A(mC.fieldIOobject("A"), np);
134     IOField<label> tethered(mC.fieldIOobject("tethered"), np);
135     IOField<vector> tetherPositions(mC.fieldIOobject("tetherPositions"), np);
137     label i = 0;
138     forAllConstIter(moleculeCloud, mC, iter)
139     {
140         const molecule& mol = iter();
142         id[i] = mol.id_;
143         mass[i] = mol.mass_;
144         U[i] = mol.U_;
145         A[i] = mol.A_;
146         tethered[i] = mol.tethered_;
147         tetherPositions[i] = mol.tetherPosition_;
148         i++;
149     }
151     id.write();
152     mass.write();
153     U.write();
154     A.write();
155     tethered.write();
156     tetherPositions.write();
159 };  // end of namespace Foam
162 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
164 Foam::Ostream& Foam::operator<<(Ostream& os, const molecule& mol)
166     if (os.format() == IOstream::ASCII)
167     {
168         os  << mol.id_
169                 << token::SPACE << mol.mass_
170                 << token::SPACE << static_cast<const Particle<molecule>&>(mol)
171                 << token::SPACE << mol.face()
172                 << token::SPACE << mol.stepFraction()
173                 << token::SPACE << mol.U_
174                 << token::SPACE << mol.A_
175                 << token::SPACE << mol.potentialEnergy_
176                 << token::SPACE << mol.rf_
177                 << token::SPACE << mol.tethered_
178                 << token::SPACE << mol.tetherPosition_;
179     }
180     else
181     {
182         os  << static_cast<const Particle<molecule>&>(mol);
183         os.write
184         (
185             reinterpret_cast<const char*>(&mol.mass_),
186             sizeof(mol.mass_)
187             + sizeof(mol.U_)
188             + sizeof(mol.A_)
189             + sizeof(mol.tetherPosition_)
190             + sizeof(mol.potentialEnergy_)
191             + sizeof(mol.rf_)
192             + sizeof(mol.tethered_)
193             + sizeof(mol.id_)
194         );
195     }
197     // Check state of Ostream
198     os.check
199     (
200         "Foam::Ostream& Foam::operator<<"
201         "(Foam::Ostream&, const Foam::molecule&)"
202     );
204     return os;
208 // ************************************************************************* //