|
| 1 | +#include "DebugTools.h" |
| 2 | + |
| 3 | +#include "Utilities/Logger.h" |
| 4 | +#include <SPlisHSPlasH/Simulation.h> |
| 5 | + |
| 6 | +using namespace SPH; |
| 7 | +using namespace GenParam; |
| 8 | + |
| 9 | +int DebugTools::DETERMINE_THREAD_IDS = -1; |
| 10 | +int DebugTools::DETERMINE_NUM_NEIGHBORS = -1; |
| 11 | +int DebugTools::DETERMINE_VELOCITY_CHANGES = -1; |
| 12 | + |
| 13 | +DebugTools::DebugTools() : |
| 14 | + ParameterObject() |
| 15 | +{ |
| 16 | + m_determineThreadIds = false; |
| 17 | + |
| 18 | + Simulation* sim = Simulation::getCurrent(); |
| 19 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 20 | + for (unsigned int fluidModelIndex = 0; fluidModelIndex < nModels; fluidModelIndex++) |
| 21 | + { |
| 22 | + FluidModel* model = sim->getFluidModel(fluidModelIndex); |
| 23 | + model->addField({ "threadId", FieldType::UInt, [this, fluidModelIndex](const unsigned int i) -> unsigned int* { return &m_threadIds[fluidModelIndex][i]; } }); |
| 24 | + model->addField({ "numNeighbors", FieldType::UInt, [this, fluidModelIndex](const unsigned int i) -> unsigned int* { return &m_numNeighbors[fluidModelIndex][i]; } }); |
| 25 | + model->addField({ "velocityChanges", FieldType::Vector3, [this, fluidModelIndex](const unsigned int i) -> Real* { return &m_velocityChanges[fluidModelIndex][i][0]; } }); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +DebugTools::~DebugTools() |
| 30 | +{ |
| 31 | + Simulation* sim = Simulation::getCurrent(); |
| 32 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 33 | + for (unsigned int fluidModelIndex = 0; fluidModelIndex < nModels; fluidModelIndex++) |
| 34 | + { |
| 35 | + FluidModel* model = sim->getFluidModel(fluidModelIndex); |
| 36 | + model->removeFieldByName("threadId"); |
| 37 | + model->removeFieldByName("numNeighbors"); |
| 38 | + model->removeFieldByName("velocityChanges"); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void DebugTools::init() |
| 43 | +{ |
| 44 | + initParameters(); |
| 45 | + |
| 46 | + Simulation* sim = Simulation::getCurrent(); |
| 47 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 48 | + |
| 49 | + m_threadIds.resize(nModels); |
| 50 | + m_numNeighbors.resize(nModels); |
| 51 | + m_vOld.resize(nModels); |
| 52 | + m_velocityChanges.resize(nModels); |
| 53 | + for (unsigned int i = 0; i < nModels; i++) |
| 54 | + { |
| 55 | + FluidModel* fm = sim->getFluidModel(i); |
| 56 | + m_threadIds[i].resize(fm->numParticles(), 0); |
| 57 | + m_numNeighbors[i].resize(fm->numParticles(), 0); |
| 58 | + m_vOld[i].resize(fm->numParticles(), Vector3r::Zero()); |
| 59 | + m_velocityChanges[i].resize(fm->numParticles(), Vector3r::Zero()); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void SPH::DebugTools::cleanup() |
| 64 | +{ |
| 65 | + Simulation* sim = Simulation::getCurrent(); |
| 66 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 67 | + |
| 68 | + for (unsigned int i = 0; i < nModels; i++) |
| 69 | + { |
| 70 | + m_threadIds[i].clear(); |
| 71 | + m_numNeighbors[i].clear(); |
| 72 | + m_vOld[i].clear(); |
| 73 | + m_velocityChanges[i].clear(); |
| 74 | + } |
| 75 | + m_threadIds.clear(); |
| 76 | + m_numNeighbors.clear(); |
| 77 | + m_vOld.clear(); |
| 78 | + m_velocityChanges.clear(); |
| 79 | +} |
| 80 | + |
| 81 | +void DebugTools::initParameters() |
| 82 | +{ |
| 83 | + ParameterObject::initParameters(); |
| 84 | + |
| 85 | + DETERMINE_THREAD_IDS = createBoolParameter("determineThreadIds", "Determine Thread IDs", &m_determineThreadIds); |
| 86 | + setGroup(DETERMINE_THREAD_IDS, "Debug Tools"); |
| 87 | + setDescription(DETERMINE_THREAD_IDS, "Determine Thread IDs and add a corresponding particle field."); |
| 88 | + |
| 89 | + DETERMINE_NUM_NEIGHBORS = createBoolParameter("determineNumNeighbors", "Determine # neighbors", &m_determineNumNeighbors); |
| 90 | + setGroup(DETERMINE_NUM_NEIGHBORS, "Debug Tools"); |
| 91 | + setDescription(DETERMINE_NUM_NEIGHBORS, "Determine number of neighbors and add a corresponding particle field."); |
| 92 | + |
| 93 | + DETERMINE_VELOCITY_CHANGES = createBoolParameter("determineVelocityChanges", "Determine velocity changes", &m_determineVelocityChanges); |
| 94 | + setGroup(DETERMINE_VELOCITY_CHANGES, "Debug Tools"); |
| 95 | + setDescription(DETERMINE_VELOCITY_CHANGES, "Determine velocity change of each particle and add a corresponding particle field."); |
| 96 | +} |
| 97 | + |
| 98 | +void SPH::DebugTools::determineThreadIds() |
| 99 | +{ |
| 100 | + Simulation* sim = Simulation::getCurrent(); |
| 101 | + const unsigned int nFluids = sim->numberOfFluidModels(); |
| 102 | + |
| 103 | + for (unsigned int fluidModelIndex = 0; fluidModelIndex < nFluids; fluidModelIndex++) |
| 104 | + { |
| 105 | + FluidModel* model = sim->getFluidModel(fluidModelIndex); |
| 106 | + const int numParticles = (int)model->numActiveParticles(); |
| 107 | + |
| 108 | + #pragma omp parallel default(shared) |
| 109 | + { |
| 110 | + #pragma omp for schedule(static) |
| 111 | + for (int i = 0; i < numParticles; i++) |
| 112 | + { |
| 113 | +#ifdef _OPENMP |
| 114 | + m_threadIds[fluidModelIndex][i] = omp_get_thread_num(); |
| 115 | +#else |
| 116 | + m_threadIds[fluidModelIndex][i] = 0; |
| 117 | +#endif |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void SPH::DebugTools::determineNumNeighbors() |
| 124 | +{ |
| 125 | + Simulation* sim = Simulation::getCurrent(); |
| 126 | + const unsigned int nFluids = sim->numberOfFluidModels(); |
| 127 | + |
| 128 | + for (unsigned int fluidModelIndex = 0; fluidModelIndex < nFluids; fluidModelIndex++) |
| 129 | + { |
| 130 | + FluidModel* model = sim->getFluidModel(fluidModelIndex); |
| 131 | + const int numParticles = (int)model->numActiveParticles(); |
| 132 | + |
| 133 | + #pragma omp parallel default(shared) |
| 134 | + { |
| 135 | + #pragma omp for schedule(static) |
| 136 | + for (int i = 0; i < numParticles; i++) |
| 137 | + { |
| 138 | + m_numNeighbors[fluidModelIndex][i] = 0; |
| 139 | + for (unsigned int pid = 0; pid < sim->numberOfPointSets(); pid++) |
| 140 | + m_numNeighbors[fluidModelIndex][i] += sim->numberOfNeighbors(fluidModelIndex, pid, i); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +void SPH::DebugTools::determineVelocityChanges() |
| 147 | +{ |
| 148 | + Simulation* sim = Simulation::getCurrent(); |
| 149 | + const unsigned int nFluids = sim->numberOfFluidModels(); |
| 150 | + |
| 151 | + for (unsigned int fluidModelIndex = 0; fluidModelIndex < nFluids; fluidModelIndex++) |
| 152 | + { |
| 153 | + FluidModel* model = sim->getFluidModel(fluidModelIndex); |
| 154 | + const int numParticles = (int)model->numActiveParticles(); |
| 155 | + |
| 156 | + #pragma omp parallel default(shared) |
| 157 | + { |
| 158 | + #pragma omp for schedule(static) |
| 159 | + for (int i = 0; i < numParticles; i++) |
| 160 | + { |
| 161 | + m_velocityChanges[fluidModelIndex][i] = model->getVelocity(i) - m_vOld[fluidModelIndex][i]; |
| 162 | + m_vOld[fluidModelIndex][i] = model->getVelocity(i); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +void DebugTools::step() |
| 169 | +{ |
| 170 | + if (m_determineThreadIds) |
| 171 | + determineThreadIds(); |
| 172 | + |
| 173 | + if (m_determineNumNeighbors) |
| 174 | + determineNumNeighbors(); |
| 175 | + |
| 176 | + if (m_determineVelocityChanges) |
| 177 | + determineVelocityChanges(); |
| 178 | +} |
| 179 | + |
| 180 | +void DebugTools::reset() |
| 181 | +{ |
| 182 | + Simulation* sim = Simulation::getCurrent(); |
| 183 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 184 | + |
| 185 | + for (unsigned int i = 0; i < nModels; i++) |
| 186 | + { |
| 187 | + FluidModel* fm = sim->getFluidModel(i); |
| 188 | + for (unsigned int j = 0; j < fm->numActiveParticles(); j++) |
| 189 | + { |
| 190 | + m_threadIds[i][j] = 0; |
| 191 | + m_numNeighbors[i][j] = 0; |
| 192 | + m_vOld[i][j].setZero(); |
| 193 | + m_velocityChanges[i][j].setZero(); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +void DebugTools::performNeighborhoodSearchSort() |
| 199 | +{ |
| 200 | + Simulation* sim = Simulation::getCurrent(); |
| 201 | + const unsigned int nModels = sim->numberOfFluidModels(); |
| 202 | + |
| 203 | + for (unsigned int i = 0; i < nModels; i++) |
| 204 | + { |
| 205 | + FluidModel* fm = sim->getFluidModel(i); |
| 206 | + const unsigned int numPart = fm->numActiveParticles(); |
| 207 | + if (numPart != 0) |
| 208 | + { |
| 209 | + auto const& d = sim->getNeighborhoodSearch()->point_set(fm->getPointSetIndex()); |
| 210 | + d.sort_field(&m_vOld[i][0]); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void DebugTools::emittedParticles(FluidModel* model, const unsigned int startIndex) |
| 216 | +{ |
| 217 | +} |
| 218 | + |
0 commit comments