@@ -7,6 +7,10 @@ use rayon::prelude::*;
77
88use std:: cmp:: max;
99
10+ use atomic_counter:: { AtomicCounter , RelaxedCounter } ;
11+
12+ use std:: sync:: Arc ;
13+
1014pub struct SeriesApproximation {
1115 pub maximum_iteration : usize ,
1216 pub delta_pixel_square : FloatExtended ,
@@ -16,6 +20,7 @@ pub struct SeriesApproximation {
1620 approximation_probes : Vec < Vec < ComplexExtended > > ,
1721 approximation_probes_derivative : Vec < Vec < ComplexExtended > > ,
1822 pub min_valid_iteration : usize ,
23+ pub max_valid_iteration : usize ,
1924 pub valid_iterations : Vec < usize > ,
2025 pub valid_interpolation : Vec < usize > ,
2126 pub probe_sampling : usize ,
@@ -45,6 +50,7 @@ impl SeriesApproximation {
4550 approximation_probes : Vec :: new ( ) ,
4651 approximation_probes_derivative : Vec :: new ( ) ,
4752 min_valid_iteration : 1 ,
53+ max_valid_iteration : 1 ,
4854 valid_iterations : Vec :: new ( ) ,
4955 valid_interpolation : Vec :: new ( ) ,
5056 probe_sampling,
@@ -55,7 +61,7 @@ impl SeriesApproximation {
5561 }
5662 }
5763
58- pub fn generate_approximation ( & mut self , center_reference : & Reference ) {
64+ pub fn generate_approximation ( & mut self , center_reference : & Reference , series_approximation_counter : & Arc < RelaxedCounter > , stop_flag : & Arc < RelaxedCounter > ) {
5965 // Reset the coefficients
6066 self . coefficients = vec ! [ vec![ ComplexExtended :: new2( 0.0 , 0.0 , 0 ) ; self . order as usize + 1 ] ; 1 ] ;
6167
@@ -66,11 +72,14 @@ impl SeriesApproximation {
6672 let add_value = ComplexExtended :: new2 ( 1.0 , 0.0 , 0 ) ;
6773
6874 let mut previous_coefficients = self . coefficients [ 0 ] . clone ( ) ;
75+ let mut next_coefficients = vec ! [ ComplexExtended :: new2( 0.0 , 0.0 , 0 ) ; self . order as usize + 1 ] ;
6976
7077 // Can be changed later into a better loop - this function could also return some more information
7178 // Go through all remaining iterations
7279 for i in 1 ..self . maximum_iteration {
73- let mut next_coefficients = vec ! [ ComplexExtended :: new2( 0.0 , 0.0 , 0 ) ; self . order as usize + 1 ] ;
80+ if stop_flag. get ( ) >= 1 {
81+ return
82+ } ;
7483
7584 // This is checking if the approximation can step forward so takes the next iteration
7685 next_coefficients[ 0 ] = center_reference. reference_data [ i] . z_extended ;
@@ -98,11 +107,12 @@ impl SeriesApproximation {
98107
99108 previous_coefficients = next_coefficients. clone ( ) ;
100109
101-
110+ series_approximation_counter. inc ( ) ;
111+
102112 // only every 100th iteration (101, 201 etc)
103113 // This is 0, 100, 200 -> 1, 101, 201
104114 if i % self . data_storage_interval == 0 {
105- self . coefficients . push ( next_coefficients) ;
115+ self . coefficients . push ( next_coefficients. clone ( ) ) ;
106116 }
107117
108118 // self.coefficients.push(next_coefficients);
@@ -117,7 +127,8 @@ impl SeriesApproximation {
117127 delta_pixel : f64 ,
118128 image_width : usize ,
119129 image_height : usize ,
120- center_reference : & Reference ) {
130+ center_reference : & Reference ,
131+ series_validation_counter : & Arc < RelaxedCounter > ) {
121132 // Delete the previous probes and calculate new ones
122133 self . probe_start = Vec :: new ( ) ;
123134 self . approximation_probes = Vec :: new ( ) ;
@@ -203,6 +214,8 @@ impl SeriesApproximation {
203214 first_valid_iterations += 1 ;
204215 }
205216
217+ series_validation_counter. inc ( ) ;
218+
206219 self . min_valid_iteration = first_valid_iterations;
207220
208221 // println!("{}", self.min_valid_iteration);
@@ -260,6 +273,8 @@ impl SeriesApproximation {
260273 derivative_probe += next_coefficients[ k] * self . approximation_probes_derivative [ i] [ k - 1 ] ;
261274 } ;
262275
276+ probe. reduce ( ) ;
277+
263278 let relative_error = ( probe - series_probe) . norm_square ( ) ;
264279 let mut derivative = derivative_probe. norm_square ( ) ;
265280
@@ -269,10 +284,14 @@ impl SeriesApproximation {
269284 derivative. exponent = 0 ;
270285 }
271286
287+ // println!("checking at: {}", *probe_iteration_level);
288+ // println!("relative error: {} derivative: {} delta_square: {}", relative_error, derivative, self.delta_pixel_square);
289+ // println!("probe: {} series_probe: {}", probe, series_probe);
290+
272291 // The first element is reduced, the second might need to be reduced a little more
273292 // Check that the error over the derivative is less than the pixel spacing
274- if relative_error / derivative > self . delta_pixel_square {
275- // println!("{} ", *probe_iteration_level);
293+ if relative_error / derivative > self . delta_pixel_square || relative_error . exponent > 0 {
294+ // println!("exceeded at: {} ", *probe_iteration_level);
276295
277296 if * probe_iteration_level <= ( current_probe_check_value + self . data_storage_interval + 1 ) {
278297 * probe_iteration_level = next_probe_check_value;
@@ -322,6 +341,8 @@ impl SeriesApproximation {
322341 }
323342 }
324343
344+ series_validation_counter. inc ( ) ;
345+
325346 self . valid_iterations = valid_iterations;
326347
327348 // Also, here we do the interpolation and set up the array
@@ -341,7 +362,20 @@ impl SeriesApproximation {
341362 }
342363 }
343364
344- // println!("{:?}", self.valid_interpolation);
365+ self . max_valid_iteration = self . valid_interpolation . iter ( ) . max ( ) . unwrap ( ) . clone ( ) ;
366+
367+ // println!("series approximation valid interpolation buffer:");
368+ // let temp_size = self.probe_sampling - 1;
369+ // for i in 0..temp_size {
370+ // let test = &self.valid_interpolation[(i * temp_size)..((i + 1) * temp_size)];
371+ // print!("[");
372+
373+ // for element in test {
374+ // print!("{:>8},", element);
375+ // }
376+
377+ // print!("\x08]\n");
378+ // }
345379
346380 if !self . experimental {
347381 self . valid_interpolation = vec ! [ self . min_valid_iteration; ( self . probe_sampling - 1 ) * ( self . probe_sampling - 1 ) ] ;
0 commit comments