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 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
|
#include "vtkLagrangianIntegrationModelExample.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkLagrangianParticle.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStringArray.h"
#include <cstring>
vtkObjectFactoryNewMacro(vtkLagrangianIntegrationModelExample);
//----------------------------------------------------------------------------
vtkLagrangianIntegrationModelExample::vtkLagrangianIntegrationModelExample()
{
// Fill the helper array
// Here one should set the seed and surface array name and components
// It will enable the uses of helper filters
this->SeedArrayNames->InsertNextValue("Particle Diameter");
this->SeedArrayComps->InsertNextValue(1);
this->SeedArrayTypes->InsertNextValue(VTK_DOUBLE);
this->SeedArrayNames->InsertNextValue("Particle Density");
this->SeedArrayComps->InsertNextValue(1);
this->SeedArrayTypes->InsertNextValue(VTK_DOUBLE);
// No Enum surface entry
SurfaceArrayDescription surfaceType2Description;
surfaceType2Description.nComp = 1;
surfaceType2Description.type = VTK_CHAR;
this->SurfaceArrayDescriptions["SurfaceType2"] = surfaceType2Description;
// More enum surface entry, use a reference "&" !
SurfaceArrayDescription& surfaceTypeDescription = this->SurfaceArrayDescriptions["SurfaceType"];
surfaceTypeDescription.enumValues.push_back(std::make_pair(101, "SpecificModel"));
this->NumIndepVars = 8; // x, y, z, u, v, w, diameter, t
this->NumFuncs = this->NumIndepVars - 1;
// Adding a tracked user data
this->NumberOfTrackedUserData = 1;
}
//----------------------------------------------------------------------------
void vtkLagrangianIntegrationModelExample::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
int vtkLagrangianIntegrationModelExample::FunctionValues(vtkLagrangianParticle* particle,
vtkDataSet* dataSet, vtkIdType cellId, double* weights, double* x, double* f)
{
// Initialize output
std::fill(f, f + this->NumFuncs, 0.0);
// Check for a particle
if (!particle)
{
vtkErrorMacro("No Particle to integrate");
return 0;
}
// Sanity Check
if (!dataSet || cellId == -1)
{
vtkErrorMacro("No cell or dataset to integrate the Particle on : Dataset: "
<< dataSet << " CellId:" << cellId);
return 0;
}
// Fetch flowVelocity at index 3
double flowVelocity[3];
if (this->GetFlowOrSurfaceDataNumberOfComponents(3, dataSet) != 3 ||
!this->GetFlowOrSurfaceData(particle, 3, dataSet, cellId, weights, flowVelocity))
{
vtkErrorMacro("Flow velocity is not set in source flow dataset or "
"have incorrect number of components, cannot use Matida equations");
return 0;
}
// Fetch flowDensity at index 4
double flowDensity;
if (this->GetFlowOrSurfaceDataNumberOfComponents(4, dataSet) != 1 ||
!this->GetFlowOrSurfaceData(particle, 4, dataSet, cellId, weights, &flowDensity))
{
vtkErrorMacro("Flow density is not set in source flow dataset or "
"have incorrect number of components, cannot use Matida equations");
return 0;
}
// Fetch flowDynamicViscosity at index 5
double flowDynamicViscosity;
if (this->GetFlowOrSurfaceDataNumberOfComponents(5, dataSet) != 1 ||
!this->GetFlowOrSurfaceData(particle, 5, dataSet, cellId, weights, &flowDynamicViscosity))
{
vtkErrorMacro("Flow dynamic viscosity is not set in source flow dataset or "
"have incorrect number of components, cannot use Matida equations");
return 0;
}
// Fetch Particle Diameter as the first user variables
double particleDiameter = particle->GetUserVariables()[0];
// Fetch Particle Density at index 7
vtkDataArray* particleDensities = vtkDataArray::SafeDownCast(this->GetSeedArray(7, particle));
if (!particleDensities)
{
vtkErrorMacro("Particle density is not set in particle data, "
"cannot use Matida equations");
return 0;
}
double particleDensity = particleDensities->GetTuple1(0);
// Recover Gravity constant, idx 8, FieldData, as defined in the xml.
// We read at a index 0 because these is the only tuple in the fieldData
double gravityConstant;
if (this->GetFlowOrSurfaceDataNumberOfComponents(8, dataSet) != 1 ||
!this->GetFlowOrSurfaceData(particle, 8, dataSet, 0, weights, &gravityConstant))
{
vtkErrorMacro("GravityConstant is not set in source flow dataset or have"
"incorrect number of components, cannot use Matida Equations");
return 0;
}
// Compute function values
for (int i = 0; i < 3; i++)
{
// Matida Equation
f[i + 3] = (flowVelocity[i] - x[i + 3]) *
this->GetDragCoefficient(flowVelocity, particle->GetVelocity(), flowDynamicViscosity,
particleDiameter, flowDensity) /
(this->GetRelaxationTime(flowDynamicViscosity, particleDiameter, particleDensity));
f[i] = x[i + 3];
}
// Use the read gravity constant instead of G
f[5] -= gravityConstant * (1 - (flowDensity / particleDensity));
// Compute the supplementary variable diameter
f[6] = -particleDiameter * 1 / 10;
// An usage example of tracked user data that sum the number of steps alongside the particles
std::vector<double>& prevUserData = particle->GetTrackedUserData();
std::vector<double>& userData = particle->GetTrackedUserData();
userData[0] = prevUserData[0] + particle->GetNumberOfSteps();
return 1;
}
//---------------------------------------------------------------------------
double vtkLagrangianIntegrationModelExample::GetRelaxationTime(
const double& dynVisc, const double& diameter, const double& density)
{
if (dynVisc == 0)
{
return std::numeric_limits<double>::infinity();
}
else
{
return (density * diameter * diameter) / (18.0 * dynVisc);
}
}
//---------------------------------------------------------------------------
double vtkLagrangianIntegrationModelExample::GetDragCoefficient(const double* flowVelocity,
const double* particleVelocity, const double& dynVisc, const double& particleDiameter,
const double& flowDensity)
{
if (dynVisc == 0)
{
return -1.0 * std::numeric_limits<double>::infinity();
}
else
{
double relativeVelocity[3];
for (int i = 0; i < 3; i++)
{
relativeVelocity[i] = particleVelocity[i] - flowVelocity[i];
}
double relativeSpeed = vtkMath::Norm(relativeVelocity);
double reynolds = flowDensity * relativeSpeed * particleDiameter / dynVisc;
return (1 + 0.15 * pow(reynolds, 0.687));
}
}
//----------------------------------------------------------------------------
void vtkLagrangianIntegrationModelExample::InitializeParticle(vtkLagrangianParticle* particle)
{
double* diameter = particle->GetUserVariables();
// Recover Particle Diameter, idx 6, see xml file
vtkDoubleArray* particleDiameters = vtkDoubleArray::SafeDownCast(this->GetSeedArray(6, particle));
if (!particleDiameters)
{
vtkErrorMacro("ParticleDiameter is not set in particle data, variable not set");
}
else
{
// Copy seed data to user variables
*diameter = particleDiameters->GetTuple1(0);
}
}
//----------------------------------------------------------------------------
void vtkLagrangianIntegrationModelExample::InitializeParticleData(
vtkFieldData* variablesParticleData, int maxTuples)
{
this->Superclass::InitializeParticleData(variablesParticleData, maxTuples);
// Create a double array
vtkNew<vtkDoubleArray> diameterArray;
// Name it
diameterArray->SetName("VariableDiameter");
// Set the number of component
int nComp = 1;
diameterArray->SetNumberOfComponents(nComp);
// Allocate a default number of tuple, multiplied
// by the number of components
diameterArray->Allocate(maxTuples * nComp);
variablesParticleData->AddArray(diameterArray.Get());
}
//----------------------------------------------------------------------------
void vtkLagrangianIntegrationModelExample::InsertParticleData(
vtkLagrangianParticle* particle, vtkFieldData* data, int stepEnum)
{
this->Superclass::InsertParticleData(particle, data, stepEnum);
vtkDataArray* diameterArray = data->GetArray("VariableDiameter");
// Add the correct data depending of the step enum
// Previous, Current or Next
switch (stepEnum)
{
case (vtkLagrangianBasicIntegrationModel::VARIABLE_STEP_PREV):
{
// Always use InsertNextTuple methods to add data
diameterArray->InsertNextTuple1(particle->GetPrevUserVariables()[0]);
break;
}
case (vtkLagrangianBasicIntegrationModel::VARIABLE_STEP_CURRENT):
{
diameterArray->InsertNextTuple1(particle->GetUserVariables()[0]);
break;
}
case (vtkLagrangianBasicIntegrationModel::VARIABLE_STEP_NEXT):
{
diameterArray->InsertNextTuple1(particle->GetNextUserVariables()[0]);
break;
}
}
}
//----------------------------------------------------------------------------
bool vtkLagrangianIntegrationModelExample::CheckFreeFlightTermination(
vtkLagrangianParticle* particle)
{
// Free Flight example, depending of the variable diameter of the particle we
// choose to terminate the particle or not
return (particle->GetUserVariables()[0] < 0.07);
}
//----------------------------------------------------------------------------
bool vtkLagrangianIntegrationModelExample::InteractWithSurface(int vtkNotUsed(surfaceType),
vtkLagrangianParticle* particle, vtkDataSet* surface, vtkIdType cellId,
std::queue<vtkLagrangianParticle*>& vtkNotUsed(particles))
{
// Surface Interaction example, depending of the variable diameter of the
// particle we choose to do one thing or the other
if (particle->GetUserVariables()[0] > 0.075)
{
// One could even redefine its own way to bounce, eg BounceSpecific(particle...)
// Or even redefine BounceParticle for all bounce surface
return this->BounceParticle(particle, surface, cellId);
}
else
{
return this->TerminateParticle(particle);
}
}
//----------------------------------------------------------------------------
bool vtkLagrangianIntegrationModelExample::IntersectWithLine(vtkLagrangianParticle* particle,
vtkCell* cell, double p1[3], double p2[3], double tol, double& t, double x[3])
{
// Here one could implement its own intersection code
return this->Superclass::IntersectWithLine(particle, cell, p1, p2, tol, t, x);
}
//----------------------------------------------------------------------------
void vtkLagrangianIntegrationModelExample::ComputeSurfaceDefaultValues(
const char* arrayName, vtkDataSet* dataset, int nComponents, double* defaultValues)
{
if (strcmp(arrayName, "SurfaceType2") == 0)
{
for (int i = 0; i < nComponents; i++)
{
defaultValues[i] = 3;
}
return;
}
else if (strcmp(arrayName, "SurfaceType") == 0)
{
for (int i = 0; i < nComponents; i++)
{
defaultValues[i] = 2;
}
return;
}
else
{
this->Superclass::ComputeSurfaceDefaultValues(arrayName, dataset, nComponents, defaultValues);
}
}
|