pySIMSalabim 1__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.
Files changed (37) hide show
  1. pySIMsalabim/__init__.py +21 -0
  2. pySIMsalabim/aux_funcs/SCLC_funcs.py +561 -0
  3. pySIMsalabim/aux_funcs/__init__.py +0 -0
  4. pySIMsalabim/aux_funcs/addons.py +55 -0
  5. pySIMsalabim/experiments/CV.py +918 -0
  6. pySIMsalabim/experiments/EQE.py +461 -0
  7. pySIMsalabim/experiments/JV_steady_state.py +259 -0
  8. pySIMsalabim/experiments/__init__.py +0 -0
  9. pySIMsalabim/experiments/hysteresis.py +753 -0
  10. pySIMsalabim/experiments/impedance.py +848 -0
  11. pySIMsalabim/experiments/imps.py +630 -0
  12. pySIMsalabim/install/__init__.py +20 -0
  13. pySIMsalabim/install/compile_simsalabim.py +76 -0
  14. pySIMsalabim/install/get_SIMsalabim.py +505 -0
  15. pySIMsalabim/install/get_gnu_parallel.py +107 -0
  16. pySIMsalabim/plots/__init__.py +0 -0
  17. pySIMsalabim/plots/band_diagram.py +209 -0
  18. pySIMsalabim/plots/plot_def.py +19 -0
  19. pySIMsalabim/plots/plot_functions.py +240 -0
  20. pySIMsalabim/tests/__init__.py +0 -0
  21. pySIMsalabim/tests/test_CV.py +91 -0
  22. pySIMsalabim/tests/test_EQE.py +97 -0
  23. pySIMsalabim/tests/test_IMPS.py +92 -0
  24. pySIMsalabim/tests/test_JV.py +88 -0
  25. pySIMsalabim/tests/test_hyst.py +92 -0
  26. pySIMsalabim/tests/test_impedance.py +89 -0
  27. pySIMsalabim/utils/__init__.py +25 -0
  28. pySIMsalabim/utils/clean_up.py +96 -0
  29. pySIMsalabim/utils/device_parameters.py +720 -0
  30. pySIMsalabim/utils/general.py +521 -0
  31. pySIMsalabim/utils/parallel_sim.py +654 -0
  32. pySIMsalabim/utils/utils.py +129 -0
  33. pySIMsalabim-1.dist-info/LICENSE +674 -0
  34. pySIMsalabim-1.dist-info/METADATA +159 -0
  35. pySIMsalabim-1.dist-info/RECORD +37 -0
  36. pySIMsalabim-1.dist-info/WHEEL +5 -0
  37. pySIMsalabim-1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,259 @@
