castep_seeding/
lib.rs

1mod auxiliary;
2mod error;
3mod export;
4mod root;
5mod seed;
6
7pub use crate::root::RootJobs;
8pub use error::SeedingErrors;
9pub use seed::parse_cell_doc_from_path;
10pub use seed::seed_folder::SeedFolder;
11pub use seed::seed_setup::{CellBuilding, ParamBuilding};
12
13#[cfg(test)]
14mod test {
15    /// Example Usage
16    use crate::CellBuilding;
17    use crate::ParamBuilding;
18    use castep_cell_io::cell_document::CellDocument;
19    use castep_param_io::param::CastepParam;
20    use std::path::{Path, PathBuf};
21
22    use crate::{
23        seed::{parse_cell_doc_from_path, seed_folder::SeedFolder},
24        RootJobs, SeedingErrors,
25    };
26
27    struct RootFolder {
28        path: PathBuf,
29    }
30
31    impl RootFolder {
32        fn new<P: AsRef<Path>>(path: P) -> Self {
33            Self {
34                path: path.as_ref().into(),
35            }
36        }
37    }
38
39    struct Seed<P: AsRef<Path>> {
40        cell_path: P,
41        cell_doc: CellDocument,
42    }
43
44    impl<P: AsRef<Path>> Seed<P> {
45        fn from_cell_path(cell_path: P) -> Result<Self, SeedingErrors> {
46            let cell_doc = parse_cell_doc_from_path(&cell_path)?;
47            Ok(Self {
48                cell_path,
49                cell_doc,
50            })
51        }
52    }
53
54    impl<P> SeedFolder for Seed<P>
55    where
56        P: AsRef<Path> + std::marker::Send,
57    {
58        fn seed_name(&self) -> &str {
59            self.cell_path
60                .as_ref()
61                .file_stem()
62                .unwrap()
63                .to_str()
64                .unwrap()
65        }
66
67        fn root_dir(&self) -> impl AsRef<Path> {
68            self.cell_path.as_ref().parent().unwrap()
69        }
70
71        fn cell_template(&self) -> &CellDocument {
72            &self.cell_doc
73        }
74    }
75
76    impl RootJobs for RootFolder {
77        fn root_path(&self) -> impl AsRef<Path> {
78            &self.path
79        }
80
81        fn generate_seed_folders(&self) -> Result<Vec<impl SeedFolder>, crate::SeedingErrors> {
82            self.get_cell_paths()?
83                .into_iter()
84                .map(Seed::from_cell_path)
85                .collect()
86        }
87    }
88
89    struct Configurator {
90        use_edft: bool,
91        potentials_loc: PathBuf,
92    }
93
94    impl Configurator {
95        fn new<P: AsRef<Path>>(use_edft: bool, potentials_loc: P) -> Self {
96            Self {
97                use_edft,
98                potentials_loc: potentials_loc.as_ref().into(),
99            }
100        }
101    }
102
103    impl CellBuilding for Configurator {}
104
105    impl ParamBuilding for Configurator {
106        fn build_param_for_task(
107            &self,
108            template_cell: &CellDocument,
109            castep_task: castep_cell_io::CastepTask,
110        ) -> Result<CastepParam, SeedingErrors> {
111            match castep_task {
112                castep_cell_io::CastepTask::BandStructure => self.dos_param_template(
113                    template_cell,
114                    castep_cell_io::EnergyCutoff::Ultrafine,
115                    self.use_edft,
116                    &self.potentials_loc,
117                ),
118                castep_cell_io::CastepTask::GeometryOptimization => self.geom_opt_param_template(
119                    template_cell,
120                    castep_cell_io::EnergyCutoff::Ultrafine,
121                    self.use_edft,
122                    &self.potentials_loc,
123                ),
124            }
125        }
126    }
127
128    #[test]
129    fn execution() {
130        let test_folder = RootFolder::new("test/Bi2Te3_001_Fe_2.2");
131        let potentials_loc = "/Users/tonywu/Downloads/Potentials";
132        let config = Configurator::new(true, potentials_loc);
133        match test_folder.build_all(&config, &config, potentials_loc) {
134            Ok(()) => {
135                println!("Success")
136            }
137            Err(e) => println!("{e}"),
138        }
139    }
140}