castep_cell_parser/
lib.rs

1// mod chumsky_parser;
2mod error;
3mod pest_parser;
4
5pub use error::CellParseError;
6pub use pest::Parser;
7pub use pest_parser::*;
8
9#[cfg(test)]
10mod tests {
11    use pest::Parser;
12
13    use crate::{CELLParser, ParsedCellDoc, Rule};
14
15    #[test]
16    fn block() {
17        let block = r#"
18%BLOCK LATTICE_CART
19      10.182880152352300       0.000000000000000       0.000000000000000
20       0.000000000000000       5.969867637928440       0.000000000000000
21       0.000000000000000       0.000000000000000       4.750940602435010
22%ENDBLOCK LATTICE_CART
23
24%BLOCK POSITIONS_FRAC
25  O   0.1643519800967740    0.0300822058464748    0.2768833704631610
26  O   0.3348562545299030    0.9636518196508849    0.7820634882457750
27  O   0.8326958291482300    0.5301084132249890    0.7199957529591300
28  O   0.6610893570101430    0.4734103801569969    0.2259017447033960
29  O   0.8337106457826989    0.9698717969773958    0.7183058106151698
30  O   0.6612108775813740    0.0454520844934712    0.2127022276070580
31  O   0.1635829455251090    0.4686437035660579    0.2756069794332100
32  O   0.3391253814473320    0.5275993634085179    0.7857324478518600
33  O   0.0516197811641552    0.7485454174406769    0.7192018908206190
34  O   0.4445448716519500    0.2504482123651820    0.2335411247263110
35  O   0.9476515127699229    0.2492599285336520    0.2740397322044580
36  O   0.5552299107568430    0.7602439679064849    0.7830059196944860
37  O   0.0923481337996638    0.2499531202263609    0.7668392058183450
38  O   0.4106703472528400    0.7435442479936909    0.2751789219973310
39  O   0.9064374592598109    0.7505300036295769    0.2296745553664400
40 Mg   0.0006199142552149   -0.0004320487921238   -0.0030768499228585
41 Mg   0.4994228156871129   -0.0019398234625990    0.4908716202795139
42 Mg  -0.0006329882669688    0.4994780040520729   -0.0020497604004188
43 Mg   0.2252827903320600    0.7471379623944169    0.4935445584935200
44 Mg   0.2782374660305380    0.2475124682348810    0.9955869732606208
45 Mg   0.7775179799257270    0.2491749730323970    0.4846031569120610
46 Mg   0.7221112967554049    0.7584509571751828    0.0158847583565514
47 Si   0.0952635537672570    0.2494188083522390    0.4258637552583270
48 Si   0.4071916529256810    0.7490334420514630    0.9342478419267760
49 Si   0.9031549548510469    0.7493022976004839    0.5712966407917540
50 Si   0.5867373163320350    0.2637625595658440    0.0400795522157690
51 Cr   0.5144716596281440    0.4777557343753299    0.5410263903216420
52%ENDBLOCK POSITIONS_FRAC
53
54%BLOCK KPOINTS_LIST
55   0.0000000000000000    0.2500000000000000    0.3333333333333333       0.333333333333333
56   0.0000000000000000    0.2500000000000000    0.0000000000000000       0.333333333333333
57   0.0000000000000000    0.2500000000000000   -0.3333333333333333       0.333333333333333
58%ENDBLOCK KPOINTS_LIST
59
60%BLOCK CELL_CONSTRAINTS
61       1       2       3
62       4       5       6
63%ENDBLOCK CELL_CONSTRAINTS
64
65FIX_COM : false
66%BLOCK IONIC_CONSTRAINTS
67%ENDBLOCK IONIC_CONSTRAINTS
68
69%BLOCK EXTERNAL_EFIELD
70    0.0000000000     0.0000000000     0.0000000000 
71%ENDBLOCK EXTERNAL_EFIELD
72
73%BLOCK SPECIES_MASS
74       O     15.9989995956
75      Mg     24.3050003052
76      Si     28.0849990845
77      Cr     51.9959983826
78%ENDBLOCK SPECIES_MASS
79
80%BLOCK SPECIES_POT
81       O  O_00PBE_OP.recpot
82      Mg  Mg_00PBE_OP.recpot
83      Si  Si_00PBE_OP.recpot
84      Cr  Cr_00.recpot
85%ENDBLOCK SPECIES_POT
86
87%BLOCK SPECIES_LCAO_STATES
88       O         2
89      Mg         4
90      Si         2
91      Cr         3
92%ENDBLOCK SPECIES_LCAO_STATES
93
94SYMMETRY_GENERATE
95
96%BLOCK HUBBARD_U
97      Fe       1       d: 0.500000000000000
98%ENDBLOCK HUBBARD_U
99
100%BLOCK HUBBARD_ALPHA
101      Fe       1       d: 0.500000000000000
102%ENDBLOCK HUBBARD_ALPHA
103
104QUANTIZATION_AXIS :    0.0000    0.0000    1.0000
105"#;
106        let cell = CELLParser::parse(Rule::cell_doc, block).expect("unsuccessful parse");
107        dbg!(&cell);
108        let cell_doc: ParsedCellDoc = CELLParser::cell_doc_map(cell);
109        println!("{}", cell_doc.get("species_pot").unwrap());
110        let pot = cell_doc
111            .get("species_pot")
112            .unwrap()
113            .as_block()
114            .map(|block| {
115                block
116                    .values()
117                    .iter()
118                    .map(|line| {
119                        let parsed = CELLParser::parse(Rule::potential_line, line).unwrap();
120                        parsed
121                            .find_first_tagged("potential_file")
122                            .map(|p| p.as_str().to_string())
123                            .unwrap()
124                    })
125                    .collect::<Vec<String>>()
126            })
127            .unwrap();
128        pot.iter().for_each(|p| println!("{}", p));
129    }
130    const PARAM: &str = r#"task : SinglePoint
131comment : CASTEP calculation from Materials Studio
132xc_functional : PBE
133spin_polarized : true
134spin :        4
135opt_strategy : Speed
136page_wvfns :        0
137cut_off_energy :      330.000000000000000
138grid_scale :        2.000000000000000
139fine_grid_scale :        3.000000000000000
140finite_basis_corr :        0
141elec_energy_tol :   5.000000000000000e-007
142max_scf_cycles :     6000
143fix_occupancy : false
144metals_method : dm
145mixing_scheme : Pulay
146mix_charge_amp :        0.500000000000000
147mix_spin_amp :        2.000000000000000
148mix_charge_gmax :        1.500000000000000
149mix_spin_gmax :        1.500000000000000
150mix_history_length :       20
151nextra_bands :       39
152smearing_width :        0.100000000000000
153spin_fix :        6
154num_dump_cycles : 0
155calculate_ELF : false
156calculate_stress : false
157popn_calculate : true
158calculate_hirshfeld : true
159calculate_densdiff : false
160popn_bond_cutoff :        3.000000000000000
161pdos_calculate_weights : true
162iprint:3
163"#;
164    #[test]
165    fn param() {
166        let parsed = CELLParser::parse(Rule::cell_doc, PARAM).expect("unsuccessful parse");
167        dbg!(&parsed);
168        let cell_param = CELLParser::cell_doc_map(parsed);
169    }
170}