1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use ndarray::Array;
use ndarray::Axis;
use ndarray::Dim;
use ndarray::Zip;
#[derive(Debug)]
pub struct Lambert {
pub x_plane: Array<f64, Dim<[usize; 2]>>,
pub z_plane: Array<f64, Dim<[usize; 2]>>,
pub r_plane: f64,
pub x: Array<f64, Dim<[usize; 2]>>,
pub y: Array<f64, Dim<[usize; 2]>>,
pub z: Array<f64, Dim<[usize; 2]>>,
}
pub fn create_lambert_equal_area_gridpoint(
sphere_points: usize,
hemisphere: String,
) -> Result<Lambert, Box<dyn std::error::Error>> {
let r_plane: f64 = 2.0_f64.sqrt();
let x_plane = Array::linspace(-r_plane, r_plane, sphere_points);
let y_plane = x_plane.clone();
let (x_plane, mut z_plane) = create_meshgrid(&x_plane, &y_plane)?;
z_plane.invert_axis(Axis(0));
let mut x = Array::zeros([sphere_points, sphere_points]);
let mut y = Array::zeros([sphere_points, sphere_points]);
let mut z = Array::zeros([sphere_points, sphere_points]);
let mut mag = Array::zeros([sphere_points, sphere_points]);
Zip::from(&mut x)
.and(&x_plane)
.and(&z_plane)
.par_apply(|a, &x_plane, &z_plane| {
*a = if 1. - (x_plane * x_plane + z_plane * z_plane) / 4. > std::f64::EPSILON {
((1. - (x_plane * x_plane + z_plane * z_plane) / 4.).abs()).sqrt() * x_plane
} else {
0.
};
});
if hemisphere == "lower" {
Zip::from(&mut y)
.and(&x_plane)
.and(&z_plane)
.par_apply(|a, &x_plane, &z_plane| {
*a = -(1. - (x_plane * x_plane + z_plane * z_plane) / 2.);
});
Zip::from(&mut z)
.and(&x_plane)
.and(&z_plane)
.par_apply(|a, &x_plane, &z_plane| {
*a =
((1. - (x_plane * x_plane + z_plane * z_plane) / 4.).abs()).sqrt() * (-z_plane);
});
} else if hemisphere == "upper" {
Zip::from(&mut y)
.and(&x_plane)
.and(&z_plane)
.par_apply(|a, &x_plane, &z_plane| {
*a = 1. - (x_plane * x_plane + z_plane * z_plane) / 2.;
});
Zip::from(&mut z)
.and(&x_plane)
.and(&z_plane)
.par_apply(|a, &x_plane, &z_plane| {
*a = ((1. - (x_plane * x_plane + z_plane * z_plane) / 4.).abs()).sqrt() * z_plane;
});
};
Zip::from(&mut mag)
.and(&x)
.and(&y)
.and(&z)
.par_apply(|a, &x, &y, &z| {
*a = (x * x + y * y + z * z).sqrt();
});
let mag = mag;
x = x / &mag;
y = y / &mag;
z = z / &mag;
Ok(Lambert {
x_plane: x_plane,
z_plane: z_plane,
r_plane: r_plane,
x: x,
y: y,
z: z,
})
}
fn create_meshgrid(
x_plane: &Array<f64, Dim<[usize; 1]>>,
y_plane: &Array<f64, Dim<[usize; 1]>>,
) -> Result<(Array<f64, Dim<[usize; 2]>>, Array<f64, Dim<[usize; 2]>>), Box<dyn std::error::Error>>
{
let mut new_x: Array<f64, Dim<[usize; 2]>> = Array::zeros([x_plane.len(), y_plane.len()]);
let mut new_y: Array<f64, Dim<[usize; 2]>> = Array::zeros([x_plane.len(), y_plane.len()]);
let mut counter = 0;
let max_count = x_plane.len();
for value in new_x.iter_mut() {
*value = x_plane[counter];
counter += 1;
if counter >= max_count {
counter = 0;
}
}
counter = 0;
let mut counter_y = 0;
for value in new_y.iter_mut() {
*value = y_plane[counter];
counter_y += 1;
if counter_y >= max_count {
counter_y = 0;
counter += 1;
}
}
Ok((new_x, new_y))
}