|
| 1 | +use super::*; |
| 2 | + |
| 3 | +#[derive(Deserialize, Clone)] |
| 4 | +pub struct InputSphereInCuboid { |
| 5 | + pub options: Options, |
| 6 | + pub material_parameters: material::MaterialParameters, |
| 7 | + pub particle_parameters: particle::ParticleParameters, |
| 8 | + pub geometry_input: sphereincuboid::SphereInCuboidInput, |
| 9 | +} |
| 10 | + |
| 11 | +impl InputFile for InputSphereInCuboid { |
| 12 | + |
| 13 | + fn new(string: &str) -> InputSphereInCuboid { |
| 14 | + toml::from_str(string).context("Could not parse TOML file. Be sure you are using the correct input file mode (e.g., ./RustBCA SPHERE sphere.toml or RustBCA.exe 0D mesh_0d.toml).").unwrap() |
| 15 | + } |
| 16 | + |
| 17 | + fn get_options(&self) -> &Options{ |
| 18 | + &self.options |
| 19 | + } |
| 20 | + fn get_material_parameters(&self) -> &material::MaterialParameters{ |
| 21 | + &self.material_parameters |
| 22 | + } |
| 23 | + fn get_particle_parameters(&self) -> &particle::ParticleParameters{ |
| 24 | + &self.particle_parameters |
| 25 | + } |
| 26 | + fn get_geometry_input(&self) -> &Self::GeometryInput{ |
| 27 | + &self.geometry_input |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Deserialize, Clone)] |
| 32 | +pub struct SphereInCuboidInput { |
| 33 | + pub length_unit: String, |
| 34 | + |
| 35 | + pub sphere_radius: f64, |
| 36 | + pub sphere_densities: Vec<f64>, |
| 37 | + pub sphere_electronic_stopping_correction_factor: f64, |
| 38 | + |
| 39 | + pub cuboid_corners: Vec<(f64, f64, f64)>, |
| 40 | + pub cuboid_densities: Vec<f64>, |
| 41 | + pub cuboid_electronic_stopping_correction_factor: f64, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Clone)] |
| 45 | +pub struct SphereInCuboid { |
| 46 | + pub sphere_radius: f64, |
| 47 | + pub sphere_densities: Vec<f64>, |
| 48 | + pub sphere_concentrations: Vec<f64>, |
| 49 | + pub sphere_electronic_stopping_correction_factor: f64, |
| 50 | + |
| 51 | + pub cuboid_corners: Vec<(f64, f64, f64)>, |
| 52 | + pub cuboid_densities: Vec<f64>, |
| 53 | + pub cuboid_concentrations: Vec<f64>, |
| 54 | + pub cuboid_electronic_stopping_correction_factor: f64, |
| 55 | + |
| 56 | + pub energy_barrier_thickness: f64, |
| 57 | +} |
| 58 | + |
| 59 | +impl GeometryInput for InputSphereInCuboid { |
| 60 | + type GeometryInput = SphereInCuboidInput; |
| 61 | +} |
| 62 | + |
| 63 | +impl Geometry for SphereInCuboid { |
| 64 | + |
| 65 | + type InputFileFormat = InputSphereInCuboid; |
| 66 | + |
| 67 | + fn new(input: &<<Self as Geometry>::InputFileFormat as GeometryInput>::GeometryInput) -> SphereInCuboid { |
| 68 | + |
| 69 | + let length_unit: f64 = match input.length_unit.as_str() { |
| 70 | + "MICRON" => MICRON, |
| 71 | + "CM" => CM, |
| 72 | + "MM" => MM, |
| 73 | + "ANGSTROM" => ANGSTROM, |
| 74 | + "NM" => NM, |
| 75 | + "M" => 1., |
| 76 | + _ => input.length_unit.parse() |
| 77 | + .expect(format!( |
| 78 | + "Input errror: could nor parse length unit {}. Use a valid float or one of ANGSTROM, NM, MICRON, CM, MM, M", |
| 79 | + &input.length_unit.as_str() |
| 80 | + ).as_str()), |
| 81 | + }; |
| 82 | + |
| 83 | + let sphere_radius: f64 = input.sphere_radius*length_unit; |
| 84 | + let sphere_densities: Vec<f64> = input.sphere_densities |
| 85 | + .iter().map(|element| element/(length_unit).powi(3)).collect(); |
| 86 | + let sphere_total_density: f64 = sphere_densities.iter().sum(); |
| 87 | + let sphere_concentrations: Vec<f64> = sphere_densities |
| 88 | + .iter().map(|&density| density/sphere_total_density).collect::<Vec<f64>>(); |
| 89 | + let sphere_electronic_stopping_correction_factor: f64 = input.sphere_electronic_stopping_correction_factor; |
| 90 | + |
| 91 | + let cuboid_corners: Vec<(f64, f64, f64)> = input.cuboid_corners |
| 92 | + .iter().map( |
| 93 | + |&(x, y, z)| (x * length_unit, y*length_unit, z*length_unit) |
| 94 | + ).collect(); |
| 95 | + let cuboid_densities: Vec<f64> = input.cuboid_densities |
| 96 | + .iter().map(|element| element/(length_unit).powi(3)).collect(); |
| 97 | + let cuboid_total_density: f64 = cuboid_densities.iter().sum(); |
| 98 | + let cuboid_concentrations: Vec<f64> = cuboid_densities |
| 99 | + .iter().map(|&density| density/cuboid_total_density).collect::<Vec<f64>>(); |
| 100 | + let cuboid_electronic_stopping_correction_factor: f64 = input.cuboid_electronic_stopping_correction_factor; |
| 101 | + |
| 102 | + let energy_barrier_thickness: f64 = cuboid_total_density.powf(-1./3.)/SQRTPI*2.; |
| 103 | + |
| 104 | + SphereInCuboid { |
| 105 | + sphere_radius, |
| 106 | + sphere_densities, |
| 107 | + sphere_concentrations, |
| 108 | + sphere_electronic_stopping_correction_factor, |
| 109 | + |
| 110 | + cuboid_corners, |
| 111 | + cuboid_densities, |
| 112 | + cuboid_concentrations, |
| 113 | + cuboid_electronic_stopping_correction_factor, |
| 114 | + |
| 115 | + energy_barrier_thickness, |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn get_densities(&self, x: f64, y: f64, z: f64) -> &Vec<f64> { |
| 120 | + let r: f64 = (x.powi(2) + y.powi(2) + z.powi(2)).sqrt(); |
| 121 | + if r > self.sphere_radius { |
| 122 | + &self.cuboid_densities |
| 123 | + } else { |
| 124 | + &self.sphere_densities |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fn get_ck(&self, x: f64, y: f64, z: f64) -> f64 { |
| 129 | + let r: f64 = (x.powi(2) + y.powi(2) + z.powi(2)).sqrt(); |
| 130 | + if r > self.sphere_radius { |
| 131 | + self.cuboid_electronic_stopping_correction_factor |
| 132 | + } else { |
| 133 | + self.sphere_electronic_stopping_correction_factor |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + fn get_total_density(&self, x: f64, y: f64, z: f64) -> f64{ |
| 138 | + let r: f64 = (x.powi(2) + y.powi(2) + z.powi(2)).sqrt(); |
| 139 | + if r > self.sphere_radius { |
| 140 | + self.cuboid_densities.iter().sum() |
| 141 | + } else { |
| 142 | + self.sphere_densities.iter().sum() |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + fn get_concentrations(&self, x: f64, y: f64, z: f64) -> &Vec<f64> { |
| 147 | + let r: f64 = (x.powi(2) + y.powi(2) + z.powi(2)).sqrt(); |
| 148 | + if r > self.sphere_radius { |
| 149 | + &self.cuboid_concentrations |
| 150 | + } else { |
| 151 | + &self.sphere_concentrations |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + fn inside(&self, x: f64, y: f64, z: f64) -> bool { |
| 156 | + let (xlo, ylo, zlo): (f64, f64, f64) = self.cuboid_corners[0]; |
| 157 | + let (xhi, yhi, zhi): (f64, f64, f64) = self.cuboid_corners[1]; |
| 158 | + |
| 159 | + xlo <= x && x <= xhi && |
| 160 | + ylo <= y && y <= yhi && |
| 161 | + zlo <= z && z <= zhi |
| 162 | + } |
| 163 | + |
| 164 | + fn inside_simulation_boundary(&self, x: f64, y: f64, z: f64) -> bool { |
| 165 | + let (xlo, ylo, zlo): (f64, f64, f64) = self.cuboid_corners[0]; |
| 166 | + let (xhi, yhi, zhi): (f64, f64, f64) = self.cuboid_corners[1]; |
| 167 | + let ebt: f64 = self.energy_barrier_thickness; |
| 168 | + |
| 169 | + (xlo - 10.0 * ebt) <= x && x <= (xhi + 10.0 * ebt) && |
| 170 | + (ylo - 10.0 * ebt) <= y && y <= (yhi + 10.0 * ebt) && |
| 171 | + (zlo - 10.0 * ebt) <= z && z <= (zhi + 10.0 * ebt) |
| 172 | + } |
| 173 | + |
| 174 | + fn inside_energy_barrier(&self, x: f64, y: f64, z: f64) -> bool { |
| 175 | + let (xlo, ylo, zlo): (f64, f64, f64) = self.cuboid_corners[0]; |
| 176 | + let (xhi, yhi, zhi): (f64, f64, f64) = self.cuboid_corners[1]; |
| 177 | + let ebt: f64 = self.energy_barrier_thickness; |
| 178 | + |
| 179 | + (xlo - ebt) <= x && x <= (xhi + ebt) && |
| 180 | + (ylo - ebt) <= y && y <= (yhi + ebt) && |
| 181 | + (zlo - ebt) <= z && z <= (zhi + ebt) |
| 182 | + } |
| 183 | + |
| 184 | + fn closest_point(&self, x: f64, y: f64, z: f64) -> (f64, f64, f64) { |
| 185 | + let (xlo, ylo, zlo): (f64, f64, f64) = self.cuboid_corners[0]; |
| 186 | + let (xhi, yhi, zhi): (f64, f64, f64) = self.cuboid_corners[1]; |
| 187 | + |
| 188 | + if self.inside(x, y, z) { |
| 189 | + let faces: [((f64, f64, f64), f64); 6] = [ |
| 190 | + ((xlo, y, z), x - xlo), |
| 191 | + ((xhi, y, z), xhi - x), |
| 192 | + ((x, ylo, z), y - ylo), |
| 193 | + ((x, yhi, z), yhi - y), |
| 194 | + ((x, y, zlo), z - zlo), |
| 195 | + ((x, y, zhi), zhi - z), |
| 196 | + ]; |
| 197 | + |
| 198 | + let closest: ((f64, f64, f64), f64) = *faces |
| 199 | + .iter() |
| 200 | + .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) |
| 201 | + .unwrap(); |
| 202 | + closest.0 |
| 203 | + } else { |
| 204 | + (x.max(xlo).min(xhi), y.max(ylo).min(yhi), z.max(zlo).min(zhi)) |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments