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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use crate::arguments::{Args, Reference};
use crate::atoms::Atoms;
use crate::io::cube::Cube;
use crate::io::vasp::Vasp;
/// File I/O for the gaussian cube format.
pub mod cube;
/// Write analysis files.
pub mod output;
/// Custom BufReader.
pub mod reader;
/// File I/O for the VASP file format.
pub mod vasp;
macro_rules! define_file_format {
// 1. HELPER: Generates the Match Block
(@match_block $self:ident, $method:ident, $args:tt, ($($variant:ident),*) ) => {
match $self {
$(
// We just paste $args here. Since it includes the parentheses '()',
// it forms a valid function call: inner.read(a, b)
Self::$variant(inner) => inner.$method $args,
)*
}
};
// 2. HELPER: Recursive Method Implementer
// Base Case: No more methods
(@implement_required
enum: $enum_name:ident,
variants: [ $($variant:ident),* ],
methods: []
) => {};
// Recursive Case: Process Head, recurse on Tail
(@implement_required
enum: $enum_name:ident,
variants: [ $($variant:ident),* ],
methods: [
(
$(#[$req_fn_attr:meta])*
fn $req_fn_name:ident( &self $(, $req_arg_name:ident : $req_arg_type:ty )* $(,)? ) -> $req_ret:ty;
)
$($rest:tt)*
]
) => {
// Implement the current method
fn $req_fn_name(&self $(, $req_arg_name : $req_arg_type )* ) -> $req_ret {
// Call the match block helper.
// note: We pass the args wrapped in (...) so they match $args:tt
define_file_format!(@match_block self, $req_fn_name, ($($req_arg_name),*), ($($variant),*) )
}
// Recurse for the rest
define_file_format!(@implement_required
enum: $enum_name,
variants: [ $($variant),* ],
methods: [ $($rest)* ]
);
};
// MAIN ENTRY POINT
(
enum $enum_name:ident { $( $variant:ident ),* $(,)? }
$(#[$trait_attr:meta])*
$vis:vis trait $trait_name:ident {
// Group A: Required Methods
required {
$(
$(#[$req_fn_attr:meta])*
fn $req_fn_name:ident( &self $(, $req_arg_name:ident : $req_arg_type:ty )* $(,)? ) -> $req_ret:ty;
)*
}
// Group B: Provided Methods (Shared Logic)
$(
provided {
$(
$(#[$prov_fn_attr:meta])*
// Capture args as 'tt' to preserve 'self' hygiene contexts
fn $prov_fn_name:ident $args:tt -> $prov_ret:ty
{ $($prov_body:tt)* }
)*
}
)?
}
) => {
// A. Trait Definition
$(#[$trait_attr])* $vis trait $trait_name {
// 1. Required Signatures
$(
$(#[$req_fn_attr])*
fn $req_fn_name(&self $(, $req_arg_name : $req_arg_type )* ) -> $req_ret;
)*
// 2. Provided Bodies
$(
$(
$(#[$prov_fn_attr])*
fn $prov_fn_name $args -> $prov_ret {
$($prov_body)*
}
)*
)?
}
// B. Enum Implementation
impl $trait_name for $enum_name {
define_file_format!(@implement_required
enum: $enum_name,
variants: [ $($variant),* ],
methods: [
$(
(
$(#[$req_fn_attr])*
fn $req_fn_name( &self $(, $req_arg_name : $req_arg_type )* ) -> $req_ret;
)
)*
]
);
}
};
}
/// Indicates the available file types of the density file.
#[derive(Clone, Copy)]
pub enum FileType {
/// CHGCAR, CHG and PARCHG.
Vasp(Vasp),
/// Guassian, CP2K etc.
Cube(Cube),
}
define_file_format! {
enum FileType { Vasp, Cube }
/// FileFormat trait. Used for handling input from a file.
pub trait FileFormat {
required {
/// Reads the file into a [`ReadFunction`] containing the information
/// needed from the file to build a [`Grid`](crate::grid::Grid).
///
/// * `filename`: The name of the file to read.
fn read(&self, filename: String) -> ReadFunction;
/// Reads the non-density section of the file into an [`Atoms`] object.
///
/// * `atom_text`: The full string of non-density information from the
/// density file.
fn to_atoms(&self, atom_text: String) -> Atoms;
/// Writes a specific density, data, to tile in the correct format.
///
/// * `atoms`: The associated &[`Atoms`] object for the density file.
/// * `data`: The density to write to file wrapped in options with None representing 0.
/// * `filename`: Where to save the file, minus any suffix as this should
/// be applied in the function.
/// * `pbar`: A progress bar for monitoring the write.
fn write(
&self,
atoms: &Atoms,
data: Vec<Option<f64>>,
filename: String,
visible_pbar: bool,
) -> std::io::Result<()>;
/// How the format the positions of maxima and atoms
///
/// * `coords`: The 3d representation of the position.
fn coordinate_format(&self, coords: [f64; 3]) -> [f64; 3];
}
provided {
/// Returns the parts required to build [`Grid`](crate::grid::Grid) and [`Atoms`] structures.
///
/// * `args`: [`Args`] parsed from the command line.
fn init(&self, args: &Args) -> InitReturn {
let (voxel_origin, grid, atoms, mut densities) =
match self.read(args.file.clone()) {
Ok(x) => x,
Err(e) => panic!("Error: Problem reading file.\n{}", e),
};
if let Some(x) = args.spin.clone() {
match densities.len() {
1 => {
let (_, g, _, d) = match self.read(x.clone()) {
Ok(r) => r,
Err(e) => panic!("{}", e),
};
if 1 != d.len() {
panic!(
"Number of densities in original file is not 1.
Ambiguous how to handle spin density when {} contains {} densities.",
x,
d.len()
);
}
assert_eq!(
g, grid,
"Error: Spin density has different grid size."
);
densities.push(d[0].clone());
}
x => panic!(
"Number of densities in original file is not 1.
Ambiguous how to handle new spin when {} already has {} spin densities.",
args.file,
x - 1
),
}
}
let rho = match args.reference.clone() {
Reference::None => Vec::with_capacity(0),
Reference::One(f) => {
let (_, g, _, densities) = match self.read(f) {
Ok(r) => r,
Err(e) => panic!("{}", e),
};
assert_eq!(
g, grid,
"Error: Reference density has different grid size."
);
densities[0].clone()
}
Reference::Two(f1, f2) => {
let (_, g, _, densities) = match self.read(f1) {
Ok(r) => r,
Err(e) => panic!("{}", e),
};
assert_eq!(
g, grid,
"Error: Reference density has different grid size."
);
let (_, g2, _, densities2) = match self.read(f2) {
Ok(r) => r,
Err(e) => panic!("{}", e),
};
assert_eq!(
g2, grid,
"Error: Reference density has different grid size."
);
densities[0]
.iter()
.zip(&densities2[0])
.map(|(a, b)| a + b)
.collect::<Vec<f64>>()
}
};
(densities, rho, atoms, grid, voxel_origin)
}
}
}
}
/// What type of density to write.
pub enum WriteType {
/// Write a Bader Atom.
Atom(Vec<isize>),
/// Don't write anything.
None,
}
/// Turn a float into fortran "scientific" notation (leading digit is zero).
pub struct FortranFormat {
/// The float to convert to a string. Wrapped in an option as we need to log
/// so `0f64` should be stored as None.
float: Option<f64>,
/// A value to multiply the float by before printing, eg. a volume.
mult: f64,
}
impl std::fmt::Display for FortranFormat {
/// Format the structure into a fortran style exponential.
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
let prec = formatter.precision().unwrap_or(6);
match self.float {
None => {
write!(formatter, " 0.{:0<width$}E{:+03}", 0, 0, width = prec)
}
Some(f) => {
let float = f * self.mult;
let exponant = float.log10() as i32 + 1;
let decimals = float.abs() * 10f64.powi(prec as i32 - exponant);
let decimals = decimals.round() as usize;
if float.is_sign_negative() {
write!(
formatter,
"-0.{:0<width$}E{:+03}",
decimals,
exponant,
width = prec
)
} else {
write!(
formatter,
" 0.{:0<width$}E{:+03}",
decimals,
exponant,
width = prec
)
}
}
}
}
}
/// Return type of the read function in FileFormat.
pub type ReadFunction =
std::io::Result<([f64; 3], [usize; 3], Atoms, Vec<Vec<f64>>)>;
/// Return type of the init function in FileFormat.
type InitReturn = (Vec<Vec<f64>>, Vec<f64>, Atoms, [usize; 3], [f64; 3]);