@@ -7,6 +7,10 @@ use rayon::prelude::*;
7
7
8
8
use std:: cmp:: max;
9
9
10
+ use atomic_counter:: { AtomicCounter , RelaxedCounter } ;
11
+
12
+ use std:: sync:: Arc ;
13
+
10
14
pub struct SeriesApproximation {
11
15
pub maximum_iteration : usize ,
12
16
pub delta_pixel_square : FloatExtended ,
@@ -16,6 +20,7 @@ pub struct SeriesApproximation {
16
20
approximation_probes : Vec < Vec < ComplexExtended > > ,
17
21
approximation_probes_derivative : Vec < Vec < ComplexExtended > > ,
18
22
pub min_valid_iteration : usize ,
23
+ pub max_valid_iteration : usize ,
19
24
pub valid_iterations : Vec < usize > ,
20
25
pub valid_interpolation : Vec < usize > ,
21
26
pub probe_sampling : usize ,
@@ -45,6 +50,7 @@ impl SeriesApproximation {
45
50
approximation_probes : Vec :: new ( ) ,
46
51
approximation_probes_derivative : Vec :: new ( ) ,
47
52
min_valid_iteration : 1 ,
53
+ max_valid_iteration : 1 ,
48
54
valid_iterations : Vec :: new ( ) ,
49
55
valid_interpolation : Vec :: new ( ) ,
50
56
probe_sampling,
@@ -55,7 +61,7 @@ impl SeriesApproximation {
55
61
}
56
62
}
57
63
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 > ) {
59
65
// Reset the coefficients
60
66
self . coefficients = vec ! [ vec![ ComplexExtended :: new2( 0.0 , 0.0 , 0 ) ; self . order as usize + 1 ] ; 1 ] ;
61
67
@@ -66,11 +72,14 @@ impl SeriesApproximation {
66
72
let add_value = ComplexExtended :: new2 ( 1.0 , 0.0 , 0 ) ;
67
73
68
74
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 ] ;
69
76
70
77
// Can be changed later into a better loop - this function could also return some more information
71
78
// Go through all remaining iterations
72
79
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
+ } ;
74
83
75
84
// This is checking if the approximation can step forward so takes the next iteration
76
85
next_coefficients[ 0 ] = center_reference. reference_data [ i] . z_extended ;
@@ -98,11 +107,12 @@ impl SeriesApproximation {
98
107
99
108
previous_coefficients = next_coefficients. clone ( ) ;
100
109
101
-
110
+ series_approximation_counter. inc ( ) ;
111
+
102
112
// only every 100th iteration (101, 201 etc)
103
113
// This is 0, 100, 200 -> 1, 101, 201
104
114
if i % self . data_storage_interval == 0 {
105
- self . coefficients . push ( next_coefficients) ;
115
+ self . coefficients . push ( next_coefficients. clone ( ) ) ;
106
116
}
107
117
108
118
// self.coefficients.push(next_coefficients);
@@ -117,7 +127,8 @@ impl SeriesApproximation {
117
127
delta_pixel : f64 ,
118
128
image_width : usize ,
119
129
image_height : usize ,
120
- center_reference : & Reference ) {
130
+ center_reference : & Reference ,
131
+ series_validation_counter : & Arc < RelaxedCounter > ) {
121
132
// Delete the previous probes and calculate new ones
122
133
self . probe_start = Vec :: new ( ) ;
123
134
self . approximation_probes = Vec :: new ( ) ;
@@ -203,6 +214,8 @@ impl SeriesApproximation {
203
214
first_valid_iterations += 1 ;
204
215
}
205
216
217
+ series_validation_counter. inc ( ) ;
218
+
206
219
self . min_valid_iteration = first_valid_iterations;
207
220
208
221
// println!("{}", self.min_valid_iteration);
@@ -260,6 +273,8 @@ impl SeriesApproximation {
260
273
derivative_probe += next_coefficients[ k] * self . approximation_probes_derivative [ i] [ k - 1 ] ;
261
274
} ;
262
275
276
+ probe. reduce ( ) ;
277
+
263
278
let relative_error = ( probe - series_probe) . norm_square ( ) ;
264
279
let mut derivative = derivative_probe. norm_square ( ) ;
265
280
@@ -269,10 +284,14 @@ impl SeriesApproximation {
269
284
derivative. exponent = 0 ;
270
285
}
271
286
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
+
272
291
// The first element is reduced, the second might need to be reduced a little more
273
292
// 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);
276
295
277
296
if * probe_iteration_level <= ( current_probe_check_value + self . data_storage_interval + 1 ) {
278
297
* probe_iteration_level = next_probe_check_value;
@@ -322,6 +341,8 @@ impl SeriesApproximation {
322
341
}
323
342
}
324
343
344
+ series_validation_counter. inc ( ) ;
345
+
325
346
self . valid_iterations = valid_iterations;
326
347
327
348
// Also, here we do the interpolation and set up the array
@@ -341,7 +362,20 @@ impl SeriesApproximation {
341
362
}
342
363
}
343
364
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
+ // }
345
379
346
380
if !self . experimental {
347
381
self . valid_interpolation = vec ! [ self . min_valid_iteration; ( self . probe_sampling - 1 ) * ( self . probe_sampling - 1 ) ] ;
0 commit comments