pyfock.Utils
1from .system_information import print_sys_info 2from .system_information import get_cpu_model 3from .print_logo import print_pyfock_logo 4from .write_density_cube import write_density_cube 5from .write_orbital_cube import write_orbital_cube 6 7 8__all__ = ['print_sys_info', 'get_cpu_model', 'print_pyfock_logo', 'write_density_cube', 'write_orbital_cube']
53def print_sys_info(): 54 """ 55 Print comprehensive system information to stdout. 56 57 Displays detailed system specifications including: 58 - Operating system name and version 59 - System architecture (32-bit/64-bit) 60 - CPU model and specifications 61 - Number of physical CPU cores 62 - Number of logical CPU threads 63 - Total system memory in gigabytes 64 65 The information is formatted in a human-readable format with 66 labeled key-value pairs. 67 68 Returns: 69 None: This function only prints to stdout and returns nothing. 70 71 Examples: 72 >>> from pyfock import Utils 73 >>> Utils.print_sys_info() 74 Operating System: Linux 5.15.0 75 System Type: 64bit 76 CPU Model: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz 77 Number of Cores: 6 78 Number of Threads: 12 79 Memory (GB): 15.54 80 81 Note: 82 Some information may show as "Unavailable" if the system 83 doesn't provide access to certain hardware details. 84 85 Dependencies: 86 Requires psutil, platform modules and the get_cpu_model() function. 87 """ 88 # Get system specifications 89 system_info = platform.uname() 90 os = f"{system_info.system} {system_info.release}" 91 system_type = platform.architecture()[0] 92 num_cores = psutil.cpu_count(logical=False) or "Unavailable" 93 num_threads = psutil.cpu_count(logical=True) or "Unavailable" 94 memory = round(psutil.virtual_memory().total / (1024 ** 3), 2) # in GB 95 cpu_model = get_cpu_model() 96 97 # Print system specifications 98 print('Operating System:', os) 99 print('System Type:', system_type) 100 print('CPU Model:', cpu_model) 101 print('Number of Cores:', num_cores) 102 print('Number of Threads:', num_threads) 103 print('Memory (GB):', memory)
Print comprehensive system information to stdout.
Displays detailed system specifications including:
- Operating system name and version
- System architecture (32-bit/64-bit)
- CPU model and specifications
- Number of physical CPU cores
- Number of logical CPU threads
- Total system memory in gigabytes
The information is formatted in a human-readable format with labeled key-value pairs.
Returns: None: This function only prints to stdout and returns nothing.
Examples:
from pyfock import Utils Utils.print_sys_info() Operating System: Linux 5.15.0 System Type: 64bit CPU Model: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Number of Cores: 6 Number of Threads: 12 Memory (GB): 15.54
Note: Some information may show as "Unavailable" if the system doesn't provide access to certain hardware details.
Dependencies: Requires psutil, platform modules and the get_cpu_model() function.
8def get_cpu_model(): 9 """ 10 Return CPU model information in a cross-platform manner. 11 12 Retrieves the CPU model string using platform-specific methods: 13 - Windows: Uses platform.processor() 14 - macOS: Executes sysctl command to get brand string 15 - Linux: Reads from /proc/cpuinfo 16 - Other: Returns "Unknown CPU" 17 18 Returns: 19 str: The CPU model name/description, or an error message if retrieval fails. 20 21 Examples: 22 >>> cpu_model = get_cpu_model() 23 >>> print(cpu_model) 24 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz 25 26 Note: 27 On some systems, CPU model information may not be available or 28 may require elevated privileges to access. 29 30 Raises: 31 No exceptions are raised directly, but any underlying system errors 32 are caught and returned as descriptive error messages. 33 """ 34 system = platform.system() 35 36 try: 37 if system == "Windows": 38 return platform.processor() 39 elif system == "Darwin": # macOS 40 command = ["sysctl", "-n", "machdep.cpu.brand_string"] 41 return subprocess.check_output(command).strip().decode() 42 elif system == "Linux": 43 with open("/proc/cpuinfo", "r") as f: 44 for line in f: 45 if "model name" in line: 46 return line.split(":")[1].strip() 47 else: 48 return "Unknown CPU" 49 except Exception as e: 50 return f"Error getting CPU model: {e}"
Return CPU model information in a cross-platform manner.
Retrieves the CPU model string using platform-specific methods:
- Windows: Uses platform.processor()
- macOS: Executes sysctl command to get brand string
- Linux: Reads from /proc/cpuinfo
- Other: Returns "Unknown CPU"
Returns: str: The CPU model name/description, or an error message if retrieval fails.
Examples:
cpu_model = get_cpu_model() print(cpu_model) Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Note: On some systems, CPU model information may not be available or may require elevated privileges to access.
Raises: No exceptions are raised directly, but any underlying system errors are caught and returned as descriptive error messages.
2def print_pyfock_logo(): 3 """Print PyFock logo with gradient colors using ANSI escape codes.""" 4 5 # ANSI color codes for gradient (blue to purple to pink) 6 colors = [ 7 '\033[38;5;39m', # Bright blue 8 '\033[38;5;45m', # Cyan blue 9 '\033[38;5;51m', # Light cyan 10 '\033[38;5;87m', # Light blue 11 '\033[38;5;123m', # Light purple 12 '\033[38;5;159m', # Very light blue 13 '\033[38;5;195m', # Light cyan-white 14 '\033[38;5;225m', # Light pink 15 '\033[38;5;219m', # Pink 16 '\033[38;5;213m', # Magenta pink 17 ] 18 19 reset = '\033[0m' # Reset color 20 21 # ASCII art for PyFock 22 23 logo_lines = [ 24 "██████ ██ ██ ███████ ██████ ██████ ██ ██", 25 "██░░░██ ░██ ██░ ██░░░░░ ██░░░░██ ██░░░░░ ██ ██░ ", 26 "██████ ░████░ █████ ██ ██ ██ █████░ ", 27 "██░░░░ ░██░ ██░░░ ██ ██ ██ ██░░██ ", 28 "██ ██ ██ ░██████░ ░██████ ██ ░██", 29 "░░ ░░ ░░ ░░░░░░ ░░░░░░ ░░ ░░" 30 ] 31 32 # Alternative block-style logo 33 block_logo = [ 34 "████████ ██ ██ ███████ ████████ ████████ ██ ██", 35 "██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ", 36 "████████ ████ ██████ ██ ██ ██ █████ ", 37 "██ ██ ██ ██ ██ ██ ██ ██ ", 38 "██ ██ ██ ████████ ████████ ██ ██" 39 ] 40 41 print("\n" + "="*60) 42 print("PyFock Python Program for Quantum Chemistry Simulations") 43 print(" by Manas Sharma") 44 print("="*60) 45 46 # Print the logo with gradient colors 47 for line in logo_lines: 48 colored_line = "" 49 chars_per_color = len(line) // len(colors) 50 51 for i, char in enumerate(line): 52 if char == ' ': 53 colored_line += char 54 else: 55 color_index = min(i // max(1, chars_per_color), len(colors) - 1) 56 colored_line += colors[color_index] + char + reset 57 58 print(colored_line) 59 60 print("\n" + "="*60)
Print PyFock logo with gradient colors using ANSI escape codes.
9def write_density_cube(mol, basis, dmat, cube_file, nx=100, ny=100, nz=100, ncores=1): 10 """Write the electron density of a molecule as a cube file.""" 11 numba.set_num_threads(ncores) 12 os.environ['OMP_NUM_THREADS'] = str(ncores) 13 os.environ["OPENBLAS_NUM_THREADS"] = str(ncores) # export OPENBLAS_NUM_THREADS=4 14 os.environ["MKL_NUM_THREADS"] = str(ncores) # export MKL_NUM_THREADS=4 15 os.environ["VECLIB_MAXIMUM_THREADS"] = str(ncores) # export VECLIB_MAXIMUM_THREADS=4 16 os.environ["NUMEXPR_NUM_THREADS"] = str(ncores) # export NUMEXPR_NUM_THREADS=4 17 18 19 # Generate a grid of coordinates for the cube file 20 coords, origin, spacing = generate_cube_coords(mol, nx=nx, ny=ny, nz=nz, padding=5.0) # shape: (ncoord, 3) 21 # Calculate ao and density values at these coordinates 22 bf_values = Integrals.bf_val_helpers.eval_bfs(basis, coords, parallel=True, non_zero_indices=None) # shape: (ncoord, nao) 23 dens_values = Integrals.bf_val_helpers.eval_rho(bf_values, dmat) # shape: (ncoord, 1) 24 # dens_values = contract('mj,mj->m', bf_values @ dmat, bf_values) 25 26 # Write the cube file 27 write_cube_file(cube_file, mol, dens_values, origin, spacing, nx, ny, nz)
Write the electron density of a molecule as a cube file.
9def write_orbital_cube(mol, basis, mo_coeffs, cube_file, nx=100, ny=100, nz=100, ncores=1): 10 """Write the molecular orbital of a molecule as a cube file.""" 11 numba.set_num_threads(ncores) 12 os.environ['OMP_NUM_THREADS'] = str(ncores) 13 os.environ["OPENBLAS_NUM_THREADS"] = str(ncores) # export OPENBLAS_NUM_THREADS=4 14 os.environ["MKL_NUM_THREADS"] = str(ncores) # export MKL_NUM_THREADS=4 15 os.environ["VECLIB_MAXIMUM_THREADS"] = str(ncores) # export VECLIB_MAXIMUM_THREADS=4 16 os.environ["NUMEXPR_NUM_THREADS"] = str(ncores) # export NUMEXPR_NUM_THREADS=4 17 18 19 # Generate a grid of coordinates for the cube file 20 coords, origin, spacing = generate_cube_coords(mol, nx=nx, ny=ny, nz=nz, padding=5.0) # shape: (ncoord, 3) 21 # Calculate ao and density values at these coordinates 22 bf_values = Integrals.bf_val_helpers.eval_bfs(basis, coords, parallel=True, non_zero_indices=None) # shape: (ncoord, nao) 23 # compute all MO values on the grid (G x N) 24 psi_grid = bf_values @ mo_coeffs # matrix multiply 25 26 27 # Write the cube file 28 write_cube_file(cube_file, mol, psi_grid, origin, spacing, nx, ny, nz)
Write the molecular orbital of a molecule as a cube file.