streamLineParticle.C 11.1 KB
Newer Older
mattijs's avatar
mattijs committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
mattijs's avatar
mattijs committed
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
mattijs's avatar
mattijs committed
15
16
17
18
19
20
21

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
mattijs's avatar
mattijs committed
23
24
25
26

\*----------------------------------------------------------------------------*/

#include "streamLineParticle.H"
27
#include "vectorFieldIOField.H"
mattijs's avatar
mattijs committed
28
29
30
31
32
33

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    defineParticleTypeNameAndDebug(streamLineParticle, 0);
34
35
}

mattijs's avatar
mattijs committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

Foam::vector Foam::streamLineParticle::interpolateFields
(
    const streamLineParticle::trackData& td,
    const point& position,
    const label cellI
)
{
    if (cellI == -1)
    {
        FatalErrorIn("streamLineParticle::interpolateFields(..)")
            << "Cell:" << cellI << abort(FatalError);
    }

    sampledScalars_.setSize(td.vsInterp_.size());
    forAll(td.vsInterp_, scalarI)
    {
        sampledScalars_[scalarI].append
        (
            td.vsInterp_[scalarI].interpolate
            (
                position,
                cellI
            )
        );
    }

    sampledVectors_.setSize(td.vvInterp_.size());
    forAll(td.vvInterp_, vectorI)
    {
        sampledVectors_[vectorI].append
        (
            td.vvInterp_[vectorI].interpolate
            (
                position,
                cellI
            )
        );
    }

    const DynamicList<vector>& U = sampledVectors_[td.UIndex_];

80
    return U.last();
mattijs's avatar
mattijs committed
81
82
83
84
85
86
87
88
89
90
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

//- Construct from components
Foam::streamLineParticle::streamLineParticle
(
    const Cloud<streamLineParticle>& c,
    const vector& position,
91
    const label cellI,
mattijs's avatar
mattijs committed
92
93
94
    const label lifeTime
)
:
95
    Particle<streamLineParticle>(c, position, cellI),
mattijs's avatar
mattijs committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    lifeTime_(lifeTime)
{}


//- Construct from Istream
Foam::streamLineParticle::streamLineParticle
(
    const Cloud<streamLineParticle>& c,
    Istream& is,
    bool readFields
)
:
    Particle<streamLineParticle>(c, is, readFields)
{
    if (readFields)
    {
        //if (is.format() == IOstream::ASCII)
        List<scalarList> sampledScalars;
        List<vectorList> sampledVectors;

        is  >> lifeTime_ >> sampledPositions_ >> sampledScalars
            >> sampledVectors;

        sampledScalars_.setSize(sampledScalars.size());
        forAll(sampledScalars, i)
        {
            sampledScalars_[i].transfer(sampledScalars[i]);
        }
        sampledVectors_.setSize(sampledVectors.size());
        forAll(sampledVectors, i)
        {
            sampledVectors_[i].transfer(sampledVectors[i]);
        }
    }

    // Check state of Istream
    is.check
    (
        "streamLineParticle::streamLineParticle"
        "(const Cloud<streamLineParticle>&, Istream&, bool)"
    );
}


// Construct copy
Foam::streamLineParticle::streamLineParticle
(
    const streamLineParticle& c
)
:
    Particle<streamLineParticle>(c),
    lifeTime_(c.lifeTime_),
    sampledPositions_(c.sampledPositions_),
    sampledScalars_(c.sampledScalars_)
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

bool Foam::streamLineParticle::move(streamLineParticle::trackData& td)
{
    td.switchProcessor = false;
    td.keepParticle = true;

160
161
162
    // Set very large dt. Note: cannot use GREAT since 1/GREAT is SMALL
    // which is a trigger value for the tracking...
    scalar deltaT = Foam::sqrt(GREAT);  //cloud().pMesh().time().deltaTValue();
mattijs's avatar
mattijs committed
163
164
165
166
167
168
169
170
171
172
173
    scalar tEnd = (1.0 - stepFraction())*deltaT;
    scalar dtMax = tEnd;

    while
    (
        td.keepParticle
    && !td.switchProcessor
    && lifeTime_ > 0
    && tEnd > ROOTVSMALL
    )
    {
174
175
176
        // TBD: implement subcycling so step through cells in more than
        //      one step.

mattijs's avatar
mattijs committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
        // set the lagrangian time-step
        scalar dt = min(dtMax, tEnd);

        // Store current position and sampled velocity.
        --lifeTime_;
        sampledPositions_.append(position());
        vector U = interpolateFields(td, position(), cell());

        if (!td.trackForward_)
        {
            U = -U;
        }

        dt *= trackToFace(position()+dt*U, td);

        tEnd -= dt;
        stepFraction() = 1.0 - tEnd/deltaT;
    }

    if (!td.keepParticle || lifeTime_ == 0)
    {
        if (lifeTime_ == 0)
        {
mattijs's avatar
mattijs committed
200
201
202
203
204
205
206
            if (debug)
            {
                Pout<< "streamLineParticle : Removing stagnant particle:"
                    << static_cast<Particle<streamLineParticle> >(*this)
                    << " sampled positions:" << sampledPositions_.size()
                    << endl;
            }
mattijs's avatar
mattijs committed
207
208
209
210
211
212
213
214
            td.keepParticle = false;
        }
        else
        {
            // Normal exit. Store last position and fields
            sampledPositions_.append(position());
            interpolateFields(td, position(), cell());

mattijs's avatar
mattijs committed
215
216
217
218
219
220
221
            if (debug)
            {
                Pout<< "streamLineParticle : Removing particle:"
                    << static_cast<Particle<streamLineParticle> >(*this)
                    << " sampled positions:" << sampledPositions_.size()
                    << endl;
            }
mattijs's avatar
mattijs committed
222
223
224
225
226
        }

        // Transfer particle data into trackData.
        //td.allPositions_.append(sampledPositions_);
        td.allPositions_.append(vectorList());
227
        vectorList& top = td.allPositions_.last();
mattijs's avatar
mattijs committed
228
229
230
231
232
233
        top.transfer(sampledPositions_);

        forAll(sampledScalars_, i)
        {
            //td.allScalars_[i].append(sampledScalars_[i]);
            td.allScalars_[i].append(scalarList());
234
            scalarList& top = td.allScalars_[i].last();
mattijs's avatar
mattijs committed
235
236
237
238
239
240
            top.transfer(sampledScalars_[i]);
        }
        forAll(sampledVectors_, i)
        {
            //td.allVectors_[i].append(sampledVectors_[i]);
            td.allVectors_[i].append(vectorList());
241
            vectorList& top = td.allVectors_[i].last();
mattijs's avatar
mattijs committed
242
243
244
245
246
247
248
249
250
251
252
253
            top.transfer(sampledVectors_[i]);
        }
    }

    return td.keepParticle;
}


bool Foam::streamLineParticle::hitPatch
(
    const polyPatch&,
    streamLineParticle::trackData& td,
254
255
256
    const label patchI,
    const scalar trackFraction,
    const tetIndices& tetIs
mattijs's avatar
mattijs committed
257
258
259
260
261
262
263
264
265
266
267
)
{
    // Disable generic patch interaction
    return false;
}


bool Foam::streamLineParticle::hitPatch
(
    const polyPatch&,
    int&,
268
269
270
    const label,
    const scalar,
    const tetIndices&
mattijs's avatar
mattijs committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
)
{
    // Disable generic patch interaction
    return false;
}


void Foam::streamLineParticle::hitWedgePatch
(
    const wedgePolyPatch& pp,
    streamLineParticle::trackData& td
)
{
    // Remove particle
    td.keepParticle = false;
}


void Foam::streamLineParticle::hitWedgePatch
(
    const wedgePolyPatch&,
    int&
)
{}


void Foam::streamLineParticle::hitSymmetryPatch
(
    const symmetryPolyPatch& pp,
    streamLineParticle::trackData& td
)
{
    // Remove particle
    td.keepParticle = false;
}


void Foam::streamLineParticle::hitSymmetryPatch
(
    const symmetryPolyPatch&,
    int&
)
{}


void Foam::streamLineParticle::hitCyclicPatch
(
    const cyclicPolyPatch& pp,
    streamLineParticle::trackData& td
)
{
    // Remove particle
    td.keepParticle = false;
}


void Foam::streamLineParticle::hitCyclicPatch
(
    const cyclicPolyPatch&,
    int&
)
{}


void Foam::streamLineParticle::hitProcessorPatch
(
    const processorPolyPatch&,
    streamLineParticle::trackData& td
)
{
    // Switch particle
    td.switchProcessor = true;
}


void Foam::streamLineParticle::hitProcessorPatch
(
    const processorPolyPatch&,
    int&
)
{}


void Foam::streamLineParticle::hitWallPatch
(
    const wallPolyPatch& wpp,
357
358
    streamLineParticle::trackData& td,
    const tetIndices&
mattijs's avatar
mattijs committed
359
360
)
{
mattijs's avatar
mattijs committed
361
    // Remove particle
mattijs's avatar
mattijs committed
362
    td.keepParticle = false;
mattijs's avatar
mattijs committed
363
364
365
366
367
368
}


void Foam::streamLineParticle::hitWallPatch
(
    const wallPolyPatch& wpp,
369
370
    int&,
    const tetIndices&
mattijs's avatar
mattijs committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
)
{}


void Foam::streamLineParticle::hitPatch
(
    const polyPatch& wpp,
    streamLineParticle::trackData& td
)
{
    // Remove particle
    td.keepParticle = false;
}


void Foam::streamLineParticle::hitPatch
(
    const polyPatch& wpp,
    int&
)
{}


394
void Foam::streamLineParticle::readFields(Cloud<streamLineParticle>& c)
mattijs's avatar
mattijs committed
395
396
397
398
399
400
401
402
403
404
405
406
{
    if (!c.size())
    {
        return;
    }

    IOField<label> lifeTime
    (
        c.fieldIOobject("lifeTime", IOobject::MUST_READ)
    );
    c.checkFieldIOobject(c, lifeTime);

407
    vectorFieldIOField sampledPositions
mattijs's avatar
mattijs committed
408
409
410
411
412
    (
        c.fieldIOobject("sampledPositions", IOobject::MUST_READ)
    );
    c.checkFieldIOobject(c, sampledPositions);

413
//    vectorFieldIOField sampleVelocity
mattijs's avatar
mattijs committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//    (
//        c.fieldIOobject("sampleVelocity", IOobject::MUST_READ)
//    );
//    c.checkFieldIOobject(c, sampleVelocity);

    label i = 0;
    forAllIter(Cloud<streamLineParticle>, c, iter)
    {
        iter().lifeTime_ = lifeTime[i];
        iter().sampledPositions_.transfer(sampledPositions[i]);
//        iter().sampleVelocity_.transfer(sampleVelocity[i]);
        i++;
    }
}


430
void Foam::streamLineParticle::writeFields(const Cloud<streamLineParticle>& c)
mattijs's avatar
mattijs committed
431
432
433
434
435
436
437
438
439
440
{
    Particle<streamLineParticle>::writeFields(c);

    label np =  c.size();

    IOField<label> lifeTime
    (
        c.fieldIOobject("lifeTime", IOobject::NO_READ),
        np
    );
441
    vectorFieldIOField sampledPositions
mattijs's avatar
mattijs committed
442
443
444
445
    (
        c.fieldIOobject("sampledPositions", IOobject::NO_READ),
        np
    );
446
//    vectorFieldIOField sampleVelocity
mattijs's avatar
mattijs committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//    (
//        c.fieldIOobject("sampleVelocity", IOobject::NO_READ),
//        np
//    );

    label i = 0;
    forAllConstIter(Cloud<streamLineParticle>, c, iter)
    {
        lifeTime[i] = iter().lifeTime_;
        sampledPositions[i] = iter().sampledPositions_;
//        sampleVelocity[i] = iter().sampleVelocity_;
        i++;
    }

    lifeTime.write();
    sampledPositions.write();
//    sampleVelocity.write();
}


// * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //

Foam::Ostream& Foam::operator<<(Ostream& os, const streamLineParticle& p)
{
    os  << static_cast<const Particle<streamLineParticle>&>(p)
        << token::SPACE << p.lifeTime_
        << token::SPACE << p.sampledPositions_
        << token::SPACE << p.sampledScalars_
        << token::SPACE << p.sampledVectors_;

    // Check state of Ostream
    os.check("Ostream& operator<<(Ostream&, const streamLineParticle&)");

    return os;
}


// ************************************************************************* //