readtools.py 5.45 KB
Newer Older
1 2
import numpy as np
import pandas as pd
3
import re
4
from .timeseries import *
5

6

7
def read_timeseries_Modelica(filename, timeseries_names=None, is_regex=False):
8
    from modelicares import SimRes
9
    sim = SimRes(filename)
10 11 12 13 14 15 16 17 18 19 20 21 22
    if timeseries_names is None and is_regex is False:
        # No trajectory names or regex specified, thus read in all
        timeseries = []
        for name in sim.names():
            timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
    elif is_regex is True:
        # Read in variables which match with regex
        timeseries = []
        p = re.compile(timeseries_names)
        timeseries_names = [name for name in sim.names() if p.search(name)]
        timeseries_names.sort()
        for name in timeseries_names:
            timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
23 24
    else:
        # Read in specified time series
25 26 27
        if not isinstance(timeseries_names, list):
            timeseries = TimeSeries(timeseries_names, sim(timeseries_names).times(), sim(timeseries_names).values())
        else:
Markus Mirz's avatar
Markus Mirz committed
28
            timeseries = []
29 30
            for name in timeseries_names:
                timeseries.append(TimeSeries(name, sim(name).times(), sim(name).values()))
31 32

    print('Modelica results column names: ' + str(timeseries_names))
33
    print('Modelica results number: ' + str(len(timeseries_names)))
34

35
    return timeseries
36

37

38
def read_timeseries_csv(filename, timeseries_names=None, print_status=True):
39 40 41 42 43 44 45
    """Reads complex time series data from DPsim log file. Real and
    imaginary part are stored in one complex variable.
    :param filename: name of the csv file that has the data
    :param timeseries_names: column name which should be read
    :return: list of Timeseries objects
    """
    pd_df = pd.read_csv(filename)
Markus Mirz's avatar
Markus Mirz committed
46
    timeseries_list = {}
47 48 49 50 51 52 53 54 55 56 57 58 59
    cmpl_result_columns = []
    real_result_columns = []

    if timeseries_names is None:
        # No column names specified, thus read in all and strip off spaces
        pd_df.rename(columns=lambda x: x.strip(), inplace=True)
        column_names = list(pd_df.columns.values)

        # Remove timestamps column name and store separately
        column_names.remove('time')
        timestamps = pd_df.iloc[:, 0]

        # Find real and complex variable names
60
        suffixes = [ ('_re', '_im'), ('.real', '.imag') ]
61
        for column in column_names:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            is_complex = False
            for suffix in suffixes:
                real_suffix = suffix[0]
                imag_suffix = suffix[1]

                if column.endswith(imag_suffix):
                    is_complex = True
                    break # Ignore imag columns

                if column.endswith(real_suffix):
                    is_complex = True
                    column_base = column.replace(real_suffix, '')

                    if column_base + imag_suffix not in column_names:
                        continue

                    cmpl_result_columns.append(column_base)
                    timeseries_list[column_base] = TimeSeries(column_base, timestamps,
                        np.vectorize(complex)(
                            pd_df[column_base + real_suffix],
                            pd_df[column_base + imag_suffix]
                        )
                    )
                    break

            if is_complex:
                continue

            real_result_columns.append(column)
Markus Mirz's avatar
Markus Mirz committed
91
            timeseries_list[column] = TimeSeries(column, timestamps, pd_df[column])
92

93 94 95 96
    else:
        # Read in specified time series
        print('cannot read specified columns yet')

Jan Dinkelbach's avatar
Jan Dinkelbach committed
97
    if print_status :
98 99 100 101
        print('column number: ' + str(len(timeseries_list)))
        print('results length: ' + str(len(timestamps)))
        print('real column names: ' + str(real_result_columns))
        print('complex column names: ' + str(cmpl_result_columns))          
102 103

    return timeseries_list
Jan Dinkelbach's avatar
Jan Dinkelbach committed
104

105 106 107 108 109 110
def read_timeseries_dpsim(filename, timeseries_names=None, print_status=True):
    return read_timeseries_csv(filename, timeseries_names, print_status)

def read_timeseries_simulink(filename, timeseries_names=None, print_status=True):
    return read_timeseries_csv(filename, timeseries_names, print_status)

Jan Dinkelbach's avatar
Jan Dinkelbach committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
def read_dpsim_log(log_path):
    log_file = open(log_path, "r")
    log_lines = [line for line in log_file]
    log_file.close()

    # Sectionize
    log_sections = {'init':[], 'none':[], 'sysmat_stamp':[], 'sysmat_final':[], 'sourcevec_stamp':[], 'sourcevec_final':[], 'ludecomp':[]}
    section = 'init'
    for line_pos in range(len(log_lines)):
        if re.search('DEBUG: Stamping .+ into system matrix:', log_lines[line_pos]):
            section = 'sysmat_stamp'
        elif re.search('INFO: System matrix:', log_lines[line_pos]):
            section = 'sysmat_final'
        elif re.search('DEBUG: Stamping .+ into source vector:', log_lines[line_pos]):
            section = 'sourcevec_stamp'
        elif re.search('INFO: Right side vector:', log_lines[line_pos]):
            section = 'sourcevec_final'
        elif re.search('INFO: LU decomposition:', log_lines[line_pos]):
            section = 'ludecomp'
130
        elif re.search('INFO: Number of network simulation nodes:', log_lines[line_pos]):
Jan Dinkelbach's avatar
Jan Dinkelbach committed
131 132 133 134 135 136 137
            section = 'none'
        elif re.search('INFO: Added .+ to simulation.', log_lines[line_pos]):
            section = 'none'
        elif re.search('INFO: Initial switch status:', log_lines[line_pos]):
            section = 'none'
        log_sections[section].append(line_pos)

138
    return log_lines, log_sections