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']
def get_cpu_model():
 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.

def write_density_cube(mol, basis, dmat, cube_file, nx=100, ny=100, nz=100, ncores=1):
 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.

def write_orbital_cube(mol, basis, mo_coeffs, cube_file, nx=100, ny=100, nz=100, ncores=1):
 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.