edipack2py 5.0.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.
edipack2py/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ from edipack2py import class_creator
2
+ from ctypes import *
3
+ import numpy as np
4
+
5
+
6
+ # this ed class contains all the global variables and methods
7
+ global_env = class_creator.global_env
@@ -0,0 +1,391 @@
1
+ from ctypes import *
2
+ import numpy as np
3
+ import os, sys
4
+ from pathlib import Path
5
+ import types
6
+ import pkgconfig
7
+
8
+ #################################
9
+ # AUXILIARY FUNCTIONS
10
+ #################################
11
+
12
+
13
+ # dummy class, to be filled
14
+ class Link:
15
+ def __init__(self, library):
16
+ self.library = library
17
+ try:
18
+ self.has_ineq = bool(c_int.in_dll(self.library, "has_ineq").value)
19
+ except Exception:
20
+ self.has_ineq = None
21
+ print("Cannot init link class: invalid library")
22
+ self.Nineq = None
23
+ self.dim_hloc = 0
24
+ self.Nsym = None
25
+ # utils: colors and bold text
26
+ self.PURPLE = "\033[95m"
27
+ self.CYAN = "\033[96m"
28
+ self.DARKCYAN = "\033[36m"
29
+ self.BLUE = "\033[94m"
30
+ self.GREEN = "\033[92m"
31
+ self.YELLOW = "\033[93m"
32
+ self.RED = "\033[91m"
33
+ self.BOLD = "\033[1m"
34
+ self.UNDERLINE = "\033[4m"
35
+ self.COLOREND = "\033[0m"
36
+
37
+
38
+ # function that will add a variable to the dummy class, will be called
39
+ # in variable definition
40
+ def add_global_variable(obj, dynamic_name, target_object, target_attribute):
41
+ @property
42
+ def getter(self):
43
+ try:
44
+ attrib = getattr(target_object, target_attribute)
45
+ try: # this is for strings
46
+ attrib = attrib.decode()
47
+ except Exception:
48
+ pass
49
+ except Exception: # this is for arrays
50
+ if len(target_object) > 1:
51
+ return [target_object[x] for x in range(len(target_object))]
52
+ return attrib
53
+
54
+ @getter.setter
55
+ def setter(self, new_value):
56
+ try: # this is for arrays
57
+ if len(target_object) > 1:
58
+ if np.isscalar(new_value):
59
+ new_value = [new_value]
60
+ minlength = min(len(target_object), len(new_value))
61
+ target_object[0:minlength] = new_value[0:minlength]
62
+ except Exception:
63
+ try:
64
+ new_value = new_value.encode()
65
+ except Exception:
66
+ pass
67
+ setattr(target_object, target_attribute, new_value)
68
+
69
+ # Dynamically add the property to the class
70
+ setattr(obj.__class__, dynamic_name, getter)
71
+ setattr(obj.__class__, dynamic_name, setter)
72
+
73
+
74
+ # get bath type
75
+ def get_bath_type(self):
76
+ """
77
+
78
+ This function returns an integer number related to the value of \
79
+ :f:var:`bath_type` in the input file
80
+
81
+ - :code:`1` for **normal** bath
82
+ - :code:`2` for **hybrid** bath
83
+ - :code:`3` for **replica** bath
84
+ - :code:`4` for **general** bath
85
+
86
+ :return: the integer index
87
+ :rtype: int
88
+
89
+ """
90
+
91
+ get_bath_type_wrap = self.library.get_bath_type
92
+ get_bath_type_wrap.argtypes = None
93
+ get_bath_type_wrap.restype = c_int
94
+ return get_bath_type_wrap()
95
+
96
+
97
+ # get ed mode
98
+ def get_ed_mode(self):
99
+ """
100
+
101
+ This function returns an integer number related to the value of \
102
+ :f:var:`ed_mode` in the input file
103
+
104
+ - :code:`1` for **normal** mode
105
+ - :code:`2` for **superc** mode
106
+ - :code:`3` for **nonsu2** mode
107
+
108
+ :return: the integer index
109
+ :rtype: int
110
+
111
+ """
112
+
113
+ get_ed_mode_wrap = self.library.get_ed_mode
114
+ get_ed_mode_wrap.argtypes = None
115
+ get_ed_mode_wrap.restype = c_int
116
+ return get_ed_mode_wrap()
117
+
118
+
119
+ ######################################
120
+ # Load shared library with C-bindings
121
+ ######################################
122
+
123
+ custompath = []
124
+ default_pc_dir = ".pkgconfig.d"
125
+ system = sys.platform
126
+ libext = ".dylib" if system == "darwin" else ".so"
127
+ libname = "edipack_cbindings"
128
+ pathlist = []
129
+
130
+ # 1st try: use custom env variable
131
+ try:
132
+ pathlist += os.environ["EDIPACK_PATH"].split(os.pathsep)
133
+ except Exception:
134
+ pass
135
+
136
+ # 2nd try: use pkgconfig directly
137
+ if pkgconfig.exists("edipack"):
138
+ pathlist += [pkgconfig.variables(libname)["libdir"]]
139
+
140
+ # 3rd try: check PKG_CONFIG_PATH
141
+ else:
142
+ try:
143
+ os.environ["PKG_CONFIG_PATH"] += os.pathsep + os.path.join(
144
+ Path.home(), default_pc_dir
145
+ )
146
+ except Exception:
147
+ os.environ["PKG_CONFIG_PATH"] = os.path.join(
148
+ Path.home(), default_pc_dir
149
+ )
150
+ if pkgconfig.exists("edipack"):
151
+ pathlist += [pkgconfig.variables(libname)["libdir"]]
152
+
153
+ # 4th try: look in standard environment variables
154
+ try:
155
+ pathlist += os.environ["LD_LIBRARY_PATH"].split(os.pathsep)
156
+ except Exception:
157
+ pass
158
+ try:
159
+ pathlist += os.environ["DYLD_LIBRARY_PATH"].split(os.pathsep)
160
+ except Exception:
161
+ pass
162
+
163
+ # try loading the library
164
+ dynamic_library = None
165
+ error_message = []
166
+
167
+ for ipath in pathlist:
168
+ try:
169
+ libfile = os.path.join(ipath, "lib" + libname + libext)
170
+ dynamic_library = CDLL(libfile)
171
+ break
172
+ except Exception as e:
173
+ error_message.append(str(e))
174
+ else:
175
+ print("Library loading failed. List of error messages:")
176
+ print(*error_message, sep="\n")
177
+
178
+
179
+ ####################################################################
180
+ # Create the global_env class (this is what the python module sees)
181
+ ####################################################################
182
+
183
+ global_env = Link(dynamic_library)
184
+
185
+ ######################################
186
+ # GLOBAL VARIABLES
187
+ ######################################
188
+
189
+ try:
190
+ add_global_variable(
191
+ global_env, "Nbath", c_int.in_dll(dynamic_library, "Nbath"), "value"
192
+ )
193
+ add_global_variable(
194
+ global_env, "Norb", c_int.in_dll(dynamic_library, "Norb"), "value"
195
+ )
196
+ add_global_variable(
197
+ global_env, "Nspin", c_int.in_dll(dynamic_library, "Nspin"), "value"
198
+ )
199
+ add_global_variable(
200
+ global_env, "Nloop", c_int.in_dll(dynamic_library, "Nloop"), "value"
201
+ )
202
+ add_global_variable(
203
+ global_env, "Nph", c_int.in_dll(dynamic_library, "Nph"), "value"
204
+ )
205
+ add_global_variable(
206
+ global_env, "Nsuccess", c_int.in_dll(dynamic_library, "Nsuccess"), "value"
207
+ )
208
+ add_global_variable(
209
+ global_env, "Lmats", c_int.in_dll(dynamic_library, "Lmats"), "value"
210
+ )
211
+ add_global_variable(
212
+ global_env, "Lreal", c_int.in_dll(dynamic_library, "Lreal"), "value"
213
+ )
214
+ add_global_variable(
215
+ global_env, "Ltau", c_int.in_dll(dynamic_library, "Ltau"), "value"
216
+ )
217
+ add_global_variable(
218
+ global_env, "Lfit", c_int.in_dll(dynamic_library, "Lfit"), "value"
219
+ )
220
+ add_global_variable(
221
+ global_env, "Lpos", c_int.in_dll(dynamic_library, "Lpos"), "value"
222
+ )
223
+ add_global_variable(
224
+ global_env, "LOGfile", c_int.in_dll(dynamic_library, "LOGfile"), "value"
225
+ )
226
+
227
+ add_global_variable(
228
+ global_env,
229
+ "Uloc",
230
+ ARRAY(c_double, 5).in_dll(dynamic_library, "Uloc"),
231
+ "value",
232
+ )
233
+ add_global_variable(
234
+ global_env, "Ust", c_double.in_dll(dynamic_library, "Ust"), "value"
235
+ )
236
+ add_global_variable(
237
+ global_env, "Jh", c_double.in_dll(dynamic_library, "Jh"), "value"
238
+ )
239
+ add_global_variable(
240
+ global_env, "Jx", c_double.in_dll(dynamic_library, "Jx"), "value"
241
+ )
242
+ add_global_variable(
243
+ global_env, "Jp", c_double.in_dll(dynamic_library, "Jp"), "value"
244
+ )
245
+ add_global_variable(
246
+ global_env, "xmu", c_double.in_dll(dynamic_library, "xmu"), "value"
247
+ )
248
+ add_global_variable(
249
+ global_env, "beta", c_double.in_dll(dynamic_library, "beta"), "value"
250
+ )
251
+ add_global_variable(
252
+ global_env,
253
+ "dmft_error",
254
+ c_double.in_dll(dynamic_library, "dmft_error"),
255
+ "value",
256
+ )
257
+ add_global_variable(
258
+ global_env, "eps", c_double.in_dll(dynamic_library, "eps"), "value"
259
+ )
260
+ add_global_variable(
261
+ global_env, "wini", c_double.in_dll(dynamic_library, "wini"), "value"
262
+ )
263
+ add_global_variable(
264
+ global_env, "wfin", c_double.in_dll(dynamic_library, "wfin"), "value"
265
+ )
266
+ add_global_variable(
267
+ global_env, "xmin", c_double.in_dll(dynamic_library, "xmin"), "value"
268
+ )
269
+ add_global_variable(
270
+ global_env, "xmax", c_double.in_dll(dynamic_library, "xmax"), "value"
271
+ )
272
+ add_global_variable(
273
+ global_env, "sb_field", c_double.in_dll(dynamic_library, "sb_field"), "value"
274
+ )
275
+ add_global_variable(
276
+ global_env, "nread", c_double.in_dll(dynamic_library, "nread"), "value"
277
+ )
278
+
279
+ add_global_variable(
280
+ global_env,
281
+ "ed_total_ud",
282
+ c_bool.in_dll(dynamic_library, "ed_total_ud"),
283
+ "value",
284
+ )
285
+ add_global_variable(
286
+ global_env, "ed_twin", c_bool.in_dll(dynamic_library, "ed_twin"), "value"
287
+ )
288
+ except Exception:
289
+ print(
290
+ "Could not setup global vars. Is EDIpack (or EDIpack2ineq) installed?"
291
+ )
292
+
293
+
294
+ ######################################
295
+ # GLOBAL FUNCTIONS
296
+ ######################################
297
+
298
+ # from here
299
+ global_env.get_bath_type = types.MethodType(get_bath_type, global_env)
300
+ global_env.get_ed_mode = types.MethodType(get_ed_mode, global_env)
301
+
302
+ # parse umatrix (newer EDIpack)
303
+ try:
304
+ from . import func_parse_umatrix
305
+
306
+ global_env.reset_umatrix = types.MethodType(
307
+ func_parse_umatrix.reset_umatrix, global_env
308
+ )
309
+ global_env.add_twobody_operator = types.MethodType(
310
+ func_parse_umatrix.add_twobody_operator, global_env
311
+ )
312
+ except Exception:
313
+ pass
314
+
315
+ # read_input
316
+ from . import func_read_input
317
+
318
+ global_env.read_input = types.MethodType(
319
+ func_read_input.read_input, global_env
320
+ )
321
+
322
+ # aux_funx
323
+ from . import func_aux_funx
324
+
325
+ global_env.set_hloc = types.MethodType(func_aux_funx.set_hloc, global_env)
326
+ global_env.search_variable = types.MethodType(
327
+ func_aux_funx.search_variable, global_env
328
+ )
329
+ global_env.check_convergence = types.MethodType(
330
+ func_aux_funx.check_convergence, global_env
331
+ )
332
+
333
+ # bath
334
+ from . import func_bath
335
+
336
+ global_env.get_bath_dimension = types.MethodType(
337
+ func_bath.get_bath_dimension, global_env
338
+ )
339
+ global_env.set_hreplica = types.MethodType(func_bath.set_hreplica, global_env)
340
+ global_env.set_hgeneral = types.MethodType(func_bath.set_hgeneral, global_env)
341
+ global_env.break_symmetry_bath = types.MethodType(
342
+ func_bath.break_symmetry_bath, global_env
343
+ )
344
+ global_env.spin_symmetrize_bath = types.MethodType(
345
+ func_bath.spin_symmetrize_bath, global_env
346
+ )
347
+ global_env.orb_symmetrize_bath = types.MethodType(
348
+ func_bath.orb_symmetrize_bath, global_env
349
+ )
350
+ global_env.orb_equality_bath = types.MethodType(
351
+ func_bath.orb_equality_bath, global_env
352
+ )
353
+ global_env.ph_symmetrize_bath = types.MethodType(
354
+ func_bath.ph_symmetrize_bath, global_env
355
+ )
356
+ global_env.save_array_as_bath = types.MethodType(
357
+ func_bath.save_array_as_bath, global_env
358
+ )
359
+
360
+ global_env.bath_inspect = types.MethodType(func_bath.bath_inspect, global_env)
361
+
362
+
363
+ # main
364
+ from . import func_main
365
+
366
+ global_env.init_solver = types.MethodType(func_main.init_solver, global_env)
367
+ global_env.solve = types.MethodType(func_main.solve, global_env)
368
+ global_env.finalize_solver = types.MethodType(
369
+ func_main.finalize_solver, global_env
370
+ )
371
+
372
+ # io
373
+ from . import func_io
374
+
375
+ global_env.build_sigma = types.MethodType(func_io.build_sigma, global_env)
376
+ global_env.build_gimp = types.MethodType(func_io.build_gimp, global_env)
377
+ global_env.get_sigma = types.MethodType(func_io.get_sigma, global_env)
378
+ global_env.get_gimp = types.MethodType(func_io.get_gimp, global_env)
379
+ global_env.get_g0and = types.MethodType(func_io.get_g0and, global_env)
380
+ global_env.get_delta = types.MethodType(func_io.get_delta, global_env)
381
+ global_env.get_dens = types.MethodType(func_io.get_dens, global_env)
382
+ global_env.get_mag = types.MethodType(func_io.get_mag, global_env)
383
+ global_env.get_docc = types.MethodType(func_io.get_docc, global_env)
384
+ global_env.get_phi = types.MethodType(func_io.get_phi, global_env)
385
+ global_env.get_eimp = types.MethodType(func_io.get_eimp, global_env)
386
+ global_env.get_chi = types.MethodType(func_io.get_chi, global_env)
387
+
388
+ # bath_fit
389
+ from . import func_bath_fit
390
+
391
+ global_env.chi2_fitgf = types.MethodType(func_bath_fit.chi2_fitgf, global_env)