|
| 1 | +#include "AnimationField.h" |
| 2 | +#include "SPHKernels.h" |
| 3 | +#include <iostream> |
| 4 | +#include "TimeManager.h" |
| 5 | +#include "TimeStep.h" |
| 6 | +#include "FluidModel.h" |
| 7 | +#include "Simulation.h" |
| 8 | +#include "extern/tinyexpr/tinyexpr.h" |
| 9 | +#include "Utilities/Logger.h" |
| 10 | + |
| 11 | +using namespace SPH; |
| 12 | + |
| 13 | +AnimationField::AnimationField( |
| 14 | + const std::string &particleFieldName, |
| 15 | + const Vector3r &pos, const Matrix3r & rotation, const Vector3r &scale, |
| 16 | + const std::string expression[3], const unsigned int type) |
| 17 | + : m_particleFieldName(particleFieldName) |
| 18 | + , m_x(pos) |
| 19 | + , m_rotation(rotation) |
| 20 | + , m_scale(scale) |
| 21 | + , m_type(type) |
| 22 | + , m_startTime(0) |
| 23 | + , m_endTime(REAL_MAX) |
| 24 | +{ |
| 25 | + for (int i = 0; i < 3; i++) |
| 26 | + m_expression[i] = expression[i]; |
| 27 | +} |
| 28 | + |
| 29 | +AnimationField::~AnimationField(void) |
| 30 | +{ |
| 31 | +} |
| 32 | + |
| 33 | +void AnimationField::reset() |
| 34 | +{ |
| 35 | +} |
| 36 | + |
| 37 | +double getTime() |
| 38 | +{ |
| 39 | + return TimeManager::getCurrent()->getTime(); |
| 40 | +} |
| 41 | + |
| 42 | +void AnimationField::step() |
| 43 | +{ |
| 44 | + Simulation *sim = Simulation::getCurrent(); |
| 45 | + TimeManager *tm = TimeManager::getCurrent(); |
| 46 | + const Real t = tm->getTime(); |
| 47 | + const Real dt = tm->getTimeStepSize(); |
| 48 | + |
| 49 | + if (t >= m_startTime && t <= m_endTime) |
| 50 | + { |
| 51 | + // animate particles |
| 52 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 53 | + for (unsigned int m = 0; m < nModels; m++) |
| 54 | + { |
| 55 | + FluidModel *fm = sim->getFluidModel(m); |
| 56 | + const unsigned int numParticles = fm->numActiveParticles(); |
| 57 | + |
| 58 | + // find angular velocity field |
| 59 | + const FieldDescription *particleField = nullptr; |
| 60 | + for (unsigned int j = 0; j < fm->numberOfFields(); j++) |
| 61 | + { |
| 62 | + const FieldDescription &field = fm->getField(j); |
| 63 | + if (field.name == m_particleFieldName) |
| 64 | + { |
| 65 | + particleField = &field; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (particleField == nullptr) |
| 71 | + continue; |
| 72 | + |
| 73 | + #pragma omp parallel for schedule(static) default(shared) |
| 74 | + for (int i = 0; i < (int)numParticles; i++) |
| 75 | + { |
| 76 | + const Vector3r &xi = fm->getPosition(i); |
| 77 | + const Vector3r &vi = fm->getVelocity(i); |
| 78 | + if (inShape(m_type, xi, m_x, m_rotation, m_scale)) |
| 79 | + { |
| 80 | + Eigen::Map<Vector3r> value(particleField->getFct(i)); |
| 81 | + te_variable vars[] = { {"t", &t}, {"dt", &dt}, |
| 82 | + {"x", &xi[0]}, {"y", &xi[1]}, {"z", &xi[2]}, |
| 83 | + {"vx", &vi[0]}, {"vy", &vi[1]}, {"vz", &vi[2]}, |
| 84 | + {"valuex", &value[0]}, {"valuey", &value[1]}, {"valuez", &value[2]}, |
| 85 | + }; |
| 86 | + const int numVars = 11; |
| 87 | + int err; |
| 88 | + |
| 89 | + ////////////////////////////////////////////////////////////////////////// |
| 90 | + // v_x |
| 91 | + ////////////////////////////////////////////////////////////////////////// |
| 92 | + if (m_expression[0] != "") |
| 93 | + { |
| 94 | + te_expr *expr_vx = te_compile(m_expression[0].c_str(), vars, numVars, &err); |
| 95 | + if (expr_vx) |
| 96 | + value[0] = te_eval(expr_vx); |
| 97 | + te_free(expr_vx); |
| 98 | + |
| 99 | + if (err != 0) |
| 100 | + LOG_ERR << "Animation field: expression for x is wrong."; |
| 101 | + } |
| 102 | + |
| 103 | + ////////////////////////////////////////////////////////////////////////// |
| 104 | + // v_y |
| 105 | + ////////////////////////////////////////////////////////////////////////// |
| 106 | + if (m_expression[1] != "") |
| 107 | + { |
| 108 | + te_expr *expr_vy = te_compile(m_expression[1].c_str(), vars, numVars, &err); |
| 109 | + if (expr_vy) |
| 110 | + value[1] = te_eval(expr_vy); |
| 111 | + te_free(expr_vy); |
| 112 | + |
| 113 | + if (err != 0) |
| 114 | + LOG_ERR << "Animation field: expression for y is wrong."; |
| 115 | + } |
| 116 | + |
| 117 | + ////////////////////////////////////////////////////////////////////////// |
| 118 | + // v_z |
| 119 | + ////////////////////////////////////////////////////////////////////////// |
| 120 | + if (m_expression[2] != "") |
| 121 | + { |
| 122 | + te_expr *expr_vz = te_compile(m_expression[2].c_str(), vars, numVars, &err); |
| 123 | + if (expr_vz) |
| 124 | + value[2] = te_eval(expr_vz); |
| 125 | + te_free(expr_vz); |
| 126 | + |
| 127 | + if (err != 0) |
| 128 | + LOG_ERR << "Animation field: expression for z is wrong."; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
0 commit comments