pynondimensionalizer.pynondimensionalizer

 1__docformat__ = "numpy"
 2
 3import click
 4from sympy import Matrix, pretty
 5from pandas import read_csv
 6from pathlib import Path
 7
 8#  ──────────────────────────────────────────────────────────────────────────
 9
10def nullspace_solve(input_file, width=None):
11    """Solves for the nullspace matrix, with the given dimensions matrix.
12
13    Parameters
14    ----------
15    input_file : str
16        Path to the input file with the dimensions matrix.
17
18    Returns
19    -------
20    output_str : str
21        String that contains the nullspace matrix, with headers.
22    D : Matrix
23        Dimensional matrix.
24    S : List
25        List of matrices comprising the nullspace matrix.
26    dim : list
27        List of strings of the dimensions.
28    var : list
29        List of strings of the variables.
30    """
31
32    D_in = read_csv(Path(input_file), index_col=0)
33
34    # building dimensional matrix and getting nullspace
35    D = Matrix(D_in)
36    S = D.nullspace()
37
38    dim = D_in.index.tolist()
39    var = list(map(str.strip, D_in.columns.tolist()))
40
41    dim_str = ' '.join(dim)
42    var_str = ' '.join(var)
43
44    # building string
45    output_str = 'Dimensions = \n\n' + dim_str + '\n\nVariables = \n\n' + var_str + '\n\nD = \n\n' + pretty(D, num_columns=width) + '\n\nS = \n\n' + pretty(S, num_columns=width) + '\n\n'
46
47    # getting non dimensional numbers
48    output_str += '𝚷 = \n' 
49
50    for i, Si in enumerate(S):
51        output_str += '\n𝚷 ' + str(i) + '\t= '
52        for j, Sij in enumerate(Si):
53            if Sij != 0:
54                output_str += var[j] + '^(' + str(Sij) + ') '
55
56
57    return output_str, D, S, dim, var
58
59
60def output_write(output_str, output_file):
61    """Writes the nullspace matrix to the output file.
62
63    Parameters
64    ----------
65    output_file : str
66        Path to the output file.
67    output_str : str
68        String containing the nullspace matrix.
69    """
70
71    with open(Path(output_file), 'w') as file:
72        file.write(output_str)
73
74#  ──────────────────────────────────────────────────────────────────────────
75# cli
76
77CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
78@click.command(context_settings=CONTEXT_SETTINGS)
79@click.option('-i', '--input', help='Input csv file containing the dimensional matrix', required=True, type=str)
80@click.option('-o', '--output', help='Output file path and name', type=str, default=None)
81@click.option('-w', '--width', help='Column width for output', type=int, default=None)
82def pynondim(input, output, width):
83    """Find the nullspace of a dimensional matrix and the nondimensional variables.
84    """
85
86    output_str, S, D, dim, var = nullspace_solve(input, width=width)
87
88    if output:
89        output_write(output_str, output)
90    else:
91        print(output_str)
def nullspace_solve(input_file, width=None):
11def nullspace_solve(input_file, width=None):
12    """Solves for the nullspace matrix, with the given dimensions matrix.
13
14    Parameters
15    ----------
16    input_file : str
17        Path to the input file with the dimensions matrix.
18
19    Returns
20    -------
21    output_str : str
22        String that contains the nullspace matrix, with headers.
23    D : Matrix
24        Dimensional matrix.
25    S : List
26        List of matrices comprising the nullspace matrix.
27    dim : list
28        List of strings of the dimensions.
29    var : list
30        List of strings of the variables.
31    """
32
33    D_in = read_csv(Path(input_file), index_col=0)
34
35    # building dimensional matrix and getting nullspace
36    D = Matrix(D_in)
37    S = D.nullspace()
38
39    dim = D_in.index.tolist()
40    var = list(map(str.strip, D_in.columns.tolist()))
41
42    dim_str = ' '.join(dim)
43    var_str = ' '.join(var)
44
45    # building string
46    output_str = 'Dimensions = \n\n' + dim_str + '\n\nVariables = \n\n' + var_str + '\n\nD = \n\n' + pretty(D, num_columns=width) + '\n\nS = \n\n' + pretty(S, num_columns=width) + '\n\n'
47
48    # getting non dimensional numbers
49    output_str += '𝚷 = \n' 
50
51    for i, Si in enumerate(S):
52        output_str += '\n𝚷 ' + str(i) + '\t= '
53        for j, Sij in enumerate(Si):
54            if Sij != 0:
55                output_str += var[j] + '^(' + str(Sij) + ') '
56
57
58    return output_str, D, S, dim, var

Solves for the nullspace matrix, with the given dimensions matrix.

Parameters
  • input_file (str): Path to the input file with the dimensions matrix.
Returns
  • output_str (str): String that contains the nullspace matrix, with headers.
  • D (Matrix): Dimensional matrix.
  • S (List): List of matrices comprising the nullspace matrix.
  • dim (list): List of strings of the dimensions.
  • var (list): List of strings of the variables.
def output_write(output_str, output_file):
61def output_write(output_str, output_file):
62    """Writes the nullspace matrix to the output file.
63
64    Parameters
65    ----------
66    output_file : str
67        Path to the output file.
68    output_str : str
69        String containing the nullspace matrix.
70    """
71
72    with open(Path(output_file), 'w') as file:
73        file.write(output_str)

Writes the nullspace matrix to the output file.

Parameters
  • output_file (str): Path to the output file.
  • output_str (str): String containing the nullspace matrix.
pynondim = <Command pynondim>

Find the nullspace of a dimensional matrix and the nondimensional variables.