| import numpy as np |
| import sys |
| from ..data_holder import DataHolder |
| from ...Coefficient import * |
| from ...Sampling import * |
| from .wos_variable import * |
|
|
| class WostVariable(WosVariable): |
| def __init__(self, input : DataHolder, seed : int = 37, weight_window = [0.5, 2], |
| max_z : float = 4, green_sampling : GreenSampling = 0, |
| newton_steps : int = 5, use_accelaration : Bool = True, opt_params : list[str] = []): |
| super().__init__(input, seed, weight_window, max_z, |
| green_sampling, newton_steps, use_accelaration, opt_params) |
| |
| @dr.syntax |
| def take_step(self, L : ArrayXf, p : Particle, mode : dr.ADMode, split : Split, dL : ArrayXf, active : Bool, active_conf : ArrayXb = ArrayXb(True), |
| conf_numbers : list[UInt32] = None, max_length : UInt32 = None, tput_kill : Float = Float(0.8), fd_forward : bool = False, |
| illumination_mask: Bool = Bool(True)) -> Particle: |
| primal = (mode == dr.ADMode.Primal) |
|
|
| if conf_numbers is not None: |
| num_conf = len(conf_numbers) |
| else: |
| num_conf = 1 |
| |
| |
| bi = self.input.shape.boundary_interaction(p.points, star_generation = False, conf_numbers = conf_numbers) |
|
|
| if bi.is_far: |
| p.thrown = Bool(True) |
| active &= Bool(False) |
| |
| |
| σ_bar = self.input.σ_bar |
| z = Float(0) |
| if self.use_accel: |
| bi.r, σ_bar, z = self.input.get_Rσz(p.points, bi.r) |
| else: |
| z = bi.r * dr.sqrt(σ_bar) |
| if z > self.max_z: |
| bi.r *= self.max_z / z |
| z = self.max_z |
| |
| self.green.initialize(z) |
| |
| bi = self.input.shape.star_generation(bi) |
|
|
| |
| dirichlet_ending = (active & bi.is_e & bi.is_d) |
| |
| |
| added_near = dr.select(dirichlet_ending & active_conf, p.w * bi.dval, 0) |
| |
| L += added_near if primal else -added_near |
| |
| |
| active &= ~dirichlet_ending |
|
|
| |
| with dr.resume_grad(when = not primal): |
| α = self.input.α.get_value(p.points) |
| |
| |
| f_cont = Float(0) |
| if dr.hint(not self.input.f.is_zero, mode = 'scalar'): |
| sample_source = Point2f(p.sampler.next_float32(), p.sampler.next_float32()) |
| |
| r_f, normG = self.green.sample(sample_source[0], bi.r, σ_bar) |
| dir_f, _ = sample_star_direction(sample_source[1], bi.is_star & bi.on_boundary, bi.bn) |
| points_f = mi.Point2f(p.points + r_f * dir_f) |
| |
| |
| ri_f = self.input.shape.ray_intersect(bi, dir_f) |
| with dr.resume_grad(when=not primal): |
| α_f = self.input.α.get_value(points_f) |
| f_f = self.input.f.get_value(points_f) |
| f_cont = p.w * f_f * normG / dr.sqrt(α * α_f) |
| |
| |
| f_cont = dr.select(active_conf, f_cont, 0) |
| |
| L += f_cont if primal else -f_cont |
|
|
| |
| |
| |
| n_cont_cont = dr.zeros(ArrayXf, shape = L.shape) |
|
|
| if dr.hint(self.input.has_continuous_neumann, mode = 'scalar'): |
| |
| if dr.hint(self.input.NEE == NEE.Special, mode = 'scalar'): |
| for i in range(num_conf): |
| conf_number = None if conf_numbers is None else conf_numbers[i] |
| |
| sample_neumann = p.sampler.next_float32() |
| dist_n, n_val, pdf_n_r, p_n = self.input.sampleNEE_special(bi, sample_neumann, conf_number) |
| G_n_r = self.green.eval(dist_n, bi.r, σ_bar) |
| |
| if ((pdf_n_r > 0) & (dist_n < bi.r) & (dist_n > 0)): |
| n_cont_cont[i] = -p.w * n_val * G_n_r / pdf_n_r |
|
|
| with dr.resume_grad(when = not primal): |
| α_n = self.input.α.get_value(p_n) |
| n_cont_cont[i] *= dr.sqrt(dr.rcp( α * α_n)) if dr.hint(self.input.shape.measured_current, mode = 'scalar') else dr.sqrt(α_n / α) |
| |
| if dr.isnan(n_cont_cont[i]) | ~illumination_mask: |
| n_cont_cont[i] = Float(0) |
| else: |
| |
| dist_n, n_val, pdf_n_r, p_n = self.input.sampleNEE(bi, p.sampler.next_float32(), conf_numbers) |
| G_n_r = self.green.eval(dist_n, bi.r, σ_bar) |
| |
| n_cont_cont_ = Float(0) |
| if ((pdf_n_r > 0) & (dist_n < bi.r) & (dist_n > 0)): |
| n_cont_cont_ = -p.w * G_n_r / pdf_n_r |
|
|
| with dr.resume_grad(when = not primal): |
| α_n = self.input.α.get_value(p_n) |
| n_cont_cont_ *= dr.sqrt(dr.rcp( α * α_n)) if dr.hint(self.input.shape.measured_current, mode = 'scalar') else dr.sqrt(α_n / α) |
| |
| if dr.isnan(n_cont_cont_) | ~illumination_mask: |
| n_cont_cont_ = Float(0) |
| n_cont_cont = n_val * n_cont_cont_ |
| |
| |
| |
| n_cont_delta =dr.zeros(ArrayXf, shape = L.shape) |
| |
| if dr.hint(self.input.has_delta, mode = 'scalar'): |
| for i in range(num_conf): |
| conf_number = None if conf_numbers is None else conf_numbers[i] |
| |
| dist_n, n_val, pdf_n_r, sampled_n = self.input.get_point_neumann(bi, conf_number) |
| |
| for d, n, pdf_r, p_n in zip(dist_n, n_val, pdf_n_r, sampled_n): |
| n_cont_delta_iter = Float(0) |
| G_n_r = self.green.eval(d, bi.r, σ_bar) |
| with dr.resume_grad(when = not primal): |
| α_n = self.input.α.get_value(p_n) |
| if (pdf_r > 0) & (d <= bi.r): |
| n_cont_delta_iter = -p.w * n * G_n_r / pdf_r |
| |
| n_cont_delta_iter *= dr.sqrt(dr.rcp(α * α_n)) if dr.hint(self.input.shape.measured_current, mode = 'scalar') else dr.sqrt(α_n / α) |
| n_cont_delta[i] += n_cont_delta_iter |
| |
| if dr.isnan(n_cont_delta[i]) | ~illumination_mask: |
| n_cont_delta[i] = Float(0) |
| |
| |
| with dr.resume_grad(when = not primal): |
| n_cont = n_cont_cont + n_cont_delta |
| |
| if bi.on_boundary: |
| n_cont *= 2 |
| n_cont = dr.select(active_conf, n_cont, 0) |
| |
| L += n_cont if primal else -n_cont |
| |
| |
| |
| sample_rec = Point2f(p.sampler.next_float32(), p.sampler.next_float32()) |
| normG = self.green.eval_norm(bi.r, σ_bar) |
| prob_vol = σ_bar * normG |
| sample_vol = active & (sample_rec[0] < prob_vol) |
| sample_rec[0] = dr.select(sample_vol, sample_rec[0] / prob_vol, (sample_rec[0] - prob_vol) / (1-prob_vol)) |
| |
| dir_next, _, _ = bi.sample_recursive(sample_rec[1]) |
| |
| ri_next = self.input.shape.ray_intersect(bi, dir_next) |
| |
| r_next = Float(bi.r) |
| if sample_vol: |
| r_next = self.green.sample(sample_rec[0], bi.r, σ_bar)[0] |
| |
| |
| on_boundary_next = (ri_next.t < r_next) |
| sample_vol &= ~on_boundary_next |
| if on_boundary_next: |
| r_next = ri_next.t |
|
|
| |
| points_next = mi.Point2f(ri_next.origin + r_next * dir_next) |
|
|
| with dr.resume_grad(when=not primal): |
| α_next = self.input.α.get_value(points_next) |
| grad_α_next, laplacian_α_next = self.input.α.get_grad_laplacian(points_next) |
| σ_next = self.input.σ.get_value(points_next) |
| σ_new = self.σ_(σ_next, α_next, grad_α_next, laplacian_α_next) |
| w_ = dr.sqrt(α_next / α) |
| w_s = dr.select(sample_vol, (1.0 - σ_new / σ_bar), 1.0) |
| w_update = w_ * w_s |
| |
| prb_cont = dr.select(dr.isfinite(w_update), L * w_update / dr.detach(w_update), 0.0) |
| |
| |
| grad_cont = prb_cont + f_cont + n_cont |
| if dr.hint(mode == dr.ADMode.Backward, mode = 'scalar'): |
| dr.backward(dr.sum(grad_cont * dL)) |
| elif dr.hint(mode == dr.ADMode.Forward, mode = 'scalar'): |
| dL += dr.forward_to(dr.sum(grad_cont)) |
| |
| active &= dr.isfinite(w_update) |
| |
| |
| if dr.hint((not fd_forward), mode = 'scalar'): |
| if dr.hint(split == Split.Agressive, mode = 'scalar'): |
| p.w_split *= w_update |
| elif dr.hint(split == Split.Normal, mode = 'scalar'): |
| p.w_split *= w_s |
| else: |
| α = self.input.α_split.get_value(p.points) |
| α_next = self.input.α_split.get_value(points_next) |
| grad_α_next, laplacian_α_next = self.input.α_split.get_grad_laplacian(points_next) |
| σ_next = self.input.σ_split.get_value(points_next) |
| σ_new = self.σ_(σ_next, α_next, grad_α_next, laplacian_α_next) |
| w_ = dr.select(active, dr.sqrt(α_next / α), 1.0) |
| w_s = dr.select(sample_vol, (1.0 - σ_new / σ_bar), 1.0) |
| if dr.hint(split == Split.Agressive, mode = 'scalar'): |
| p.w_split *= (w_ * w_s) |
| elif dr.hint(split == Split.Normal, mode = 'scalar'): |
| p.w_split *= w_s |
| |
| if dr.hint(max_length is not None, mode = 'scalar'): |
| if p.path_length > max_length: |
| p.w *= tput_kill |
| p.w_split *= tput_kill |
|
|
| |
| p.w *= w_update |
| p.points = points_next |
| p.path_length += 1 |
| return p |