Exemple #1
0
#!/usr/bin/python
# Example of SimRes.plot()

from modelicares import SimRes

sim = SimRes('ChuaCircuit.mat')
sim.plot(ynames1='L.i', ylabel1="Current",
         ynames2='L.der(i)', ylabel2="Derivative of current",
         title="Chua circuit")
Exemple #2
0
def static():
    """Create static images for the HTML documentation and the base README.md.
    """
    import matplotlib.pyplot as plt
    from modelicares import SimRes, LinResList, read_params

    join = os.path.join

    # Options
    indir = "../examples"  # Directory with the mat files.
    outdir = "_static"  # Directory where the images should be generated
    dpi = 90  # DPI for the HTML index images
    dpi_small = 30  # DPI for the README images
    kwargs = dict(bbox_inches='tight', format='png')  # Other savefig() options

    # ThreeTanks
    # ----------
    sim = SimRes(join(indir, 'ThreeTanks.mat'))
    sim.sankey(title=("Sankey diagrams of "
                      "Modelica.Fluid.Examples.Tanks.ThreeTanks"),
               times=[0, 50, 100, 150], n_rows=2, format='%.1f ',
               names=['tank1.ports[1].m_flow', 'tank2.ports[1].m_flow',
                      'tank3.ports[1].m_flow'],
               labels=['Tank 1', 'Tank 2', 'Tank 3'],
               orientations=[-1, 0, 1],
               scale=0.1, margin=6, offset=1.5,
               pathlengths=2, trunklength=10)
    plt.savefig(join(outdir, 'ThreeTanks.png'), dpi=dpi, **kwargs)
    plt.savefig(join(outdir, 'ThreeTanks-small.png'), dpi=dpi_small, **kwargs)
    plt.close()

    # ChuaCircuit
    # -----------
    sim = SimRes(join(indir, 'ChuaCircuit.mat'))
    ax = sim.plot(ynames1='L.v', ylabel1="Voltage",
                  xname='L.i', xlabel="Current",
                  title=("Modelica.Electrical.Analog.Examples.ChuaCircuit\n"
                         "Current through and voltage across the inductor"))[0]
    # Mark the start and stop points.
    def mark(time, text):
        """Mark a frequency point.
        """
        i = sim['L.i'].values(time)
        v = sim['L.v'].values(time)
        plt.plot(i, v, 'bo')
        ax.annotate(text, xy=(i, v), xytext=(0, -4), ha='center', va='top',
                    textcoords='offset points')
    mark(0, "Start")
    mark(2500, "Stop")
    # Save and close.
    plt.savefig(join(outdir, 'ChuaCircuit.png'), dpi=dpi, **kwargs)
    plt.savefig(join(outdir, 'ChuaCircuit-small.png'), dpi=dpi_small, **kwargs)
    plt.close()

    # PIDs-bode
    # ---------
    lins = LinResList(join(indir, 'PID.mat'), join(indir, 'PID/*/'))
    for lin in lins:
        lin.label = "Td = %g s" % read_params('Td', join(lin.dirname,
                                                         'dsin.txt'))
    lins.sort(key=lambda lin: lin.label)
    lins.bode(title=("Bode plot of Modelica.Blocks.Continuous.PID\n"
                     "with varying differential time constant"))
    plt.savefig(join(outdir, 'PIDs-bode.png'), dpi=dpi, **kwargs)
    plt.savefig(join(outdir, 'PIDs-bode-small.png'), dpi=dpi_small, **kwargs)
    plt.close()
# file: plotTestPWM.py
#!/usr/bin/python
import OMPython
import numpy as np
import matplotlib.pyplot as plt 
import pandas; pandas.set_option('display.max_rows', 5)
from modelicares import SimRes

