Fluid: Fix for issues with external forces

A general refactor / fix commit that should clear out the issues that have been reported on external forces and moving effectors (e.g. T79537, T81660, T80088).
This commit is contained in:
Sebastián Barschkis 2020-10-18 20:33:58 +02:00
parent 1f046e05b6
commit 663e047102
Notes: blender-bot 2023-02-13 20:43:02 +01:00
Referenced by issue #82175, Why do Blender 2.82a, Blender 2.90.1, and Blender 2.91 smoke simulations look different?
3 changed files with 26 additions and 45 deletions

View File

@ -122,7 +122,6 @@ timePerFrame_s$ID$ = $TIME_PER_FRAME$\n\
# In Blender fluid.c: frame_length = DT_DEFAULT * (25.0 / fps) * time_scale\n\
# with DT_DEFAULT = 0.1\n\
frameLength_s$ID$ = $FRAME_LENGTH$\n\
frameLengthUnscaled_s$ID$ = frameLength_s$ID$ / timeScale_s$ID$\n\
frameLengthRaw_s$ID$ = 0.1 * 25 # dt = 0.1 at 25 fps\n\
\n\
dt0_s$ID$ = $DT$\n\
@ -149,18 +148,9 @@ mantaMsg('1 Blender length unit is ' + str(ratioResToBLength_s$ID$) + ' Mantaflo
ratioBTimeToTimestep_s$ID$ = float(1) / float(frameLengthRaw_s$ID$) # the time within 1 blender time unit, see also fluid.c\n\
mantaMsg('1 Blender time unit is ' + str(ratioBTimeToTimestep_s$ID$) + ' Mantaflow time units long.')\n\
\n\
ratioFrameToFramelength_s$ID$ = float(1) / float(frameLengthUnscaled_s$ID$ ) # the time within 1 frame\n\
mantaMsg('frame / frameLength is ' + str(ratioFrameToFramelength_s$ID$) + ' Mantaflow time units long.')\n\
\n\
scaleAcceleration_s$ID$ = ratioResToBLength_s$ID$ * (ratioBTimeToTimestep_s$ID$**2)# [meters/btime^2] to [cells/timestep^2] (btime: sec, min, or h, ...)\n\
mantaMsg('scaleAcceleration is ' + str(scaleAcceleration_s$ID$))\n\
\n\
scaleSpeedFrames_s$ID$ = ratioResToBLength_s$ID$ * ratioFrameToFramelength_s$ID$ # [blength/frame] to [cells/frameLength]\n\
mantaMsg('scaleSpeed is ' + str(scaleSpeedFrames_s$ID$))\n\
\n\
scaleSpeedTime_s$ID$ = ratioResToBLength_s$ID$ * ratioBTimeToTimestep_s$ID$ # [blength/btime] to [cells/frameLength]\n\
mantaMsg('scaleSpeedTime is ' + str(scaleSpeedTime_s$ID$))\n\
\n\
gravity_s$ID$ *= scaleAcceleration_s$ID$ # scale from world acceleration to cell based acceleration\n\
\n\
# OpenVDB options\n\
@ -380,17 +370,10 @@ def fluid_pre_step_$ID$():\n\
x_obvel_s$ID$.safeDivide(numObs_s$ID$)\n\
y_obvel_s$ID$.safeDivide(numObs_s$ID$)\n\
z_obvel_s$ID$.safeDivide(numObs_s$ID$)\n\
\n\
x_obvel_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
y_obvel_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
z_obvel_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
copyRealToVec3(sourceX=x_obvel_s$ID$, sourceY=y_obvel_s$ID$, sourceZ=z_obvel_s$ID$, target=obvelC_s$ID$)\n\
\n\
# translate invels (world space) to grid space\n\
if using_invel_s$ID$:\n\
x_invel_s$ID$.multConst(scaleSpeedTime_s$ID$)\n\
y_invel_s$ID$.multConst(scaleSpeedTime_s$ID$)\n\
z_invel_s$ID$.multConst(scaleSpeedTime_s$ID$)\n\
copyRealToVec3(sourceX=x_invel_s$ID$, sourceY=y_invel_s$ID$, sourceZ=z_invel_s$ID$, target=invelC_s$ID$)\n\
\n\
if using_guiding_s$ID$:\n\
@ -400,9 +383,6 @@ def fluid_pre_step_$ID$():\n\
velT_s$ID$.multConst(vec3(gamma_sg$ID$))\n\
\n\
# translate external forces (world space) to grid space\n\
x_force_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
y_force_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
z_force_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
copyRealToVec3(sourceX=x_force_s$ID$, sourceY=y_force_s$ID$, sourceZ=z_force_s$ID$, target=forces_s$ID$)\n\
\n\
# If obstacle has velocity, i.e. is a moving obstacle, switch to dynamic preconditioner\n\
@ -626,10 +606,6 @@ def bake_guiding_process_$ID$(framenr, format_guiding, path_guiding, resumable):
x_guidevel_s$ID$.safeDivide(numGuides_s$ID$)\n\
y_guidevel_s$ID$.safeDivide(numGuides_s$ID$)\n\
z_guidevel_s$ID$.safeDivide(numGuides_s$ID$)\n\
\n\
x_guidevel_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
y_guidevel_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
z_guidevel_s$ID$.multConst(scaleSpeedFrames_s$ID$)\n\
copyRealToVec3(sourceX=x_guidevel_s$ID$, sourceY=y_guidevel_s$ID$, sourceZ=z_guidevel_s$ID$, target=guidevelC_s$ID$)\n\
\n\
mantaMsg('Extrapolating guiding velocity')\n\

View File

@ -925,7 +925,7 @@ static void update_velocities(FluidEffectorSettings *fes,
velocity_map[index * 3 + 2] = hit_vel[2];
# ifdef DEBUG_PRINT
/* Debugging: Print object velocities. */
printf("adding effector object vel: [%f, %f, %f]\n", hit_vel[0], hit_vel[1], hit_vel[2]);
printf("setting effector object vel: [%f, %f, %f]\n", hit_vel[0], hit_vel[1], hit_vel[2]);
# endif
}
else {
@ -1065,7 +1065,7 @@ static void obstacles_from_mesh(Object *coll_ob,
add_v3fl_v3fl_v3i(co, mvert[i].co, fds->shift);
if (has_velocity) {
sub_v3_v3v3(&vert_vel[i * 3], co, &fes->verts_old[i * 3]);
mul_v3_fl(&vert_vel[i * 3], fds->dx / dt);
mul_v3_fl(&vert_vel[i * 3], 1.0f / dt);
}
copy_v3_v3(&fes->verts_old[i * 3], co);
@ -1824,6 +1824,7 @@ static void sample_mesh(FluidFlowSettings *ffs,
float *velocity_map,
int index,
const int base_res[3],
const float global_size[3],
const float flow_center[3],
BVHTreeFromMesh *tree_data,
const float ray_start[3],
@ -1983,9 +1984,23 @@ static void sample_mesh(FluidFlowSettings *ffs,
printf("adding flow object vel: [%f, %f, %f]\n", hit_vel[0], hit_vel[1], hit_vel[2]);
# endif
}
velocity_map[index * 3] += ffs->vel_coord[0];
velocity_map[index * 3 + 1] += ffs->vel_coord[1];
velocity_map[index * 3 + 2] += ffs->vel_coord[2];
/* Convert xyz velocities flow settings from world to grid space. */
float convert_vel[3];
copy_v3_v3(convert_vel, ffs->vel_coord);
float time_mult = 1.0 / (25.f * DT_DEFAULT);
float size_mult = MAX3(base_res[0], base_res[1], base_res[2]) /
MAX3(global_size[0], global_size[1], global_size[2]);
mul_v3_v3fl(convert_vel, ffs->vel_coord, size_mult * time_mult);
velocity_map[index * 3] += convert_vel[0];
velocity_map[index * 3 + 1] += convert_vel[1];
velocity_map[index * 3 + 2] += convert_vel[2];
# ifdef DEBUG_PRINT
printf("initial vel: [%f, %f, %f]\n",
velocity_map[index * 3],
velocity_map[index * 3 + 1],
velocity_map[index * 3 + 2]);
# endif
}
}
@ -2039,6 +2054,7 @@ static void emit_from_mesh_task_cb(void *__restrict userdata,
bb->velocity,
index,
data->fds->base_res,
data->fds->global_size,
data->flow_center,
data->tree,
ray_start,
@ -2136,7 +2152,7 @@ static void emit_from_mesh(
add_v3fl_v3fl_v3i(co, mvert[i].co, fds->shift);
if (has_velocity) {
sub_v3_v3v3(&vert_vel[i * 3], co, &ffs->verts_old[i * 3]);
mul_v3_fl(&vert_vel[i * 3], fds->dx / dt);
mul_v3_fl(&vert_vel[i * 3], 1.0 / dt);
}
copy_v3_v3(&ffs->verts_old[i * 3], co);
}
@ -2434,19 +2450,6 @@ static void adaptive_domain_adjust(
/* Redo adapt time step in manta to refresh solver state (ie time variables) */
manta_adapt_timestep(fds->fluid);
}
/* update global size field with new bbox size */
/* volume bounds */
float minf[3], maxf[3], size[3];
madd_v3fl_v3fl_v3fl_v3i(minf, fds->p0, fds->cell_size, fds->res_min);
madd_v3fl_v3fl_v3fl_v3i(maxf, fds->p0, fds->cell_size, fds->res_max);
/* calculate domain dimensions */
sub_v3_v3v3(size, maxf, minf);
/* apply object scale */
for (int i = 0; i < 3; i++) {
size[i] = fabsf(size[i] * ob->scale[i]);
}
copy_v3_v3(fds->global_size, size);
}
BLI_INLINE void apply_outflow_fields(int index,
@ -4423,7 +4426,7 @@ float BKE_fluid_get_velocity_at(struct Object *ob, float position[3], float velo
FluidDomainSettings *fds = fmd->domain;
float time_mult = 25.f * DT_DEFAULT;
float size_mult = MAX3(fds->global_size[0], fds->global_size[1], fds->global_size[2]) /
fds->maxres;
MAX3(fds->base_res[0], fds->base_res[1], fds->base_res[2]);
float vel_mag;
float density = 0.0f, fuel = 0.0f;
float pos[3];

View File

@ -2781,7 +2781,9 @@ static void rna_def_fluid_flow_settings(BlenderRNA *brna)
RNA_def_property_array(prop, 3);
RNA_def_property_range(prop, -1000.1, 1000.1);
RNA_def_property_ui_text(
prop, "Initial", "Initial velocity in X, Y and Z direction in world space");
prop,
"Initial",
"Additional initial velocity in X, Y and Z direction (added to source velocity)");
RNA_def_property_update(prop, NC_OBJECT | ND_MODIFIER, "rna_Fluid_flow_reset");
prop = RNA_def_property(srna, "volume_density", PROP_FLOAT, PROP_NONE);