1
+ """Perform steady-state JV simulations"""
2
+
3
+ ######### Package Imports #########################################################################
4
+
5
+ import os, uuid, sys
6
+ import pandas as pd
7
+ import numpy as np
8
+ import matplotlib.pyplot as plt
9
+ from scipy import constants
10
+ # import pySIMsalabim
11
+ ## Import pySIMsalabim, if not successful, add the parent directory to the system path
12
+ try :
13
+ import pySIMsalabim as sim
14
+ except ImportError:
15
+ # Add the parent directory to the system path
16
+ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
17
+ import pySIMsalabim as sim
18
+ from pySIMsalabim.utils import general as utils_gen
19
+ from pySIMsalabim.utils.parallel_sim import *
20
+ from pySIMsalabim.utils.utils import update_cmd_pars
21
+
22
+ ######### Functions #################################################################################
23
+
24
+ def run_SS_JV(simss_device_parameters, session_path, JV_file_name = 'JV.dat', varFile = 'none', G_fracs = [], parallel = False, max_jobs = max(1,os.cpu_count()-1), run_mode = True, **kwargs):
25
+ """
26
+
27
+ Parameters
28
+ ----------
29
+ simss_device_parameters : string
30
+ Name of the device parameters file.
31
+ session_path : string
32
+ Path to the session folder where the simulation will run.
33
+ JV_file_name : string
34
+ Name of the JV file.
35
+ parallel : bool, optional
36
+ Run the simulations in parallel, by default False
37
+ max_jobs : int, optional
38
+ Maximum number of parallel jobs, by default max(1,os.cpu_count()-1)
39
+ cmd_pars : _type_, optional
40
+ _description_, by default None
41
+ UUID : str, optional
42
+ _description_, by default ''
43
+ run_mode : bool, optional
44
+ indicate whether the script is in 'web' mode (True) or standalone mode (False). Used to control the console output, by default True
45
+ **kwargs : dict
46
+ Additional arguments to be passed to the function.
47
+
48
+ Returns
49
+ -------
50
+ int
51
+ Exitcode of the simulation.
52
+ str
53
+ Message from the simulation.
54
+
55
+ """
56
+
57
+ UUID = kwargs.get('UUID', '') # Check if the user wants to add a UUID to the JV file name
58
+ cmd_pars = kwargs.get('cmd_pars', None) # Check if the user wants to add additional command line parameters to the
59
+ force_multithreading = kwargs.get('force_multithreading', False) # Check if the user wants to force multithreading instead of using GNU parallel
60
+ cmd_pars = kwargs.get('cmd_pars', None) # Check if the user wants to add additional command line parameters
61
+ # Check if the user wants to force the use of thread safe mode, necessary for Windows with parallel simulations
62
+ if os.name == 'nt':
63
+ threadsafe = kwargs.get('threadsafe', True) # Check if the user wants to force the use of threads instead of processes
64
+ else:
65
+ threadsafe = kwargs.get('threadsafe', False) # Check if the user wants to force the use of threads instead of processes
66
+
67
+ turnoff_autoTidy = kwargs.get('turnoff_autoTidy', None) # Check if the user wants to turn off the autoTidy function in SIMsalabim
68
+ if turnoff_autoTidy is None:
69
+ if not threadsafe:
70
+ turnoff_autoTidy = True
71
+ else:
72
+ turnoff_autoTidy = False
73
+
74
+ # Update the JV file name with the UUID
75
+ if UUID != '':
76
+ dum_str = f'_{UUID}'
77
+ else:
78
+ dum_str = ''
79
+
80
+ # Define the command to be executed
81
+ if G_fracs is None:
82
+ # Update the filenames with the UUID
83
+ JV_file_name = os.path.join(session_path,JV_file_name)
84
+ logFile = os.path.join(session_path,'log.txt')
85
+ scParsFile = os.path.join(session_path,'scPars.txt')
86
+ if UUID != '':
87
+ JV_file_name_base, JV_file_name_ext = os.path.splitext(JV_file_name)
88
+ JV_file_name = JV_file_name_base + dum_str + JV_file_name_ext
89
+ logFile = os.path.join(session_path,'log'+dum_str+'.txt')
90
+ scParsFile = os.path.join(session_path,'scPars'+dum_str+'.txt')
91
+ if varFile != 'none':
92
+ var_file_base, var_file_ext = os.path.splitext(varFile)
93
+ varFile = var_file_base + dum_str + var_file_ext
94
+ varFile = os.path.join(session_path,varFile)
95
+
96
+
97
+
98
+ # Specify the arguments to be attached to the cmd
99
+ SS_JV_args = [{'par':'dev_par_file','val':simss_device_parameters},
100
+ {'par':'JVFile','val':JV_file_name},
101
+ {'par':'logFile','val':logFile},
102
+ {'par':'scParsFile','val':scParsFile}
103
+ ]
104
+
105
+ if turnoff_autoTidy:
106
+ SS_JV_args.append({'par':'autoTidy','val':'0'})
107
+
108
+ if varFile != 'none':
109
+ SS_JV_args.append({'par':'varFile','val':varFile})
110
+
111
+ # Update the cmd_pars with the SS_JV_args
112
+ if cmd_pars is not None:
113
+ SS_JV_args = update_cmd_pars(SS_JV_args, cmd_pars)
114
+
115
+ result, message = utils_gen.run_simulation('simss',SS_JV_args,session_path,run_mode = run_mode)
116
+
117
+ return result, message
118
+
119
+ else:
120
+ # Update the filenames with the UUID
121
+ JV_file_name = os.path.join(session_path,JV_file_name)
122
+ JV_file_name_base, JV_file_name_ext = os.path.splitext(JV_file_name)
123
+ if varFile != 'none':
124
+ var_file_base, var_file_ext = os.path.splitext(varFile)
125
+ varFile = var_file_base + dum_str + var_file_ext
126
+ varFile = os.path.join(session_path,varFile)
127
+
128
+ # SS_JV_args = [{'par':'dev_par_file','val':simss_device_parameters}]
129
+ SS_JV_args_list = []
130
+ for G_frac in G_fracs:
131
+ dum_args = [{'par':'dev_par_file','val':simss_device_parameters},
132
+ {'par':'G_frac','val':str(G_frac)},
133
+ {'par':'JVFile','val':JV_file_name_base + f'_Gfrac_{G_frac}' + dum_str + JV_file_name_ext},
134
+ {'par':'logFile','val':os.path.join(session_path,'log'+f'_Gfrac_{G_frac}'+dum_str+'.txt')},
135
+ {'par':'scParsFile','val':os.path.join(session_path,'scPars'+f'_Gfrac_{G_frac}'+dum_str+'.txt')},
136
+ {'par':'varFile','val':os.path.join(session_path,var_file_base + f'_Gfrac_{G_frac}' + dum_str +var_file_ext)} if varFile != 'none' else {'par':'varFile','val':'none'}
137
+ ]
138
+
139
+ if turnoff_autoTidy:
140
+ dum_args.append({'par':'autoTidy','val':'0'})
141
+
142
+ if cmd_pars is not None:
143
+ dum_args = update_cmd_pars(dum_args, cmd_pars)
144
+
145
+ SS_JV_args_list.append(dum_args)
146
+
147
+ if parallel and len(G_fracs) > 1:
148
+ results = run_simulation_parallel('simss', SS_JV_args_list, session_path, max_jobs, force_multithreading=force_multithreading)
149
+ msg_list = ['' for i in range(len(results))]
150
+ else:
151
+ results, msg_list = [], []
152
+ for dum_args in SS_JV_args_list:
153
+
154
+ if threadsafe:
155
+ result, message = utils_gen.run_simulation_filesafe('simss', dum_args, session_path, run_mode)
156
+ else:
157
+ result, message = utils_gen.run_simulation('simss', dum_args, session_path, run_mode)
158
+
159
+ results.append(result.returncode)
160
+ msg_list.append(message)
161
+
162
+ if all([res == 0 for res in results]):
163
+ return 0, 'All JV simulations completed successfully'
164
+ else:
165
+ return results, msg_list
166
+
167
+ ## Running the function as a standalone script
168
+ if __name__ == "__main__":
169
+ ## Manual Steady State input parameters. These are overwritten by command line arguments if provided
170
+ # G_fracs = [0.9, 1.0]
171
+ G_fracs = None
172
+
173
+ # Define folder and file paths
174
+ session_path = os.path.join('../../','SIMsalabim','SimSS')
175
+
176
+ simss_device_parameters = 'simulation_setup.txt'
177
+
178
+ JV_name = 'JV.dat'
179
+ Var_name = 'Var.dat'
180
+
181
+ # UUID = str(uuid.uuid4()) # Add a UUID to the simulation
182
+ UUID = ''
183
+
184
+ # Not user input
185
+ parallel = False
186
+ run_mode = False # If False, show verbose output in console
187
+
188
+ ############## Command line arguments ##############
189
+ ## Notes
190
+ ## - The command line arguments are optional and can be provided in any order
191
+ ## - Each command line argument must be provided in the format -par_name value
192
+ ## - Possible arguments include all SIMsalabim parameters and Steady State JV specific parameters as listed before
193
+ ## - Special arguments
194
+ ## - G_fracs : string
195
+ ## - The G fractions to simulate, separated by a comma
196
+ ## - sp : string
197
+ ## - The session path, i.e. the working directory for the simulation
198
+ ## - simsetup : string
199
+ ## - The name of the simss simulation setup parameters file
200
+ ## - UUID : string
201
+ ## - An UUID to add to the simulation (output)
202
+
203
+ cmd_pars_dict = {}
204
+ cmd_pars = []
205
+
206
+ # Check if any arguments are provided.
207
+ if len(sys.argv) >= 2:
208
+ # Each arguments should be in the format -par val, i.e. in pairs of two. Skip the first argument as this is the script name
209
+ if not len(sys.argv[1:]) % 2 == 0:
210
+ print('Error in command line parameters. Please provide arguments in the format -par_name value -par_name value ...')
211
+ sys.exit(1)
212
+ else:
213
+ input_list = sys.argv[1:]
214
+ # Loop over the input list and put pairs into a dictionary with the first argument as key and the second argument as value
215
+ for i in range(0,len(input_list),2):
216
+ # Check if the key already exists, if not, add it to the cmd_pars_dict
217
+ if str(input_list[i][1:]) in cmd_pars_dict:
218
+ print(f'Duplicate parameter found in the command line parameters: {str(input_list[i][1:])}')
219
+ sys.exit(1)
220
+ else:
221
+ cmd_pars_dict[str(input_list[i][1:])] = str(input_list[i+1])
222
+
223
+ # Check and process specific keys/arguments that are not native SIMsalabim arguments
224
+ # Handle the session_path/sp argument separately, as the other parameters depend on this
225
+ if 'sp' in cmd_pars_dict:
226
+ session_path = cmd_pars_dict['sp']
227
+ # remove from cmd_pars
228
+ cmd_pars_dict.pop('sp')
229
+
230
+ # Define mappings for keys and variables
231
+ key_action_map = {
232
+ 'simsetup': lambda val: {'zimt_device_parameters': val},
233
+ 'G_fracs': lambda val: {'G_fracs': val.split(',')}, # IMPORTANT: if multiple Gfracs are provided, they must be separated by a comma!!
234
+ 'JV_name': lambda val: {'JV_name': val},
235
+ 'Var_name': lambda val: {'Var_name': val},
236
+ 'UUID': lambda val: {'UUID': val},
237
+ }
238
+
239
+ for key in list(cmd_pars_dict.keys()): # Use list to avoid modifying the dictionary while iterating
240
+ if key in key_action_map:
241
+ # Apply the corresponding action
242
+ result = key_action_map[key](cmd_pars_dict[key])
243
+ globals().update(result) # Dynamically update global variables
244
+ cmd_pars_dict.pop(key)
245
+
246
+ # Handle remaining keys in `cmd_pars_dict` and add them to the cmd_pars list
247
+ cmd_pars.extend({'par': key, 'val': value} for key, value in cmd_pars_dict.items())
248
+
249
+ # Run the function
250
+ results, message = run_SS_JV(simss_device_parameters, session_path, JV_name, Var_name, G_fracs, parallel = parallel, run_mode = run_mode, cmd_pars=cmd_pars, UUID=UUID)
251
+
252
+ # Print the results if simulation failed
253
+ if run_mode:
254
+ if G_fracs == None:
255
+ print(message)
256
+ else:
257
+ if results != 0:
258
+ for i, msg in enumerate(message):
259
+ print(f'G_frac: {G_fracs[i]} - {msg}')
File without changes