sim = SimRes('Result_res.mat')
print sim['Time'].IV()
print sim['Time'].FV()
pulses = sim.find('pulse*.y')
sim.plot(pulses);
plt.savefig('test.png')
# plt.show()
# sim['pulseWidthVar.y'].array(t=(0, 0.2 ))
Exemple #4
0
def static():
    """Create static images for the HTML documentation and the base README.md.
    """
    import matplotlib.pyplot as plt
    from modelicares import SimRes, LinResList, read_params

    join = os.path.join

    # Options
    indir = "../examples"  # Directory with the mat files.
    outdir = "_static"  # Directory where the images should be generated
    dpi = 90  # DPI for the HTML index images
    dpi_small = 30  # DPI for the README images
    kwargs = dict(bbox_inches='tight', format='png')  # Other savefig() options

    # ThreeTanks
    # ----------
    sim = SimRes(join(indir, 'ThreeTanks.mat'))
    sim.sankey(title=("Sankey diagrams of "
                      "Modelica.Fluid.Examples.Tanks.ThreeTanks"),
               times=[0, 50, 100, 150],
               n_rows=2,
               format='%.1f ',
               names=[
                   'tank1.ports[1].m_flow', 'tank2.ports[1].m_flow',
                   'tank3.ports[1].m_flow'
               ],
               labels=['Tank 1', 'Tank 2', 'Tank 3'],
               orientations=[-1, 0, 1],
               scale=0.1,
               margin=6,
               offset=1.5,
               pathlengths=2,
               trunklength=10)
    plt.savefig(join(outdir, 'ThreeTanks.png'), dpi=dpi, **kwargs)
    plt.savefig(join(outdir, 'ThreeTanks-small.png'), dpi=dpi_small, **kwargs)
    plt.close()

    # ChuaCircuit
    # -----------
    sim = SimRes(join(indir, 'ChuaCircuit.mat'))
    ax = sim.plot(ynames1='L.v',
                  ylabel1="Voltage",
                  xname='L.i',
                  xlabel="Current",
                  title=("Modelica.Electrical.Analog.Examples.ChuaCircuit\n"
                         "Current through and voltage across the inductor"))[0]

    # Mark the start and stop points.
    def mark(time, text):
        """Mark a frequency point.
        """
        i = sim['L.i'].values(time)
        v = sim['L.v'].values(time)
        plt.plot(i, v, 'bo')
        ax.annotate(text,
                    xy=(i, v),
                    xytext=(0, -4),
                    ha='center',
                    va='top',
                    textcoords='offset points')

    mark(0, "Start")
    mark(2500, "Stop")
    # Save and close.
    plt.savefig(join(outdir, 'ChuaCircuit.png'), dpi=dpi, **kwargs)
    plt.savefig(join(outdir, 'ChuaCircuit-small.png'), dpi=dpi_small, **kwargs)
    plt.close()

    # PIDs-bode
    # ---------
    lins = LinResList(join(indir, 'PID.mat'), join(indir, 'PID/*/'))
    for lin in lins:
        lin.label = "Td = %g s" % read_params('Td',
                                              join(lin.dirname, 'dsin.txt'))
    lins.sort(key=lambda lin: lin.label)
    lins.bode(title=("Bode plot of Modelica.Blocks.Continuous.PID\n"
                     "with varying differential time constant"))
    plt.savefig(join(outdir, 'PIDs-bode.png'), dpi=dpi, **kwargs)
    plt.savefig(join(outdir, 'PIDs-bode-small.png'), dpi=dpi_small, **kwargs)
    plt.close()
    MODELS, RESULTS_DIR = write_script(EXPERIMENTS, working_dir=WORKING_DIR,
                                       packages=PACKAGES, fname=FNAME)

    # Ask Dymola to run the script.
    # For Linux:
    os.system('dymola ' + FNAME)
    # TODO: Add support for Windows.
    # For Windows:
    # os.system(r'C:\Program files\Dymola\bin\Dymola.exe ' + FNAME)
else:
    MODELS = [experiment.model[experiment.model.rfind('.') + 1:]
              for experiment in EXPERIMENTS]
    RESULTS_DIR = os.path.split(FNAME)[0]

# Create plots.
# Begin customize--------------------------------------------------------------

for i, model in enumerate(MODELS):
    sim = SimRes(os.path.join(RESULTS_DIR, str(i + 1), 'dsres.mat'))
    sim.plot(title="Chua Circuit with L = %.0f %s" % (sim['L.L'].IV(),
                                                      sim['L.L'].unit),
             ynames1=['L.i'], ylabel1='Current',
             ynames2=['L.der(i)'], ylabel2='Derivative of current',
             label=os.path.join(str(i + 1), model))

# End customize----------------------------------------------------------------

# Save the plots.
saveall(FORMATS)
plt.show()
#!/usr/bin/python
"""Example of SimRes.plot()
"""

# pylint: disable=I0011, C0103

from modelicares import SimRes

sim = SimRes('ChuaCircuit.mat')
sim.plot(ynames1='L.i', ylabel1="Current",
         ynames2='L.der(i)', ylabel2="Derivative of current",
         title="Chua circuit")
    # For Linux:
    os.system('dymola ' + FNAME)
    # TODO: Support Windows.
    # For Windows:
    # os.system(r'C:\Program files\Dymola\bin\Dymola.exe ' + FNAME)
else:
    MODELS = [
        experiment.model[experiment.model.rfind('.') + 1:]
        for experiment in EXPERIMENTS
    ]
    RESULTS_DIR = os.path.split(FNAME)[0]

# Create plots.
# Begin customize--------------------------------------------------------------

for i, model in enumerate(MODELS):
    sim = SimRes(os.path.join(RESULTS_DIR, str(i + 1), 'dsres.mat'))
    sim.plot(title="Chua Circuit with L = %.0f %s" %
             (sim['L.L'].IV(), sim['L.L'].unit),
             ynames1=['L.i'],
             ylabel1='Current',
             ynames2=['L.der(i)'],
             ylabel2='Derivative of current',
             label=os.path.join(str(i + 1), model))

# End customize----------------------------------------------------------------

# Save the plots.
saveall(FORMATS)
plt.show()
# file: plotTestPWM.py
#!/usr/bin/python
import OMPython
import numpy as np
import matplotlib.pyplot as plt
import pandas
pandas.set_option('display.max_rows', 5)
from modelicares import SimRes

sim = SimRes('Result_res.mat')
print sim['Time'].IV()
print sim['Time'].FV()
pulses = sim.find('pulse*.y')
sim.plot(pulses)
plt.savefig('test.png')
# plt.show()
# sim['pulseWidthVar.y'].array(t=(0, 0.2 ))