Welcome to mirror list, hosted at ThFree Co, Russian Federation.

turbulencepart.cpp « preprocessed « mantaflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 168ae9cc2f2ed710882f0983939e9f29a51cf2a4 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288


// DO NOT EDIT !
// This file is generated using the MantaFlow preprocessor (prep generate).

/******************************************************************************
 *
 * MantaFlow fluid solver framework
 * Copyright 2011 Tobias Pfaff, Nils Thuerey
 *
 * This program is free software, distributed under the terms of the
 * Apache License, Version 2.0
 * http://www.ynu.org/licenses
 *
 * Turbulence particles
 *
 ******************************************************************************/

#include "turbulencepart.h"
#include "shapes.h"
#include "randomstream.h"

using namespace std;
namespace Manta {

TurbulenceParticleSystem::TurbulenceParticleSystem(FluidSolver *parent, WaveletNoiseField &noise)
    : ParticleSystem<TurbulenceParticleData>(parent), noise(noise)
{
}

ParticleBase *TurbulenceParticleSystem::clone()
{
  TurbulenceParticleSystem *nm = new TurbulenceParticleSystem(getParent(), noise);
  compress();

  nm->mData = mData;
  nm->setName(getName());
  return nm;
}

inline Vec3 hsv2rgb(Real h, Real s, Real v)
{
  Real r = 0, g = 0, b = 0;

  int i = (int)(h * 6);
  Real f = h * 6 - i;
  Real p = v * (1 - s);
  Real q = v * (1 - f * s);
  Real t = v * (1 - (1 - f) * s);

  switch (i % 6) {
    case 0:
      r = v, g = t, b = p;
      break;
    case 1:
      r = q, g = v, b = p;
      break;
    case 2:
      r = p, g = v, b = t;
      break;
    case 3:
      r = p, g = q, b = v;
      break;
    case 4:
      r = t, g = p, b = v;
      break;
    case 5:
      r = v, g = p, b = q;
      break;
    default:
      break;
  }

  return Vec3(r, g, b);
}

void TurbulenceParticleSystem::seed(Shape *shape, int num)
{
  static RandomStream rand(34894231);
  Vec3 sz = shape->getExtent(), p0 = shape->getCenter() - sz * 0.5;
  for (int i = 0; i < num; i++) {
    Vec3 p;
    do {
      p = rand.getVec3() * sz + p0;
    } while (!shape->isInside(p));
    Real z = (p.z - p0.z) / sz.z;
    add(TurbulenceParticleData(p, hsv2rgb(z, 0.75, 1.0)));
  }
}

void TurbulenceParticleSystem::resetTexCoords(int num, const Vec3 &inflow)
{
  if (num == 0) {
    for (int i = 0; i < size(); i++)
      mData[i].tex0 = mData[i].pos - inflow;
  }
  else {
    for (int i = 0; i < size(); i++)
      mData[i].tex1 = mData[i].pos - inflow;
  }
}

struct KnSynthesizeTurbulence : public KernelBase {
  KnSynthesizeTurbulence(TurbulenceParticleSystem &p,
                         FlagGrid &flags,
                         WaveletNoiseField &noise,
                         Grid<Real> &kGrid,
                         Real alpha,
                         Real dt,
                         int octaves,
                         Real scale,
                         Real invL0,
                         Real kmin)
      : KernelBase(p.size()),
        p(p),
        flags(flags),
        noise(noise),
        kGrid(kGrid),
        alpha(alpha),
        dt(dt),
        octaves(octaves),
        scale(scale),
        invL0(invL0),
        kmin(kmin)
  {
    runMessage();
    run();
  }
  inline void op(IndexInt idx,
                 TurbulenceParticleSystem &p,
                 FlagGrid &flags,
                 WaveletNoiseField &noise,
                 Grid<Real> &kGrid,
                 Real alpha,
                 Real dt,
                 int octaves,
                 Real scale,
                 Real invL0,
                 Real kmin) const
  {
    const Real PERSISTENCE = 0.56123f;

    const Vec3 pos(p[idx].pos);
    if (flags.isInBounds(pos)) {  // && !flags.isObstacle(pos)) {
      Real k2 = kGrid.getInterpolated(pos) - kmin;
      Real ks = k2 < 0 ? 0.0 : sqrt(k2);

      // Wavelet noise lookup
      Real amplitude = scale * ks;
      Real multiplier = invL0;
      Vec3 vel(0.);
      for (int o = 0; o < octaves; o++) {
        // Vec3 ns = noise.evaluateCurl(p[i].pos * multiplier) * amplitude;
        Vec3 n0 = noise.evaluateCurl(p[idx].tex0 * multiplier) * amplitude;
        Vec3 n1 = noise.evaluateCurl(p[idx].tex1 * multiplier) * amplitude;
        vel += alpha * n0 + (1.0f - alpha) * n1;

        // next scale
        amplitude *= PERSISTENCE;
        multiplier *= 2.0f;
      }

      // advection
      Vec3 dx = vel * dt;
      p[idx].pos += dx;
      p[idx].tex0 += dx;
      p[idx].tex1 += dx;
    }
  }
  inline TurbulenceParticleSystem &getArg0()
  {
    return p;
  }
  typedef TurbulenceParticleSystem type0;
  inline FlagGrid &getArg1()
  {
    return flags;
  }
  typedef FlagGrid type1;
  inline WaveletNoiseField &getArg2()
  {
    return noise;
  }
  typedef WaveletNoiseField type2;
  inline Grid<Real> &getArg3()
  {
    return kGrid;
  }
  typedef Grid<Real> type3;
  inline Real &getArg4()
  {
    return alpha;
  }
  typedef Real type4;
  inline Real &getArg5()
  {
    return dt;
  }
  typedef Real type5;
  inline int &getArg6()
  {
    return octaves;
  }
  typedef int type6;
  inline Real &getArg7()
  {
    return scale;
  }
  typedef Real type7;
  inline Real &getArg8()
  {
    return invL0;
  }
  typedef Real type8;
  inline Real &getArg9()
  {
    return kmin;
  }
  typedef Real type9;
  void runMessage()
  {
    debMsg("Executing kernel KnSynthesizeTurbulence ", 3);
    debMsg("Kernel range"
               << " size " << size << " ",
           4);
  };
  void operator()(const tbb::blocked_range<IndexInt> &__r) const
  {
    for (IndexInt idx = __r.begin(); idx != (IndexInt)__r.end(); idx++)
      op(idx, p, flags, noise, kGrid, alpha, dt, octaves, scale, invL0, kmin);
  }
  void run()
  {
    tbb::parallel_for(tbb::blocked_range<IndexInt>(0, size), *this);
  }
  TurbulenceParticleSystem &p;
  FlagGrid &flags;
  WaveletNoiseField &noise;
  Grid<Real> &kGrid;
  Real alpha;
  Real dt;
  int octaves;
  Real scale;
  Real invL0;
  Real kmin;
};

void TurbulenceParticleSystem::synthesize(FlagGrid &flags,
                                          Grid<Real> &k,
                                          int octaves,
                                          Real switchLength,
                                          Real L0,
                                          Real scale,
                                          Vec3 inflowBias)
{
  static Real ctime = 0;
  static Vec3 inflow(0.);
  Real dt = getParent()->getDt();

  // collect inflow bias
  inflow += inflowBias * dt;

  // alpha: hat function over time
  Real oldAlpha = 2.0f * nmod(ctime / switchLength, Real(1.0));
  ctime += dt;
  Real alpha = 2.0f * nmod(ctime / switchLength, Real(1.0));

  if (oldAlpha < 1.0f && alpha >= 1.0f)
    resetTexCoords(0, inflow);
  if (oldAlpha > alpha)
    resetTexCoords(1, inflow);
  if (alpha > 1.0f)
    alpha = 2.0f - alpha;
  alpha = 1.0;

  KnSynthesizeTurbulence(
      *this, flags, noise, k, alpha, dt, octaves, scale, 1.0f / L0, 1.5 * square(0.1));
}

void TurbulenceParticleSystem::deleteInObstacle(FlagGrid &flags)
{
  for (int i = 0; i < size(); i++)
    if (flags.isObstacle(mData[i].pos))
      mData[i].flag |= PDELETE;
  compress();
}

}  // namespace Manta