xtalforge-cli 1.3.0

Command-line interface for xtalforge.
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! The `build` command family (graphene, nanotubes and fullerenes).

use clap::Subcommand;
use serde_json::json;

use crate::format::StructureFormat;
use crate::{Cli, CliError, output};

#[derive(Debug, Subcommand)]
pub(crate) enum BuildAction {
    /// Build an AA-stacked graphene sheet.
    Graphene {
        /// Repeats along a.
        nx: u32,
        /// Repeats along b.
        ny: u32,
        /// Number of layers.
        #[arg(long, default_value_t = 1)]
        layers: u32,
        /// Interlayer spacing in Angstrom.
        #[arg(long, default_value_t = 3.35)]
        interlayer: f64,
        /// Vacuum above the stack in Angstrom.
        #[arg(long, default_value_t = 15.0)]
        vacuum: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a C20 dodecahedron.
    C20 {
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a C60 fullerene.
    C60 {
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build two graphene layers with a relative twist.
    Twist {
        /// Repeats along a.
        n: u32,
        /// Repeats along b.
        m: u32,
        /// Requested twist angle in degrees.
        theta: f64,
        /// Number of layers.
        #[arg(long, default_value_t = 2)]
        layers: u32,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a honeycomb nanoribbon with vacuum.
    Ribbon {
        /// Width in repeat units.
        width: u32,
        /// Length in repeat units.
        length: u32,
        /// Vacuum on both sides in Angstrom.
        #[arg(long, default_value_t = 12.0)]
        vacuum: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a gyroid ZTC carbon model.
    Ztc {
        /// Samples per cube axis.
        #[arg(long, default_value_t = 16)]
        samples: usize,
        /// Cubic cell side in Angstrom.
        #[arg(long, default_value_t = 12.0)]
        cell: f64,
        /// Level-set value.
        #[arg(long, default_value_t = 0.0)]
        isovalue: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a Schwarz P carbon model.
    Schwarzite {
        /// Samples per cube axis.
        #[arg(long, default_value_t = 16)]
        samples: usize,
        /// Cubic cell side in Angstrom.
        #[arg(long, default_value_t = 12.0)]
        cell: f64,
        /// Level-set value.
        #[arg(long, default_value_t = 0.0)]
        isovalue: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a disordered carbon from a recipe (dia or pcu net).
    Carbon {
        /// Net name (`dia` or `pcu`).
        #[arg(long, default_value = "dia")]
        net: String,
        /// Repeats along a, b and c.
        #[arg(long, default_value_t = 1)]
        repeat_x: usize,
        #[arg(long, default_value_t = 1)]
        repeat_y: usize,
        #[arg(long, default_value_t = 1)]
        repeat_z: usize,
        /// Carbon density in g/cm3.
        #[arg(long, default_value_t = 0.42)]
        density: f64,
        /// Strut radius over the mean net edge.
        #[arg(long, default_value_t = 0.25)]
        radius_ratio: f64,
        /// Fraction of the closed sheet kept.
        #[arg(long, default_value_t = 0.38)]
        coverage: f64,
        /// Deterministic seed.
        #[arg(long, default_value_t = 0)]
        seed: u64,
        /// Hydrogen per carbon.
        #[arg(long, default_value_t = 0.07)]
        hydrogen: f64,
        /// Fluorine per carbon.
        #[arg(long, default_value_t = 0.29)]
        fluorine: f64,
        /// Oxygen per carbon.
        #[arg(long, default_value_t = 0.044)]
        oxygen: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a polymer chain from a repeat-unit SMILES.
    Polymer {
        /// Repeat-unit SMILES (`C=C` or `C=Cc1ccccc1`).
        smiles: String,
        /// Number of monomers.
        monomers: u32,
        /// Target density in gram per cubic centimetre.
        #[arg(long, default_value_t = 1.0)]
        density: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
    /// Build a chiral nanotube.
    Nanotube {
        /// Chiral index n.
        n: i32,
        /// Chiral index m.
        m: i32,
        /// Number of translation cells.
        #[arg(long, default_value_t = 1)]
        length: u32,
        /// Vacuum around the tube in Angstrom.
        #[arg(long, default_value_t = 10.0)]
        vacuum: f64,
        /// Output path; defaults to standard output.
        #[arg(long, value_name = "PATH")]
        out: Option<String>,
        /// Output format; defaults to the output extension or CIF.
        #[arg(long, value_enum)]
        format: Option<StructureFormat>,
    },
}

/// Options for the graphene builder.
pub(crate) struct GrapheneOptions<'a> {
    /// Repeats along a.
    pub nx: u32,
    /// Repeats along b.
    pub ny: u32,
    /// Number of layers.
    pub layers: u32,
    /// Interlayer spacing.
    pub interlayer: f64,
    /// Vacuum above the stack.
    pub vacuum: f64,
    /// Output path.
    pub out: Option<&'a str>,
    /// Output format.
    pub format: Option<StructureFormat>,
}

/// Builds a graphene sheet or stack.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn graphene(cli: &Cli, options: GrapheneOptions<'_>) -> Result<(), CliError> {
    let structure = xtalforge::build::graphene(
        options.nx,
        options.ny,
        options.layers,
        options.interlayer,
        options.vacuum,
    )
    .map_err(CliError::core)?;
    output::emit_structure(cli, &structure, options.out, options.format)
}

/// Builds a nanotube.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn nanotube(
    cli: &Cli,
    n: i32,
    m: i32,
    length: u32,
    vacuum: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure = xtalforge::build::nanotube(n, m, length, vacuum).map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

/// Builds a C20 dodecahedron.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn c20(
    cli: &Cli,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure = xtalforge::build::face_spiral(20).map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

/// Builds two twisted graphene layers.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn twist(
    cli: &Cli,
    n: u32,
    m: u32,
    theta: f64,
    layers: u32,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure =
        xtalforge::build::commensurate_twist(n, m, theta, layers).map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

/// Builds a honeycomb nanoribbon.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn ribbon(
    cli: &Cli,
    width: u32,
    length: u32,
    vacuum: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure = xtalforge::build::nanoribbon(width, length, vacuum).map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

fn ztc_options(samples: usize, cell: f64, isovalue: f64) -> xtalforge::build::ZtcOptions {
    xtalforge::build::ZtcOptions {
        samples,
        cell,
        isovalue,
    }
}

/// Builds a gyroid ZTC carbon model.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn ztc(
    cli: &Cli,
    samples: usize,
    cell: f64,
    isovalue: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure = xtalforge::build::build_ztc(&ztc_options(samples, cell, isovalue))
        .map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

/// Builds a Schwarz P carbon model.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn schwarzite(
    cli: &Cli,
    samples: usize,
    cell: f64,
    isovalue: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure = xtalforge::build::build_schwarzite(&ztc_options(samples, cell, isovalue))
        .map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

/// Builds a polymer chain and records its steps.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn polymer(
    cli: &Cli,
    smiles: &str,
    monomers: u32,
    density: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let build = xtalforge::build::build_polymer(
        smiles,
        monomers,
        &xtalforge::build::PolymerOptions { density },
    )
    .map_err(CliError::core)?;
    if out.is_none() && cli.json {
        println!(
            "{}",
            serde_json::json!({
                "structure": build.structure,
                "steps": build.steps,
            })
        );
        return Ok(());
    }
    output::emit_structure(cli, &build.structure, out, format)?;
    if !cli.quiet && !cli.json {
        for step in &build.steps {
            println!("step: {step}");
        }
    }
    Ok(())
}

/// Builds a C60 fullerene.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
pub(crate) fn c60(
    cli: &Cli,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let structure = xtalforge::build::fullerene(60).map_err(CliError::core)?;
    output::emit_structure(cli, &structure, out, format)
}

/// Adds the missing hydrogens.
///
/// # Errors
///
/// Returns [`CliError`] for a read or builder failure.
pub(crate) fn hydrogens(
    cli: &Cli,
    path: &str,
    distance: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    use xtalforge::build::hydrogens::{HydrogenOptions, add_hydrogens};
    let structure = crate::input::read_structure_path(path).map_err(CliError::core)?;
    let filled = add_hydrogens(
        &structure,
        &HydrogenOptions {
            distance,
            ..Default::default()
        },
    )
    .map_err(CliError::core)?;
    output::emit_structure(cli, &filled, out, format)
}

/// Builds a disordered carbon from a recipe.
///
/// # Errors
///
/// Returns [`CliError`] for a builder or output failure.
#[allow(clippy::too_many_arguments)] // one flag per recipe field
pub(crate) fn carbon(
    cli: &Cli,
    net: &str,
    repeat: [usize; 3],
    density: f64,
    radius_ratio: f64,
    coverage: f64,
    seed: u64,
    hydrogen: f64,
    fluorine: f64,
    oxygen: f64,
    out: Option<&str>,
    format: Option<StructureFormat>,
) -> Result<(), CliError> {
    let recipe = xtalforge::build::CarbonRecipe {
        net: net.to_owned(),
        repeat,
        density,
        radius_ratio,
        coverage,
        seed,
        hydrogen,
        fluorine,
        oxygen,
        ..xtalforge::build::CarbonRecipe::default()
    };
    let build = xtalforge::build::build_carbon(&recipe).map_err(CliError::core)?;
    if cli.json {
        println!("{}", json!(build));
        return Ok(());
    }
    println!(
        "{} atoms, {:.3} g/cm3, mean C-C {:.3} A, {} piece(s), periodicity {}",
        build.structure.sites().len(),
        build.density,
        build.mean_bond,
        build.pieces,
        build.periodicity
    );
    for note in &build.notes {
        println!("note: {note}");
    }
    output::emit_structure(cli, &build.structure, out, format)
}