yu-mcal 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mcal/mcal.py ADDED
@@ -0,0 +1,838 @@
1
+ """mcal"""
2
+ import argparse
3
+ import functools
4
+ import pickle
5
+ from pathlib import Path
6
+ from time import time
7
+ from typing import Dict, List, Literal, Optional, Tuple, Union
8
+
9
+ import numpy as np
10
+ from numpy.typing import NDArray
11
+
12
+ from mcal.utils.cif_reader import CifReader
13
+ from mcal.utils.gaus_log_reader import check_normal_termination
14
+ from mcal.utils.gjf_maker import GjfMaker
15
+ from mcal.calculations.hopping_mobility_model import (
16
+ diffusion_coefficient_tensor,
17
+ diffusion_coefficient_tensor_MC,
18
+ diffusion_coefficient_tensor_ODE,
19
+ marcus_rate,
20
+ mobility_tensor
21
+ )
22
+ from mcal.calculations.rcal import Rcal
23
+ from mcal.calculations.tcal import Tcal
24
+
25
+
26
+ print = functools.partial(print, flush=True)
27
+
28
+
29
+ def main():
30
+ """Calculate mobility tensor considering anisotropy and path continuity.
31
+
32
+ Examples
33
+ --------
34
+ Basic usage:
35
+ - Calculate p-type mobility for xxx crystal\n
36
+ $ python hop_mcal.py xxx.cif p
37
+
38
+ - Calculate n-type mobility for xxx crystal\n
39
+ $ python hop_mcal.py xxx.cif n
40
+
41
+ With resource options:
42
+ - Use 8 CPUs and 16GB memory\n
43
+ $ python hop_mcal.py xxx.cif p -c 8 -m 16
44
+
45
+ - Use different calculation method (default is B3LYP/6-31G(d,p))\n
46
+ $ python hop_mcal.py xxx.cif p -M "B3LYP/6-311G(d,p)"
47
+
48
+ High-precision calculation:
49
+ - Calculate all transfer integrals without speedup using moment of inertia and distance between centers of weight\n
50
+ $ python hop_mcal.py xxx.cif p --fullcal
51
+
52
+ - Expand calculation range to 3x3x3 supercell\n
53
+ $ python hop_mcal.py xxx.cif p --cellsize 1
54
+
55
+ - Expand calculation range to 5x5x5 supercell to widen transfer integral calculation range\n
56
+ $ python hop_mcal.py xxx.cif p --cellsize 2
57
+
58
+ Resume and save results:
59
+ - Resume from existing calculations\n
60
+ $ python hop_mcal.py xxx.cif p --resume
61
+
62
+ - Save results to pickle file\n
63
+ $ python hop_mcal.py xxx.cif p --pickle
64
+
65
+ - Read results from existing pickle file\n
66
+ $ python hop_mcal.py xxx_result.pkl p -rp
67
+
68
+ - Read results from existing log files without running Gaussian\n
69
+ $ python hop_mcal.py xxx.cif p -r
70
+
71
+ Compare calculation methods:
72
+ - Compare results using kinetic Monte Carlo and ODE methods\n
73
+ $ python hop_mcal.py xxx.cif p --mc --ode
74
+ """
75
+ # Error range for skipping calculation of transfer integrals using moment of inertia and distance between centers of weight.
76
+ CENTER_OF_WEIGHT_ERROR = 1.0e-7
77
+ MOMENT_OF_INERTIA_ERROR = np.array([[1.0e-3, 1.0e-3, 1.0e-3]])
78
+
79
+ """This code is to execute hop_mcal for command line."""
80
+ parser = argparse.ArgumentParser()
81
+ parser.add_argument('file', help='cif file name or pickle file name if you want to use -rp option', type=str)
82
+ parser.add_argument('osc_type', help='organic semiconductor type', type=str)
83
+ parser.add_argument(
84
+ '-M', '--method',
85
+ help='calculation method used in Gaussian calculations (default is B3LYP/6-31G(d,p))',
86
+ type=str,
87
+ default='B3LYP/6-31G(d,p)',
88
+ )
89
+ parser.add_argument('-c', '--cpu', help='setting the number of cpu (default is 4)', type=int, default=4)
90
+ parser.add_argument(
91
+ '-m', '--mem',
92
+ help='setting the number of memory [GB] (default is 10 GB)',
93
+ type=int,
94
+ default=10,
95
+ )
96
+ parser.add_argument('-g', '--g09', help='use Gaussian 09 (default is Gaussian 16)', action='store_true')
97
+ parser.add_argument('-r', '--read', help='read log files without executing Gaussian', action='store_true')
98
+ parser.add_argument(
99
+ '-rp', '--read_pickle',
100
+ help='read results from existing pickle file',
101
+ action='store_true'
102
+ )
103
+ parser.add_argument('-p', '--pickle', help='save to pickle the result of calculation', action='store_true')
104
+ parser.add_argument(
105
+ '--cellsize',
106
+ help='number of unit cells to expand in each direction around the central unit cell '
107
+ '(Examples: 1 creates 3x3x3, 2 creates 5x5x5 supercell (default is 2))',
108
+ type=int,
109
+ default=2,
110
+ )
111
+ parser.add_argument(
112
+ '--fullcal',
113
+ help='do not process for speeding up using moment of inertia and distance between centers of weight',
114
+ action='store_true',
115
+ )
116
+ parser.add_argument('--mc', help='use Monte Carlo method to calculate diffusion coefficient', action='store_true')
117
+ parser.add_argument(
118
+ '--ode',
119
+ help='use Ordinary Differential Equation method to calculate diffusion coefficient',
120
+ action='store_true',
121
+ )
122
+ parser.add_argument(
123
+ '--resume',
124
+ help='resume calculation',
125
+ action='store_true',
126
+ )
127
+ args = parser.parse_args()
128
+
129
+ args.osc_type = args.osc_type.lower()
130
+
131
+ if args.g09:
132
+ gau_com = 'g09'
133
+ else:
134
+ gau_com = 'g16'
135
+
136
+ # file info
137
+ cif_file = Path(args.file)
138
+ directory = cif_file.parent
139
+ filename = cif_file.stem
140
+ cif_path_without_ext = f'{directory}/{filename}'
141
+
142
+ print('---------------------------------------')
143
+ print(' mcal 0.1.0 (2025/12/30) by Matsui Lab. ')
144
+ print('---------------------------------------')
145
+
146
+ if args.read_pickle:
147
+ read_pickle(args.file)
148
+ exit()
149
+
150
+ print(f'\nCalculate as {args.osc_type}-type organic semiconductor.')
151
+ print(f'\nInput File Name: {args.file}')
152
+ Tcal.print_timestamp()
153
+ print()
154
+ start_time = time()
155
+
156
+ ##### Calculate reorganization energy #####
157
+ cif_reader = CifReader(cif_path=cif_file)
158
+ print(f'Export {cif_path_without_ext}_unit_cell.mol')
159
+ cif_reader.export_unit_cell_file(f'{cif_path_without_ext}_unit_cell.mol', format='mol')
160
+ print('Please verify that the created unit cell is correct.\n')
161
+ symbols = cif_reader.unique_symbols[0]
162
+ coordinates = cif_reader.unique_coords[0]
163
+ coordinates = cif_reader.convert_frac_to_cart(coordinates)
164
+
165
+ if not args.read:
166
+ print('Create gjf for reorganization energy.')
167
+ create_reorg_gjf(
168
+ symbols,
169
+ coordinates,
170
+ filename,
171
+ directory,
172
+ args.cpu,
173
+ args.mem,
174
+ args.method,
175
+ )
176
+
177
+ if args.osc_type == 'p':
178
+ rcal = Rcal(gjf_file=f'{cif_path_without_ext}_opt_n.gjf')
179
+ elif args.osc_type == 'n':
180
+ rcal = Rcal(gjf_file=f'{cif_path_without_ext}_opt_n.gjf', osc_type='n')
181
+ else:
182
+ raise OSCTypeError
183
+
184
+ skip_specified_cal = []
185
+ if args.read:
186
+ print('Skip calculation of reorganization energy.')
187
+ elif args.resume:
188
+ rcal.check_extension_log(f'{cif_path_without_ext}_opt_n.gjf')
189
+ skip_specified_cal = check_reorganization_energy_completion(cif_path_without_ext, args.osc_type, extension_log=rcal._extension_log)
190
+ else:
191
+ print('Calculate reorganization energy.')
192
+
193
+ reorg_energy = rcal.calc_reorganization(gau_com=gau_com, only_read=args.read, is_output_detail=True, skip_specified_cal=skip_specified_cal)
194
+
195
+ print_reorg_energy(args.osc_type, reorg_energy)
196
+
197
+ ##### Calculate transfer integrals #####
198
+ transfer_integrals = []
199
+ mom_dis_ti = [] # Store moment of inertia, distance between centers of weight and transfer integral
200
+
201
+ expand_mols = cif_reader.expand_mols(args.cellsize)
202
+ for s in range(len(cif_reader.unique_symbols.keys())):
203
+ unique_symbols = cif_reader.unique_symbols[s]
204
+ unique_coords = cif_reader.unique_coords[s]
205
+ unique_coords = cif_reader.convert_frac_to_cart(unique_coords)
206
+ for (i, j, k), expand_mol in expand_mols.items():
207
+ for t, (symbols, coordinates) in expand_mol.items():
208
+ # Skip creating gjf for transfer integrals because they are molecules with translation symmetry
209
+ if s > t:
210
+ continue
211
+ elif s == t:
212
+ if (i, j, k) == (0, 0, 0):
213
+ continue
214
+ elif i < 0 or (i == 0 and (j < 0 or (j == 0 and k < 0))):
215
+ continue
216
+
217
+ coordinates = cif_reader.convert_frac_to_cart(coordinates)
218
+
219
+ min_distance = cal_min_distance(
220
+ unique_symbols, unique_coords,
221
+ symbols, coordinates
222
+ )
223
+ if min_distance > 5:
224
+ print()
225
+ print(f'Skip calculation of transfer integral from {s}-th in (0,0,0) cell to {t}-th in ({i},{j},{k}) cell because the minimum distance is over 5 \u212B.\n')
226
+ continue
227
+
228
+ moment, _ = cal_moment_of_inertia(
229
+ unique_symbols, unique_coords,
230
+ symbols, coordinates
231
+ )
232
+
233
+ distance = cal_distance_between_cen_of_weight(
234
+ unique_symbols, unique_coords,
235
+ symbols, coordinates
236
+ )
237
+
238
+ is_run_ti = True
239
+ same_ti = 0
240
+
241
+ # skip calculation of transfer integrals using moment of inertia and distance between centers of weight.
242
+ if not args.fullcal:
243
+ for m, d, ti in mom_dis_ti:
244
+ if (np.all(m - MOMENT_OF_INERTIA_ERROR < moment) and np.all(moment < m + MOMENT_OF_INERTIA_ERROR)) and (d - CENTER_OF_WEIGHT_ERROR < distance < d + CENTER_OF_WEIGHT_ERROR):
245
+ is_run_ti = False
246
+ same_ti = ti
247
+ break
248
+
249
+ if is_run_ti:
250
+ gjf_name = f'{filename}-({s}_{t}_{i}_{j}_{k})'
251
+ gjf_file = f'{directory}/{gjf_name}'
252
+
253
+ tcal = Tcal(gjf_file)
254
+
255
+ is_normal_term = False
256
+ if args.resume:
257
+ tcal.check_extension_log()
258
+ is_normal_term = check_transfer_integral_completion(gjf_file, extension_log=tcal._extension_log)
259
+
260
+ if not args.read and not is_normal_term:
261
+ print()
262
+ print('Create gjf for transfer integral.')
263
+ create_ti_gjf(
264
+ {'symbols': unique_symbols, 'coordinates': unique_coords},
265
+ {'symbols': symbols, 'coordinates': coordinates},
266
+ gjf_basename=gjf_name,
267
+ save_dir=directory,
268
+ cpu=args.cpu,
269
+ mem=args.mem,
270
+ method=args.method,
271
+ )
272
+ tcal.create_monomer_file()
273
+
274
+ if args.g09:
275
+ gaussian_command = 'g09'
276
+ else:
277
+ gaussian_command = 'g16'
278
+ print(f'Calculate transfer integral from {s}-th in (0,0,0) cell to {t}-th in ({i},{j},{k}) cell.')
279
+ tcal.run_gaussian(gaussian_command)
280
+ else:
281
+ print()
282
+ print(f'Skip calculation of transfer integral from {s}-th in (0,0,0) cell to {t}-th in ({i},{j},{k}) cell.')
283
+
284
+ tcal.check_extension_log()
285
+ tcal.read_monomer1()
286
+ tcal.read_monomer2()
287
+ tcal.read_dimer()
288
+
289
+ if args.osc_type == 'p':
290
+ transfer = Tcal.cal_transfer_integrals(
291
+ tcal.mo1[tcal.n_elect1-1], tcal.overlap, tcal.fock, tcal.mo2[tcal.n_elect2-1]
292
+ )
293
+ elif args.osc_type == 'n':
294
+ transfer = Tcal.cal_transfer_integrals(
295
+ tcal.mo1[tcal.n_elect1], tcal.overlap, tcal.fock, tcal.mo2[tcal.n_elect2]
296
+ )
297
+
298
+ transfer = transfer * 1e-3 # meV to eV
299
+ print_transfer_integral(args.osc_type, transfer)
300
+ transfer_integrals.append((s, t, i, j, k, transfer))
301
+ mom_dis_ti.append((moment, distance, transfer))
302
+ else:
303
+ print()
304
+ print(f'Skip calculation of transfer integral from {s}-th in (0,0,0) cell to {t}-th in ({i},{j},{k}) cell due to identical moment of inertia and distance between centers of weight.')
305
+ print_transfer_integral(args.osc_type, same_ti)
306
+ transfer_integrals.append((s, t, i, j, k, same_ti))
307
+
308
+ ##### Calculate mobility tensor considering anisotropy. #####
309
+ hop = []
310
+
311
+ for s, t, i, j, k, ti in transfer_integrals:
312
+ hop.append((s, t, i, j, k, marcus_rate(ti, reorg_energy)))
313
+
314
+ diffusion_coef_tensor = diffusion_coefficient_tensor(cif_reader.lattice * 1e-8, hop)
315
+ print_tensor(diffusion_coef_tensor, msg="Diffusion coefficient tensor")
316
+ mu = mobility_tensor(diffusion_coef_tensor)
317
+ print_tensor(mu)
318
+ value, vector = cal_eigenvalue_decomposition(mu)
319
+ print_mobility(value, vector)
320
+
321
+ ##### Simulate mobility tensor calculation using Monte Carlo method #####
322
+ if args.mc:
323
+ D_MC = diffusion_coefficient_tensor_MC(cif_reader.lattice * 1e-8, hop)
324
+ print_tensor(D_MC, msg="Diffusion coefficient tensor (MC)")
325
+ mu_MC = mobility_tensor(D_MC)
326
+ print_tensor(mu_MC, msg="Mobility tensor (MC)")
327
+ value_MC, vector_MC = cal_eigenvalue_decomposition(mu_MC)
328
+ print_mobility(value_MC, vector_MC, sim_type='MC')
329
+
330
+ ##### Simulate mobility tensor calculation using Ordinary Differential Equation method #####
331
+ if args.ode:
332
+ D_ODE = diffusion_coefficient_tensor_ODE(cif_reader.lattice * 1e-8, hop)
333
+ print_tensor(D_ODE, msg="Diffusion coefficient tensor (ODE)")
334
+ mu_ODE = mobility_tensor(D_ODE)
335
+ print_tensor(mu_ODE, msg="Mobility tensor (ODE)")
336
+ value_ODE, vector_ODE = cal_eigenvalue_decomposition(mu_ODE)
337
+ print_mobility(value_ODE, vector_ODE, sim_type='ODE')
338
+
339
+ # Save reorganization, transfer integrals, hop, mobility tensor
340
+ if args.pickle:
341
+ with open(f'{cif_path_without_ext}_result.pkl', 'wb') as f:
342
+ pickle.dump({
343
+ 'osc_type': args.osc_type,
344
+ 'lattice': cif_reader.lattice,
345
+ 'z_value': cif_reader.z_value,
346
+ 'reorganization': reorg_energy,
347
+ 'transfer_integrals': transfer_integrals,
348
+ 'hop': hop,
349
+ 'diffusion_coefficient_tensor': diffusion_coef_tensor,
350
+ 'mobility_tensor': mu,
351
+ 'mobility_value': value,
352
+ 'mobility_vector': vector
353
+ }, f)
354
+
355
+ Tcal.print_timestamp()
356
+ end_time = time()
357
+ elapsed_time = end_time - start_time
358
+ if elapsed_time < 1:
359
+ print(f'Elapsed Time: {elapsed_time*1000:.0f} ms')
360
+ elif elapsed_time < 60:
361
+ print(f'Elapsed Time: {elapsed_time:.0f} s')
362
+ elif elapsed_time < 3600:
363
+ print(f'Elapsed Time: {elapsed_time/60:.0f} min')
364
+ else:
365
+ print(f'Elapsed Time: {elapsed_time/3600:.0f} h')
366
+
367
+
368
+ def atom_weight(symbol: str) -> float:
369
+ """Get atom weight
370
+
371
+ Parameters
372
+ ----------
373
+ symbol : str
374
+ Symbol of atom
375
+
376
+ Returns
377
+ -------
378
+ float
379
+ Atomic weight
380
+ """
381
+ ELEMENT_PROP = CifReader.ELEMENT_PROP
382
+ weight = ELEMENT_PROP[ELEMENT_PROP['symbol'] == symbol]['weight'].values[0]
383
+
384
+ return weight
385
+
386
+
387
+ def cal_cen_of_weight(
388
+ symbols1: NDArray[str],
389
+ coordinates1: NDArray[np.float64],
390
+ symbols2: Optional[NDArray[str]] = None,
391
+ coordinates2: Optional[NDArray[np.float64]] = None,
392
+ ) -> NDArray[np.float64]:
393
+ """Calculate center of weight
394
+
395
+ Parameters
396
+ ----------
397
+ symbols1 : NDArray[str]
398
+ Symbols of atoms in one monomer
399
+ coordinates1 : NDArray[np.float64]
400
+ Coordinates of atoms in one monomer
401
+ symbols2 : Optional[NDArray[str]], optional
402
+ Symbols of atoms in another monomer, by default None
403
+ coordinates2 : Optional[NDArray[np.float64]], optional
404
+ Coordinates of atoms in another monomer, by default None
405
+
406
+ Returns
407
+ -------
408
+ NDArray[np.float64]
409
+ Center of weight
410
+ """
411
+ if symbols2 is not None and coordinates2 is not None:
412
+ symbols1 = np.concatenate((symbols1, symbols2), axis=0)
413
+ coordinates1 = np.concatenate((coordinates1, coordinates2), axis=0)
414
+
415
+ weights = np.array([atom_weight(sym) for sym in symbols1])
416
+ total_weight = np.sum(weights)
417
+
418
+ weighted_coords = weights[:, np.newaxis] * coordinates1
419
+ weighted_sum = np.sum(weighted_coords, axis=0)
420
+
421
+ cen_of_weight = weighted_sum / total_weight
422
+
423
+ return cen_of_weight
424
+
425
+
426
+ def cal_distance_between_cen_of_weight(
427
+ symbols1: NDArray[str],
428
+ coordinates1: NDArray[np.float64],
429
+ symbols2: NDArray[str],
430
+ coordinates2: NDArray[np.float64],
431
+ ) -> float:
432
+ """Calculate distance between centers of weight
433
+
434
+ Parameters
435
+ ----------
436
+ symbols1 : NDArray[str]
437
+ Symbols of atoms in one monomer
438
+ coordinates1 : NDArray[np.float64]
439
+ Coordinates of atoms in one monomer
440
+ symbols2 : NDArray[str]
441
+ Symbols of atoms in another monomer
442
+ coordinates2 : NDArray[np.float64]
443
+ Coordinates of atoms in another monomer
444
+
445
+ Returns
446
+ -------
447
+ float
448
+ Distance between centers of weight
449
+ """
450
+ mol1_cen_coord = cal_cen_of_weight(symbols1, coordinates1)
451
+ mol2_cen_coord = cal_cen_of_weight(symbols2, coordinates2)
452
+ distance = np.sqrt(np.sum(np.square(mol1_cen_coord-mol2_cen_coord)))
453
+
454
+ return distance
455
+
456
+
457
+ def cal_eigenvalue_decomposition(mobility_tensor: NDArray[np.float64]) -> Tuple[NDArray[np.float64], NDArray[np.float64]]:
458
+ """Calculate eigenvalue decomposition of mobility tensor
459
+
460
+ Parameters
461
+ ----------
462
+ mobility_tensor : NDArray[np.float64]
463
+ Mobility tensor
464
+
465
+ Returns
466
+ -------
467
+ Tuple[NDArray[np.float64], NDArray[np.float64]]
468
+ Eigenvalue(mobility value) and eigenvector(mobility vector)
469
+ """
470
+ value, vector = np.linalg.eig(mobility_tensor)
471
+ return value, vector
472
+
473
+
474
+ def cal_min_distance(
475
+ symbols1: NDArray[str],
476
+ coords1: NDArray[np.float64],
477
+ symbols2: NDArray[str],
478
+ coords2: NDArray[np.float64],
479
+ ) -> float:
480
+ """Calculate minimum distance between two sets of atoms.
481
+
482
+ Parameters
483
+ ----------
484
+ symbols1 : NDArray[str]
485
+ Symbols of atoms in one monomer
486
+ coords1 : NDArray[np.float64]
487
+ Coordinates of atoms in one monomer
488
+ symbols2 : NDArray[str]
489
+ Symbols of atoms in another monomer
490
+ coords2 : NDArray[np.float64]
491
+ Coordinates of atoms in another monomer
492
+
493
+ Returns
494
+ -------
495
+ float
496
+ Minimum distance between two sets of atoms
497
+ """
498
+ ELEMENT_PROP = CifReader.ELEMENT_PROP
499
+ VDW_RADII = ELEMENT_PROP[['symbol', 'vdw_radius']].set_index('symbol').to_dict()['vdw_radius']
500
+
501
+ radii1 = np.array(
502
+ [VDW_RADII[symbol] for symbol in symbols1]
503
+ )
504
+ radii2 = np.array(
505
+ [VDW_RADII[symbol] for symbol in symbols2]
506
+ )
507
+
508
+ distances = np.sqrt(np.sum((coords1[:, np.newaxis] - coords2)**2, axis=2)) - radii1[:, np.newaxis] - radii2
509
+
510
+ min_distance = np.min(distances)
511
+
512
+ return min_distance
513
+
514
+
515
+ def cal_moment_of_inertia(
516
+ symbols1: NDArray[str],
517
+ coordinates1: NDArray[np.float64],
518
+ symbols2: NDArray[str],
519
+ coordinates2: NDArray[np.float64],
520
+ ) -> Tuple[NDArray[np.float64], NDArray[np.float64]]:
521
+ """Calculate moment of inertia and eigenvectors of the inertia tensor.
522
+
523
+ Parameters
524
+ ----------
525
+ symbols1 : NDArray[str]
526
+ Symbols of atoms in one monomer
527
+ coordinates1 : NDArray[np.float64]
528
+ Coordinates of atoms in one monomer
529
+ symbols2 : NDArray[str]
530
+ Symbols of atoms in another monomer
531
+ coordinates2 : NDArray[np.float64]
532
+ Coordinates of atoms in another monomer
533
+
534
+ Returns
535
+ -------
536
+ Tuple[NDArray[np.float64], NDArray[np.float64]]
537
+ Moment of inertia and eigenvectors of the inertia tensor
538
+ """
539
+ symbols1 = np.concatenate((symbols1, symbols2), axis=0)
540
+ coordinates1 = np.concatenate((coordinates1, coordinates2), axis=0)
541
+
542
+ cen_of_weight = cal_cen_of_weight(symbols1, coordinates1)
543
+
544
+ weights = np.array([atom_weight(sym) for sym in symbols1])
545
+
546
+ xi = coordinates1[:, 0] - cen_of_weight[0]
547
+ yi = coordinates1[:, 1] - cen_of_weight[1]
548
+ zi = coordinates1[:, 2] - cen_of_weight[2]
549
+
550
+ tmp_coords = np.column_stack((xi, yi, zi))
551
+
552
+ moment = np.zeros((3, 3))
553
+
554
+ for i in range(3):
555
+ moment[i, i] = np.sum(weights * (tmp_coords[:, (i+1)%3]**2 + tmp_coords[:, (i+2)%3]**2))
556
+
557
+ for i in range(3):
558
+ for j in range(i+1, 3):
559
+ moment[i, j] = moment[j, i] = -np.sum(weights * tmp_coords[:, i] * tmp_coords[:, j])
560
+
561
+ moment, p = np.linalg.eig(moment)
562
+
563
+ return moment, p
564
+
565
+
566
+ def check_reorganization_energy_completion(
567
+ cif_path_without_ext: str,
568
+ osc_type: Literal['p', 'n'],
569
+ extension_log: str = '.log'
570
+ ) -> List[Literal['opt_neutral', 'opt_ion', 'neutral', 'ion']]:
571
+ """Check if all reorganization energy calculations are completed normally.
572
+
573
+ Parameters
574
+ ----------
575
+ cif_path_without_ext : str
576
+ Base path of cif file (without extension)
577
+ osc_type : Literal['p', 'n']
578
+ Semiconductor type (p-type or n-type)
579
+ extension_log : str
580
+ Extension of log file
581
+
582
+ Returns
583
+ -------
584
+ List[Literal['opt_neutral', 'opt_ion', 'neutral', 'ion']]
585
+ List of calculations to skip
586
+ """
587
+ skip_specified_cal = []
588
+ if check_normal_termination(f'{cif_path_without_ext}_opt_n{extension_log}'):
589
+ skip_specified_cal.append('opt_neutral')
590
+ if check_normal_termination(f'{cif_path_without_ext}_n{extension_log}'):
591
+ skip_specified_cal.append('neutral')
592
+
593
+ if osc_type == 'p':
594
+ if check_normal_termination(f'{cif_path_without_ext}_opt_c{extension_log}'):
595
+ skip_specified_cal.append('opt_ion')
596
+ if check_normal_termination(f'{cif_path_without_ext}_c{extension_log}'):
597
+ skip_specified_cal.append('ion')
598
+ elif osc_type == 'n':
599
+ if check_normal_termination(f'{cif_path_without_ext}_opt_a{extension_log}'):
600
+ skip_specified_cal.append('opt_ion')
601
+ if check_normal_termination(f'{cif_path_without_ext}_a{extension_log}'):
602
+ skip_specified_cal.append('ion')
603
+
604
+ return skip_specified_cal
605
+
606
+
607
+ def check_transfer_integral_completion(gjf_file: str, extension_log: str = '.log') -> bool:
608
+ """Check if all transfer integral calculations are completed normally.
609
+
610
+ Parameters
611
+ ----------
612
+ gjf_file : str
613
+ Base path of gjf file (without extension)
614
+
615
+ Returns
616
+ -------
617
+ bool
618
+ True if all calculations (dimer, monomer1, monomer2) terminated normally
619
+ """
620
+ required_files = ['', '_m1', '_m2']
621
+ return all(
622
+ check_normal_termination(f'{gjf_file}{suffix}{extension_log}')
623
+ for suffix in required_files
624
+ )
625
+
626
+
627
+ def create_reorg_gjf(
628
+ symbols: NDArray[str],
629
+ coordinates: NDArray[np.float64],
630
+ basename: str,
631
+ save_dir: str,
632
+ cpu: int,
633
+ mem: int,
634
+ method: str,
635
+ ) -> None:
636
+ """Create gjf file for reorganization energy calculation.
637
+
638
+ Parameters
639
+ ----------
640
+ symbols : NDArray[str]
641
+ Symbols of atoms
642
+ coordinates : NDArray[np.float64]
643
+ Coordinates of atoms
644
+ basename : str
645
+ Base name of gjf file
646
+ save_dir : str
647
+ Directory to save gjf file
648
+ cpu : int
649
+ Number of cpu
650
+ mem : int
651
+ Number of memory [GB]
652
+ method : str
653
+ Calculation method used in Gaussian calculations
654
+ """
655
+ gjf_maker = GjfMaker()
656
+ gjf_maker.set_function(method)
657
+ gjf_maker.create_chk_file()
658
+ gjf_maker.output_detail()
659
+ gjf_maker.opt()
660
+
661
+ gjf_maker.set_symbols(symbols)
662
+ gjf_maker.set_coordinates(coordinates)
663
+ gjf_maker.set_resource(cpu_num=cpu, mem_num=mem)
664
+
665
+ gjf_maker.export_gjf(
666
+ file_name=f'{basename}_opt_n',
667
+ save_dir=save_dir,
668
+ chk_rwf_name=f'{save_dir}/{basename}_opt_n'
669
+ )
670
+
671
+
672
+ def create_ti_gjf(
673
+ unique_mol: Dict[str, Union[NDArray[str], NDArray[np.float64]]],
674
+ neighbor_mol: Dict[str, Union[NDArray[str], NDArray[np.float64]]],
675
+ gjf_basename: str,
676
+ save_dir: str = '.',
677
+ cpu: int = 4,
678
+ mem: int = 16,
679
+ method: str = 'B3LYP/6-31G*',
680
+ ) -> None:
681
+ """Create gjf file for transfer integral calculation.
682
+
683
+ Parameters
684
+ ----------
685
+ unique_mol : Dict[str, Union[NDArray[str], NDArray[np.float64]]]
686
+ Dictionary containing symbols and coordinates of unique monomer
687
+ neighbor_mol : Dict[str, Union[NDArray[str], NDArray[np.float64]]]
688
+ Dictionary containing symbols and coordinates of neighbor monomer
689
+ gjf_basename : str
690
+ Base name of gjf file
691
+ save_dir : str
692
+ Directory to save gjf file, by default '.'
693
+ cpu : int
694
+ Number of cpu, by default 4
695
+ mem : int
696
+ Number of memory [GB], by default 16
697
+ method : str
698
+ Calculation method used in Gaussian calculations, by default 'B3LYP/6-31G(d,p)'
699
+ """
700
+ gjf_maker = GjfMaker()
701
+ gjf_maker.set_resource(cpu_num=cpu, mem_num=mem)
702
+ gjf_maker.set_function(method)
703
+ gjf_maker.create_chk_file()
704
+ gjf_maker.add_root('Symmetry=None')
705
+
706
+ gjf_maker.set_symbols(unique_mol['symbols'])
707
+ gjf_maker.set_coordinates(unique_mol['coordinates'])
708
+ gjf_maker.set_symbols(neighbor_mol['symbols'])
709
+ gjf_maker.set_coordinates(neighbor_mol['coordinates'])
710
+
711
+ gjf_maker.add_link()
712
+ gjf_maker.add_root('Symmetry=None')
713
+ gjf_maker.add_root('Pop=Full')
714
+ gjf_maker.add_root('IOp(3/33=4,5/33=3)')
715
+
716
+ gjf_maker.export_gjf(file_name=gjf_basename, save_dir=save_dir)
717
+
718
+
719
+ def print_mobility(value: NDArray[np.float64], vector: NDArray[np.float64], sim_type: Literal['MC', 'ODE'] = ''):
720
+ """Print mobility and mobility vector
721
+
722
+ Parameters
723
+ ----------
724
+ value : NDArray[np.float64]
725
+ Mobility value
726
+ vector : NDArray[np.float64]
727
+ Mobility vector
728
+ sim_type : str
729
+ Simulation type (MC or ODE)
730
+ """
731
+ msg_value = 'Mobility value'
732
+ msg_vector = 'Mobility vector'
733
+
734
+ if sim_type:
735
+ msg_value += f' ({sim_type})'
736
+ msg_vector += f' ({sim_type})'
737
+
738
+ print()
739
+ print('-' * (len(msg_value)+2))
740
+ print(f' {msg_value} ')
741
+ print('-' * (len(msg_value)+2))
742
+ print(f"{value[0]:12.6g} {value[1]:12.6g} {value[2]:12.6g}")
743
+ print()
744
+
745
+ print()
746
+ print('-' * (len(msg_vector)+2))
747
+ print(f' {msg_vector} ')
748
+ print('-' * (len(msg_vector)+2))
749
+ for v in vector:
750
+ print(f"{v[0]:12.6g} {v[1]:12.6g} {v[2]:12.6g}")
751
+ print()
752
+
753
+
754
+ def print_reorg_energy(osc_type: Literal['p', 'n'], reorg_energy: float):
755
+ """Print reorganization energy
756
+
757
+ Parameters
758
+ ----------
759
+ osc_type : Literal['p', 'n']
760
+ Semiconductor type (p-type or n-type)
761
+ reorg_energy : float
762
+ Reorganization energy [eV]
763
+ """
764
+ print()
765
+ print('-----------------------')
766
+ print(' Reorganization energy ')
767
+ print('-----------------------')
768
+ print(f'{osc_type}-type: {reorg_energy:10.6g} eV\n')
769
+
770
+
771
+ def print_tensor(mu: NDArray[np.float64], msg: str = 'Mobility tensor'):
772
+ """Print mobility tensor
773
+
774
+ Parameters
775
+ ----------
776
+ mu : NDArray[np.float64]
777
+ Mobility tensor
778
+ msg : str
779
+ Message, by default 'Mobility tensor'
780
+ """
781
+ print()
782
+ print('-' * (len(msg)+2))
783
+ print(f' {msg} ')
784
+ print('-' * (len(msg)+2))
785
+ for a in mu:
786
+ print(f"{a[0]:12.6g} {a[1]:12.6g} {a[2]:12.6g}")
787
+ print()
788
+
789
+
790
+ def print_transfer_integral(osc_type: Literal['p', 'n'], transfer: float):
791
+ """Print transfer integral
792
+
793
+ Parameters
794
+ ----------
795
+ osc_type : Literal['p', 'n']
796
+ Semiconductor type (p-type or n-type)
797
+ transfer : float
798
+ Transfer integral [eV]
799
+ """
800
+ mol_orb = {'p': 'HOMO', 'n': 'LUMO'}
801
+ print()
802
+ print('-------------------')
803
+ print(' Transfer integral ')
804
+ print('-------------------')
805
+ print(f'{mol_orb[osc_type]}: {transfer:12.6g} eV\n')
806
+
807
+
808
+ def read_pickle(file_name: str):
809
+ print(f'\nInput File Name: {file_name}')
810
+
811
+ with open(file_name, 'rb') as f:
812
+ results = pickle.load(f)
813
+
814
+ # print(results)
815
+
816
+ print(f'\nCalculate as {results["osc_type"]}-type organic semiconductor.')
817
+
818
+ print_reorg_energy(results['osc_type'], results['reorganization'])
819
+
820
+ for s, t, i, j, k, ti in results['transfer_integrals']:
821
+ print()
822
+ print(f'{s}-th in (0,0,0) cell to {t}-th in ({i},{j},{k}) cell')
823
+ print_transfer_integral(results['osc_type'], ti)
824
+
825
+ print_tensor(results['diffusion_coefficient_tensor'], msg="Diffusion coefficient tensor")
826
+
827
+ print_tensor(results['mobility_tensor'])
828
+
829
+ print_mobility(results['mobility_value'], results['mobility_vector'])
830
+
831
+
832
+ class OSCTypeError(Exception):
833
+ """Exception for semiconductor type"""
834
+ pass
835
+
836
+
837
+ if __name__ == '__main__':
838
+ main()