|
1 | 1 | //! TODO |
2 | 2 |
|
3 | 3 | use super::sparse_array3::SparseArray3; |
4 | | -use super::subgrid::{Mu2, Stats, Subgrid, SubgridEnum, SubgridIndexedIter}; |
| 4 | +use super::subgrid::{Mu2, Subgrid, SubgridIndexedIter}; |
5 | 5 | use serde::{Deserialize, Serialize}; |
6 | 6 | use std::borrow::Cow; |
7 | | -use std::mem; |
8 | 7 |
|
9 | 8 | /// TODO |
10 | 9 | #[derive(Clone, Deserialize, Serialize)] |
@@ -59,104 +58,9 @@ impl Subgrid for ImportOnlySubgridV1 { |
59 | 58 | self.array.is_empty() |
60 | 59 | } |
61 | 60 |
|
62 | | - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { |
63 | | - if let SubgridEnum::ImportOnlySubgridV1(other_grid) = other { |
64 | | - if self.array.is_empty() && !transpose { |
65 | | - mem::swap(&mut self.array, &mut other_grid.array); |
66 | | - } else { |
67 | | - // TODO: the general case isn't implemented |
68 | | - assert!(self.x1_grid() == other_grid.x1_grid()); |
69 | | - assert!(self.x2_grid() == other_grid.x2_grid()); |
70 | | - |
71 | | - for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() { |
72 | | - // the following should always be the case |
73 | | - assert_eq!(mu2.ren, mu2.fac); |
74 | | - let q2 = &mu2.ren; |
75 | | - |
76 | | - let index = match self |
77 | | - .q2_grid |
78 | | - .binary_search_by(|val| val.partial_cmp(q2).unwrap()) |
79 | | - { |
80 | | - Ok(index) => index, |
81 | | - Err(index) => { |
82 | | - self.q2_grid.insert(index, *q2); |
83 | | - self.array.increase_x_at(index); |
84 | | - index |
85 | | - } |
86 | | - }; |
87 | | - |
88 | | - for ((_, j, k), value) in other_grid |
89 | | - .array |
90 | | - .indexed_iter() |
91 | | - .filter(|&((i, _, _), _)| i == other_index) |
92 | | - { |
93 | | - let (j, k) = if transpose { (k, j) } else { (j, k) }; |
94 | | - self.array[[index, j, k]] += value; |
95 | | - } |
96 | | - } |
97 | | - } |
98 | | - } else { |
99 | | - todo!(); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - fn scale(&mut self, factor: f64) { |
104 | | - if factor == 0.0 { |
105 | | - self.array.clear(); |
106 | | - } else { |
107 | | - self.array.iter_mut().for_each(|x| *x *= factor); |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - fn symmetrize(&mut self) { |
112 | | - let mut new_array = |
113 | | - SparseArray3::new(self.q2_grid.len(), self.x1_grid.len(), self.x2_grid.len()); |
114 | | - |
115 | | - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k >= j) { |
116 | | - new_array[[i, j, k]] = sigma; |
117 | | - } |
118 | | - // do not change the diagonal entries (k==j) |
119 | | - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k < j) { |
120 | | - new_array[[i, k, j]] += sigma; |
121 | | - } |
122 | | - |
123 | | - mem::swap(&mut self.array, &mut new_array); |
124 | | - } |
125 | | - |
126 | | - fn clone_empty(&self) -> SubgridEnum { |
127 | | - Self { |
128 | | - array: SparseArray3::new(self.q2_grid.len(), self.x1_grid.len(), self.x2_grid.len()), |
129 | | - q2_grid: self.q2_grid.clone(), |
130 | | - x1_grid: self.x1_grid.clone(), |
131 | | - x2_grid: self.x2_grid.clone(), |
132 | | - } |
133 | | - .into() |
134 | | - } |
135 | | - |
136 | 61 | fn indexed_iter(&self) -> SubgridIndexedIter<'_> { |
137 | 62 | Box::new(self.array.indexed_iter()) |
138 | 63 | } |
139 | | - |
140 | | - fn stats(&self) -> Stats { |
141 | | - Stats { |
142 | | - total: self.q2_grid.len() * self.x1_grid.len() * self.x2_grid.len(), |
143 | | - allocated: self.array.len() + self.array.zeros(), |
144 | | - zeros: self.array.zeros(), |
145 | | - overhead: self.array.overhead(), |
146 | | - bytes_per_value: mem::size_of::<f64>(), |
147 | | - } |
148 | | - } |
149 | | - |
150 | | - fn static_scale(&self) -> Option<Mu2> { |
151 | | - if let &[static_scale] = self.q2_grid.as_slice() { |
152 | | - Some(Mu2 { |
153 | | - ren: static_scale, |
154 | | - fac: static_scale, |
155 | | - }) |
156 | | - } else { |
157 | | - None |
158 | | - } |
159 | | - } |
160 | 64 | } |
161 | 65 |
|
162 | 66 | /// TODO |
@@ -208,192 +112,7 @@ impl Subgrid for ImportOnlySubgridV2 { |
208 | 112 | self.array.is_empty() |
209 | 113 | } |
210 | 114 |
|
211 | | - fn merge(&mut self, other: &mut SubgridEnum, transpose: bool) { |
212 | | - if let SubgridEnum::ImportOnlySubgridV2(other_grid) = other { |
213 | | - if self.array.is_empty() && !transpose { |
214 | | - mem::swap(&mut self.array, &mut other_grid.array); |
215 | | - } else { |
216 | | - let rhs_x1 = if transpose { |
217 | | - other_grid.x2_grid() |
218 | | - } else { |
219 | | - other_grid.x1_grid() |
220 | | - }; |
221 | | - let rhs_x2 = if transpose { |
222 | | - other_grid.x1_grid() |
223 | | - } else { |
224 | | - other_grid.x2_grid() |
225 | | - }; |
226 | | - |
227 | | - if (self.x1_grid() != rhs_x1) || (self.x2_grid() != rhs_x2) { |
228 | | - let mut x1_grid = self.x1_grid.clone(); |
229 | | - let mut x2_grid = self.x2_grid.clone(); |
230 | | - |
231 | | - x1_grid.extend_from_slice(&rhs_x1); |
232 | | - x1_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); |
233 | | - x1_grid.dedup(); |
234 | | - x2_grid.extend_from_slice(&rhs_x2); |
235 | | - x2_grid.sort_by(|a, b| a.partial_cmp(b).unwrap()); |
236 | | - x2_grid.dedup(); |
237 | | - |
238 | | - let mut array = |
239 | | - SparseArray3::new(self.array.dimensions().0, x1_grid.len(), x2_grid.len()); |
240 | | - |
241 | | - for ((i, j, k), value) in self.array.indexed_iter() { |
242 | | - let target_j = x1_grid |
243 | | - .iter() |
244 | | - .position(|&x| x == self.x1_grid[j]) |
245 | | - .unwrap_or_else(|| unreachable!()); |
246 | | - let target_k = x2_grid |
247 | | - .iter() |
248 | | - .position(|&x| x == self.x2_grid[k]) |
249 | | - .unwrap_or_else(|| unreachable!()); |
250 | | - |
251 | | - array[[i, target_j, target_k]] = value; |
252 | | - } |
253 | | - |
254 | | - self.array = array; |
255 | | - self.x1_grid = x1_grid; |
256 | | - self.x2_grid = x2_grid; |
257 | | - } |
258 | | - |
259 | | - for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() { |
260 | | - let index = match self |
261 | | - .mu2_grid |
262 | | - .binary_search_by(|val| val.partial_cmp(mu2).unwrap()) |
263 | | - { |
264 | | - Ok(index) => index, |
265 | | - Err(index) => { |
266 | | - self.mu2_grid.insert(index, mu2.clone()); |
267 | | - self.array.increase_x_at(index); |
268 | | - index |
269 | | - } |
270 | | - }; |
271 | | - |
272 | | - for ((_, j, k), value) in other_grid |
273 | | - .array |
274 | | - .indexed_iter() |
275 | | - .filter(|&((i, _, _), _)| i == other_index) |
276 | | - { |
277 | | - let (j, k) = if transpose { (k, j) } else { (j, k) }; |
278 | | - let target_j = self |
279 | | - .x1_grid |
280 | | - .iter() |
281 | | - .position(|&x| x == rhs_x1[j]) |
282 | | - .unwrap_or_else(|| unreachable!()); |
283 | | - let target_k = self |
284 | | - .x2_grid |
285 | | - .iter() |
286 | | - .position(|&x| x == rhs_x2[k]) |
287 | | - .unwrap_or_else(|| unreachable!()); |
288 | | - |
289 | | - self.array[[index, target_j, target_k]] += value; |
290 | | - } |
291 | | - } |
292 | | - } |
293 | | - } else { |
294 | | - todo!(); |
295 | | - } |
296 | | - } |
297 | | - |
298 | | - fn scale(&mut self, factor: f64) { |
299 | | - if factor == 0.0 { |
300 | | - self.array.clear(); |
301 | | - } else { |
302 | | - self.array.iter_mut().for_each(|x| *x *= factor); |
303 | | - } |
304 | | - } |
305 | | - |
306 | | - fn symmetrize(&mut self) { |
307 | | - let mut new_array = |
308 | | - SparseArray3::new(self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()); |
309 | | - |
310 | | - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k >= j) { |
311 | | - new_array[[i, j, k]] = sigma; |
312 | | - } |
313 | | - // do not change the diagonal entries (k==j) |
314 | | - for ((i, j, k), sigma) in self.array.indexed_iter().filter(|((_, j, k), _)| k < j) { |
315 | | - new_array[[i, k, j]] += sigma; |
316 | | - } |
317 | | - |
318 | | - mem::swap(&mut self.array, &mut new_array); |
319 | | - } |
320 | | - |
321 | | - fn clone_empty(&self) -> SubgridEnum { |
322 | | - Self { |
323 | | - array: SparseArray3::new(self.mu2_grid.len(), self.x1_grid.len(), self.x2_grid.len()), |
324 | | - mu2_grid: self.mu2_grid.clone(), |
325 | | - x1_grid: self.x1_grid.clone(), |
326 | | - x2_grid: self.x2_grid.clone(), |
327 | | - } |
328 | | - .into() |
329 | | - } |
330 | | - |
331 | 115 | fn indexed_iter(&self) -> SubgridIndexedIter<'_> { |
332 | 116 | Box::new(self.array.indexed_iter()) |
333 | 117 | } |
334 | | - |
335 | | - fn stats(&self) -> Stats { |
336 | | - Stats { |
337 | | - total: self.mu2_grid.len() * self.x1_grid.len() * self.x2_grid.len(), |
338 | | - allocated: self.array.len() + self.array.zeros(), |
339 | | - zeros: self.array.zeros(), |
340 | | - overhead: self.array.overhead(), |
341 | | - bytes_per_value: mem::size_of::<f64>(), |
342 | | - } |
343 | | - } |
344 | | - |
345 | | - fn static_scale(&self) -> Option<Mu2> { |
346 | | - if let [static_scale] = self.mu2_grid.as_slice() { |
347 | | - Some(static_scale.clone()) |
348 | | - } else { |
349 | | - None |
350 | | - } |
351 | | - } |
352 | | -} |
353 | | - |
354 | | -impl From<&SubgridEnum> for ImportOnlySubgridV2 { |
355 | | - fn from(subgrid: &SubgridEnum) -> Self { |
356 | | - // find smallest ranges |
357 | | - let (mu2_range, x1_range, x2_range) = subgrid.indexed_iter().fold( |
358 | | - ( |
359 | | - subgrid.mu2_grid().len()..0, |
360 | | - subgrid.x1_grid().len()..0, |
361 | | - subgrid.x2_grid().len()..0, |
362 | | - ), |
363 | | - |prev, ((imu2, ix1, ix2), _)| { |
364 | | - ( |
365 | | - prev.0.start.min(imu2)..prev.0.end.max(imu2 + 1), |
366 | | - prev.1.start.min(ix1)..prev.1.end.max(ix1 + 1), |
367 | | - prev.2.start.min(ix2)..prev.2.end.max(ix2 + 1), |
368 | | - ) |
369 | | - }, |
370 | | - ); |
371 | | - |
372 | | - let (mu2_grid, static_scale) = subgrid.static_scale().map_or_else( |
373 | | - || (subgrid.mu2_grid()[mu2_range.clone()].to_vec(), false), |
374 | | - |scale| (vec![scale], true), |
375 | | - ); |
376 | | - let x1_grid = subgrid.x1_grid()[x1_range.clone()].to_vec(); |
377 | | - let x2_grid = subgrid.x2_grid()[x2_range.clone()].to_vec(); |
378 | | - |
379 | | - let mut array = SparseArray3::new(mu2_grid.len(), x1_grid.len(), x2_grid.len()); |
380 | | - |
381 | | - for ((imu2, ix1, ix2), value) in subgrid.indexed_iter() { |
382 | | - // if there's a static scale we want every value to be added to same grid point |
383 | | - let index = if static_scale { |
384 | | - 0 |
385 | | - } else { |
386 | | - imu2 - mu2_range.start |
387 | | - }; |
388 | | - |
389 | | - array[[index, ix1 - x1_range.start, ix2 - x2_range.start]] += value; |
390 | | - } |
391 | | - |
392 | | - Self { |
393 | | - array, |
394 | | - mu2_grid, |
395 | | - x1_grid, |
396 | | - x2_grid, |
397 | | - } |
398 | | - } |
399 | 118 | } |
0 commit comments