File: Smoke2D.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (158 lines) | stat: -rw-r--r-- 5,347 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)

#include "Smoke2D.h"
#include "Wm5Memory.h"

//----------------------------------------------------------------------------
template <typename Real>
Smoke2D<Real>::Smoke2D (Real x0, Real y0, Real x1, Real y1, Real dt,
    Real denViscosity, Real velViscosity, int imax, int jmax,
    int numGaussSeidelIterations, bool densityDirichlet, int numVortices)
    :
    FLUIDBASE(x0, y0, x1, y1, dt, denViscosity, velViscosity, imax, jmax,
        numGaussSeidelIterations, densityDirichlet),
    mNumVortices(numVortices),
    mNumActive(0),
    mGravity((Real)0)
{
    mVortexCenter = new1<Vector2<Real> >(mNumVortices);
    mVortexVariance = new1<Real>(mNumVortices);
    mVortexAmplitude = new1<Real>(mNumVortices);
    mTimelessDensity = new2<Real>(mIMaxP1, mJMaxP1);
    mTimelessVortex = new3<Vector2<Real> >(mNumVortices, mIMaxP1, mJMaxP1);
    mTimelessWind = new2<Vector2<Real> >(mIMaxP1, mJMaxP1);

    int v;
    for (v = 0; v < mNumVortices; ++v)
    {
        mVortexCenter[v][0] = Math<Real>::UnitRandom();
        mVortexCenter[v][1] = Math<Real>::UnitRandom();
        mVortexVariance[v] = Math<Real>::IntervalRandom((Real)0.001,
            (Real)0.01);
        mVortexAmplitude[v] = Math<Real>::IntervalRandom((Real)128,
            (Real)256);
        if (Math<Real>::SymmetricRandom() < (Real)0)
        {
            mVortexAmplitude[v] *= (Real)-1;
        }
    }

    for (int j = 0; j <= mJMax; ++j)
    {
        Real y = mY[j];

        for (int i = 0; i <= mIMax; ++i)
        {
            Real x = mX[i];

            // density source
            Real dx = x - (Real)0.25;
            Real dy = y - (Real)0.75;
            Real arg = -(dx*dx + dy*dy)/(Real)0.01;
            mTimelessDensity[j][i] = ((Real)2)*Math<Real>::Exp(arg);

            // density sink
            dx = x - (Real)0.75;
            dy = y - (Real)0.25;
            arg = -(dx*dx + dy*dy)/(Real)0.01;
            mTimelessDensity[j][i] -= ((Real)2)*Math<Real>::Exp(arg);

            // velocity vortex source
            for (v = 0; v < mNumVortices; ++v)
            {
                dx = x - mVortexCenter[v][0];
                dy = y - mVortexCenter[v][1];
                arg = -(dx*dx + dy*dy)/mVortexVariance[v];
                Vector2<Real> vortex(dy, -dx);
                vortex *= mVortexAmplitude[v]*Math<Real>::Exp(arg);
                mTimelessVortex[j][i][v] = vortex;
            }

            // velocity wind source
            Real diff = y - (Real)0.5;
            Real ampl = ((Real)32)*Math<Real>::Exp(-diff*diff/(Real)0.001);
            Vector2<Real> wind(ampl, (Real)0);
            mTimelessWind[j][i] = wind;
        }
    }
}
//----------------------------------------------------------------------------
template <typename Real>
Smoke2D<Real>::~Smoke2D ()
{
    delete1(mVortexCenter);
    delete1(mVortexVariance);
    delete1(mVortexAmplitude);
    delete2(mTimelessDensity);
    delete3(mTimelessVortex);
    delete2(mTimelessWind);
}
//----------------------------------------------------------------------------
template <typename Real>
Real Smoke2D<Real>::InitialDensity (Real, Real, int, int)
{
    return Math<Real>::UnitRandom();
}
//----------------------------------------------------------------------------
template <typename Real>
Vector2<Real> Smoke2D<Real>::InitialVelocity (Real, Real, int, int)
{
    return Vector2<Real>::ZERO;
}
//----------------------------------------------------------------------------
template <typename Real>
Real Smoke2D<Real>::SourceDensity (Real, Real, Real, int i, int j)
{
    return mTimelessDensity[j][i];
}
//----------------------------------------------------------------------------
template <typename Real>
Vector2<Real> Smoke2D<Real>::SourceVelocity (Real, Real, Real, int i, int j)
{
    Vector2<Real> impulse =
        Vector2<Real>((Real)0, -mGravity) +
        mTimelessWind[j][i];

    for (int v = 0; v < mNumActive; ++v)
    {
        impulse += mTimelessVortex[j][i][v];
    }

    return impulse;
}
//----------------------------------------------------------------------------
template <typename Real>
void Smoke2D<Real>::SetNumActiveVortices (int numActive)
{
    if (0 <= numActive && numActive <= mNumVortices)
    {
        mNumActive = numActive;
    }
    else
    {
        mNumActive = 0;
    }
}
//----------------------------------------------------------------------------
template <typename Real>
void Smoke2D<Real>::SetGravity (Real gravity)
{
    if (gravity > (Real)0)
    {
        mGravity = gravity;
    }
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template class Smoke2D<float>;
template class Smoke2D<double>;
//----------------------------------------------------------------------------