xref: /petsc/lib/petsc/bin/petsc_conf.py (revision 59e7829ccff665372c1d09026a11beea9cbf8624)
1import warnings
2
3def get_conf():
4    """Parses various PETSc configuration/include files to get data types.
5
6    precision, indices, complexscalars = get_conf()
7
8    Output:
9      precision: 'single', 'double', 'longlong' indicates precision of PetscScalar
10      indices: '32', '64' indicates bit-size of PetscInt
11      complex: True/False indicates whether PetscScalar is complex or not.
12    """
13
14    import sys, os
15    precision = None
16    indices = None
17    complexscalars = None
18
19    if 'PETSC_DIR' in os.environ:
20        petscdir = os.environ['PETSC_DIR']
21    else:
22        warnings.warn('PETSC_DIR env not set - unable to locate PETSc installation, using defaults')
23        return None, None, None
24
25    if os.path.isfile(os.path.join(petscdir,'lib','petsc','conf','petscrules')):
26        # found prefix install
27        petscvariables = os.path.join(petscdir,'lib','petsc','conf','petscvariables')
28        petscconfinclude = os.path.join(petscdir,'include','petscconf.h')
29    else:
30        if 'PETSC_ARCH' in os.environ:
31            petscarch = os.environ['PETSC_ARCH']
32            if os.path.isfile(os.path.join(petscdir,petscarch,'lib','petsc','conf','petscrules')):
33                # found legacy install
34                petscvariables = os.path.join(petscdir,petscarch,'lib','petsc','conf','petscvariables')
35                petscconfinclude = os.path.join(petscdir,petscarch,'include','petscconf.h')
36            else:
37                warnings.warn('Unable to locate PETSc installation in specified PETSC_DIR/PETSC_ARCH, using defaults')
38                return None, None, None
39        else:
40            warnings.warn('PETSC_ARCH env not set or incorrect PETSC_DIR is given - unable to locate PETSc installation, using defaults')
41            return None, None, None
42
43    try:
44        fid = open(petscvariables, 'r')
45    except IOError:
46        warnings.warn('Nonexistent or invalid PETSc installation, using defaults')
47        return None, None, None
48    else:
49        for line in fid:
50            if line.startswith('PETSC_PRECISION'):
51                precision = line.strip().split('=')[1].strip('\n').strip()
52
53        fid.close()
54
55    try:
56        fid = open(petscconfinclude, 'r')
57    except IOError:
58        warnings.warn('Nonexistent or invalid PETSc installation, using defaults')
59        return None, None, None
60    else:
61        for line in fid:
62            if line.startswith('#define PETSC_USE_64BIT_INDICES 1'):
63                indices = '64bit'
64            elif line.startswith('#define PETSC_USE_COMPLEX 1'):
65                complexscalars = True
66
67        if indices is None:
68            indices = '32bit'
69        if complexscalars is None:
70            complexscalars = False
71        fid.close()
72
73    return precision, indices, complexscalars
74