4. The Simulation Tools

The following section describes the tools provide by Gridsim to develop in a better way.

This page contains information about:

4.1. Time series

Module author: Gillian Basso <gillian.basso@hevs.ch>

Code author: Michael Clausen <clm@hevs.ch>

Code author: Gillian Basso <gillian.basso@hevs.ch>

The gridsim.timeseries module allows to import time series based on data Reader as attributes.

The data are translated into attributes of the TimeSeries by calling the TimeSeries.load() method on a TimeSeriesObject or any subclass of it:

obj = TimeSeries()
obj.load('../../data/examples/example.csv')

This will call the gridsim.iodata.input.Reader.load() function and process the data to simplify the access from the simulator.

It is important to have a time data or to identify the data representing the time.

If the data contain a temperature data, it can be access with:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from gridsim.unit import units
from gridsim.iodata.input import CSVReader
from gridsim.timeseries import TimeSeriesObject

# Load time series into a new object.
obj = TimeSeriesObject(CSVReader('./data/example.csv'))
obj.load()

# Print value at start time.
print obj.temperature

# Proceed 120 and output the temperature at that moment.
obj.set_time(120*units.second)
print obj.temperature

This would give an output like this:

20.0
18.5
class gridsim.timeseries.TimeSeries(self, reader)

Bases: object

The time series objects allows to process data from Reader into a time series and allows to access these in a very simple way by providing them as attributes.

Example:

from gridsim.unit import units
from gridsim.core import AbstractSimulationElement, AbstractSimulationModule
from gridsim.simulation import Simulator
from gridsim.recorder import PlotRecorder
from gridsim.timeseries import TimeSeriesObject
from gridsim.iodata.input import CSVReader
from gridsim.iodata.output import FigureSaver


# Create own simulation element and module and
# register them within the simulator.
class MyObject(AbstractSimulationElement):

    def __init__(self, friendly_name, reader):
        super(MyObject, self).__init__(friendly_name)
        self._time_series = TimeSeriesObject(reader)
        self._time_series.load(time_converter=lambda t: t*units.day)

    def __getattr__(self, item):
        return getattr(self._time_series, item)

    def reset(self):
        pass

    def calculate(self, time, delta_time):
        pass

    def update(self, time, delta_time):
        self._time_series.set_time(time)

    def convert(self, item, converter):
        return self._time_series.convert(item, converter)


class MyModule(AbstractSimulationModule):
    def __init__(self):
        self._elements = []
        super(MyModule, self).__init__()

    def add(self, element):
        self._elements.append(element)
        return element

    def attribute_name(self):
        return 'my'

    def all_elements(self):
        return []

    def reset(self):
        pass

    def calculate(self, time, delta_time):
        pass

    def update(self, time, delta_time):
        for el in self._elements:
            el.update(time, delta_time)

Simulator.register_simulation_module(MyModule)


# Create a simulator, add an element and record the temperature signal using
# a recorder.
sim = Simulator()

print("Loading data...")

obj = sim.my.add(MyObject("myObject", CSVReader('./data/example_time_series.csv')))
obj.convert("temperature", lambda t: units(t, units.degC))

rec = PlotRecorder('temperature', units.month, units.kelvin)
sim.record(rec, obj)

print("Running simulation...")

sim.run(units.year, units.day)

print("Saving data...")

FigureSaver(rec, "Temperature").save('./output/timeseries2-example.png')

The result of the script:

_images/timeseries2-example.png
max_time
map_attribute(self, name, mapped_name, is_time_key=False)

If the attribute name of the file does not match the target attribute name for the object, you can use this method to map the original name to a new attribute name.

Warning

The old mapping will be removed.

Example:

obj.map('temp', 'temperature')

The example will change the name of the attribute mapped to the data column from ‘temp’ to ‘temperature’.

Warning

the old value is delete from the TimeSeries

Parameters:
  • name (str) – Original name of the attribute/column.
  • mapped_name (str) – New name for the attribute.
  • is_time_key (bool) – should be True if the modified name is the time key.
convert(self, item, converter)

Convert each element of the list mapped by item with the convert function.

Parameters:
  • item (str) – the key of the list to convert
  • converter (function) – the conversion function
set_time(self, time=0)

Changes the actual time on the object.

Parameters:time (float, int or time, see gridsim.unit) – the new time.
load(*args, **keywords)
class gridsim.timeseries.TimeSeriesObject(*args, **keywords)

Bases: gridsim.timeseries.TimeSeries

This class is the standard time series implementation with no assumption about data.

This class has a TimeSeriesObject.compile_data() function to optimize the process but is still slow.

map_attribute(self, name, mapped_name, is_time_key=False)
Parameters:
  • name (str) – Original name of the attribute/column in CSV file.
  • mapped_name (str) – New name for the attribute.
  • is_time_key (bool) – should be True if the modified name is the time key.
convert(self, item, converter)

Convert each element of the list mapped by item with the convert function.

Parameters:
  • item (str) – the key of the list to convert
  • converter (function) – the conversion function
set_time(self, time=0)

Changes the actual time on the object.

Parameters:time (float, int or time, see gridsim.unit) – the new time.
load(self, time_converter=None, time_key='time')
Parameters:
  • time_converter
  • time_key
Returns:

class gridsim.timeseries.SortedConstantStepTimeSeriesObject(self, reader)

Bases: gridsim.timeseries.TimeSeries

This class is a time series with a constant time step between each data.

map_attribute(self, name, mapped_name, is_time_key=False)
Parameters:
  • name (str) – Original name of the attribute/column in CSV file.
  • mapped_name (str) – New name for the attribute.
  • is_time_key (bool) – should be True if the modified name is the time key.
set_time(self, time=0)

Changes the actual time on the object.

Parameters:time (float, int or time, see gridsim.unit) – the new time.
load(*args, **keywords)

4.2. Decorators

Module author: Gillian Basso <gillian.basso@hevs.ch>

Gridsim decorators module. Defines all decorators used in the Gridsim simulator.

gridsim.decorators.timed()

This decorator can be used to register the execution time of class methods.

Example:

class MyClass(object):
    @timed
    def my_short_func(arg1, arg2):
        return arg1 * arg2

    @timed
    def my_long_func(arg1, arg2):
        # long section of code
        ...

Warning

This decorator only works with class methods and not with functions.

At the end of the execution, the console prompt the time for each function:

Function MyClass.my_short_func 1 times.  Execution time max: 0.0000119, average: 0.0000119 Total time: 0.0000119
Function MyClass.my_long_func called 13 times.  Execution time max: 0.0001291, average: 0.0001177 Total time: 0.0015298
gridsim.decorators.accepts()

Type Enforcement. Verifies types of parameters given to the function.

Example:

@accepts((0,int), (1,(int,float)))
def func(arg1, arg2):
    return arg1 * arg2

Warning

In a class function, the current class cannot be used (e.g.: self type cannot be defined with accepts)

gridsim.decorators.returns()

Type Enforcement. Verifies return type of the function.

Example:

@returns((int,float))
def func(arg1, arg2):
    return arg1 * arg2
gridsim.decorators.deprecated()

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.

Example:

@deprecated
def func(arg1, arg2):
    return arg1 * arg2

4.3. Utils

Module author: Gillian Basso <gillian.basso@hevs.ch>

Code author: Michael Clausen <clm@hevs.ch>

Gridsim util module. Defines utility classes and functions.

class gridsim.util.Position(*args, **keywords)

Represents an abstract position on a three-dimensional space. The coordinate system is WGS84.

Parameters:
  • latitude (int, float) – North-south point location the on the Earth’s surface.
  • longitude (int, float) – East-west position of a point on the Earth’s surface.
  • altitude (int, float) – Altitude. We recommend to use sea level as reference.

Example:

from gridsim.util import Position

# Create a point and output the coordinates.
office = Position(46.240301, 7.358394, 566)
print office

# Create a second point.
home = Position(46.309180, 7.972517, 676)

# Calculate the difference between the two points.
print home.distance_to(office)

Output:

Position: {
    latitude: 46,
    longitude: 7,
    altitude: 566
}
latitude = None

Specifies the north-south point location on the Earth’s surface.

longitude = None

Specifies the east-west point position of on the Earth’s surface.

altitude = None

Altitude, we recommend to use the WGS84’s sea level as reference.

distance_to(*args, **keywords)
Calculates the distance between two points in meters [m]. It uses the
haversine formula to calculate the distance, so the altitude is ignored completely and the distance is calculated for booth points using the earth radius of 6371 km.
Parameters:other (Position) – The point to which the distance has to be calculated.
Returns:The distance between the two points in meters [m].
class gridsim.util.Material(self, c, p, k)

Warning

This class cannot be instanced directly. Use sub classes instead. Each Material must inherit this class.

This class offer simple access to constants of materials.

Example:

>>> from gridsim.util import Air
>>> print Air().thermal_conductivity
0.02587 thermal_conductivity

With thermal_conductivity is W/Km, see gridsim.unit for more information.

Parameters:
  • c (int, float) – The thermal capacity in J/kgK.
  • p (int, float) – The weight of the material in g/m3.
  • k (int, float) – The thermal conductivity in W/Km.
Returns:

thermal_capacity

The thermal capacity in [J/kgK] In order to get the thermal capacity of an object, you only have to multiply this specific capacity with the object’s mass in gram:

# Calculate the thermal capacity of 1kg of air.
t_cap = AIR().thermal_capacity
Returns:the thermal capacity
Return type:thermal capacity, see gridsim.unit
weight

The weight of the material in [kg/m3].

In order to get the mass of an object, you have just to multiply this specific wight with the object’s volume in m3:

# Calculate the mass capacity of 1m3 of air.
t_cap = Air().weight * 1
Returns:the weight of the material
Return type:weight, see gridsim.unit
thermal_conductivity

The thermal conductivity in [W/Km].

In order to get the thermal conductivity of an object, you have just to multiply this specific conductivity with the object’s area in m2 and divide it with the objects thickness:

# Calculate the thermal conductivity of a window with 1m2 surface
# and a thickness of 3mm.
t_cond = Glass().thermal_conductivity * 1 / 0.003
Returns:The thermal conductivity
Return type:thermal conductivity, see gridsim.unit
class gridsim.util.Steel

Implementation of steel:

  • Thermal capacity: 490 J/kgK
  • Weight: 7700 kg/m3
  • Thermal conductivity 46.6 W/Km
class gridsim.util.Stone

Implementation of stone:

  • Thermal capacity: 750 J/kgK
  • Weight: 2515 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Gold

Implementation of gold:

  • Thermal capacity: 130 J/kgK
  • Weight: 19200 kg/m3
  • Thermal conductivity 317.1 W/Km
class gridsim.util.Copper

Implementation of copper:

  • Thermal capacity: 383 J/kgK
  • Weight: 2200 kg/m3
  • Thermal conductivity 401.2 W/Km
class gridsim.util.Petrol

Implementation of petrol:

  • Thermal capacity: 2140 J/kgK
  • Weight: 881 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Wax

Implementation of wax:

  • Thermal capacity: 3430 J/kgK
  • Weight: unknown (None)
  • Thermal conductivity unknown (None)
class gridsim.util.Sandstone

Implementation of sandstone:

  • Thermal capacity: 920 J/kgK
  • Weight: 2323 kg/m3
  • Thermal conductivity 2.5 W/Km
class gridsim.util.Cobalt

Implementation of cobalt:

  • Thermal capacity: 460 J/kgK
  • Weight: 8900 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Zinc

Implementation of zinc:

  • Thermal capacity: 380 J/kgK
  • Weight: 7140 kg/m3
  • Thermal conductivity 11 W/Km
class gridsim.util.Marple

Implementation of marple:

  • Thermal capacity: 880 J/kgK
  • Weight: 2563 kg/m3
  • Thermal conductivity 2.08 W/Km
class gridsim.util.Granite

Implementation of granite:

  • Thermal capacity: 790 J/kgK
  • Weight: 2400 kg/m3
  • Thermal conductivity 2.855 W/Km
class gridsim.util.Silk

Implementation of silk:

  • Thermal capacity: 1380 J/kgK
  • Weight: unknown (None)
  • Thermal conductivity unknown (None)
class gridsim.util.Hydrogen

Implementation of hydrogen:

  • Thermal capacity: 14320 J/kgK
  • Weight: 69600 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.HardBrick

Implementation of hard brick:

  • Thermal capacity: 1000 J/kgK
  • Weight: 2403 kg/m3
  • Thermal conductivity 1.31 W/Km
class gridsim.util.Platinum

Implementation of platinum:

  • Thermal capacity: 130 J/kgK
  • Weight: 21450 kg/m3
  • Thermal conductivity 71.61 W/Km
class gridsim.util.Aluminium

Implementation of aluminium:

  • Thermal capacity: 896 J/kgK
  • Weight: 1522 kg/m3
  • Thermal conductivity 236.9 W/Km
class gridsim.util.ArtificialWool

Implementation of artificial wool:

  • Thermal capacity: 1357 J/kgK
  • Weight: 1314 kg/m3
  • Thermal conductivity 0.049 W/Km
class gridsim.util.Tar

Implementation of tar:

  • Thermal capacity: 1470 J/kgK
  • Weight: 1153 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Chromium

Implementation of chromium:

  • Thermal capacity: 500 J/kgK
  • Weight: 6856 kg/m3
  • Thermal conductivity 93.93 W/Km
class gridsim.util.Slate

Implementation of slate:

  • Thermal capacity: 760 J/kgK
  • Weight: 2691 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.DryEarth

Implementation of dry earth:

  • Thermal capacity: 1260 J/kgK
  • Weight: 1249 kg/m3
  • Thermal conductivity 0.864 W/Km
class gridsim.util.Rubber

Implementation of rubber:

  • Thermal capacity: 2010 J/kgK
  • Weight: 1522 kg/m3
  • Thermal conductivity 0.16 W/Km
class gridsim.util.Concrete

Implementation of concrete:

  • Thermal capacity: 750 J/kgK
  • Weight: 2403 kg/m3
  • Thermal conductivity 1.04 W/Km
class gridsim.util.Pvc

Implementation of pvc:

  • Thermal capacity: 880 J/kgK
  • Weight: 1200 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Paper

Implementation of paper:

  • Thermal capacity: 1336 J/kgK
  • Weight: 1201 kg/m3
  • Thermal conductivity 0.05 W/Km
class gridsim.util.Graphite

Implementation of graphite:

  • Thermal capacity: 710 J/kgK
  • Weight: 2070 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Iron

Implementation of iron:

  • Thermal capacity: 452 J/kgK
  • Weight: 2500 kg/m3
  • Thermal conductivity 80.43 W/Km
class gridsim.util.Clay

Implementation of clay:

  • Thermal capacity: 920 J/kgK
  • Weight: 1073 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.GraphiteCarbon

Implementation of graphite carbon:

  • Thermal capacity: 710 J/kgK
  • Weight: unknown (None)
  • Thermal conductivity unknown (None)
class gridsim.util.Salt

Implementation of salt:

  • Thermal capacity: 880 J/kgK
  • Weight: 1000 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Mercury

Implementation of mercury:

  • Thermal capacity: 140 J/kgK
  • Weight: 13534 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Charcoal

Implementation of charcoal:

  • Thermal capacity: 1000 J/kgK
  • Weight: 208 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Oil

Implementation of oil:

  • Thermal capacity: 1670. J/kgK
  • Weight: 942 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Nickel

Implementation of nickel:

  • Thermal capacity: 461 J/kgK
  • Weight: 8666 kg/m3
  • Thermal conductivity 90.95 W/Km
class gridsim.util.Silicone

Implementation of silicone:

  • Thermal capacity: 750 J/kgK
  • Weight: 2330 kg/m3
  • Thermal conductivity 0.3 W/Km
class gridsim.util.DryCement

Implementation of dry cement:

  • Thermal capacity: 1550 J/kgK
  • Weight: 1506 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Cork

Implementation of cork:

  • Thermal capacity: 1900 J/kgK
  • Weight: 240 kg/m3
  • Thermal conductivity 0.0435 W/Km
class gridsim.util.Chalk

Implementation of chalk:

  • Thermal capacity: 900 J/kgK
  • Weight: 2499 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Gypsum

Implementation of gypsum:

  • Thermal capacity: 1090 J/kgK
  • Weight: 2787 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Wood

Implementation of wood:

  • Thermal capacity: 2000 J/kgK
  • Weight: 500 kg/m3
  • Thermal conductivity 0.14 W/Km
class gridsim.util.GlassWool

Implementation of glass wool:

  • Thermal capacity: 670 J/kgK
  • Weight: 25 kg/m3
  • Thermal conductivity 0.04 W/Km
class gridsim.util.Butane

Implementation of butane:

  • Thermal capacity: 1658 J/kgK
  • Weight: 2.006 kg/m3
  • Thermal conductivity 0.01607 W/Km
class gridsim.util.Tungsten

Implementation of tungsten:

  • Thermal capacity: 134 J/kgK
  • Weight: 19220 kg/m3
  • Thermal conductivity 174.2 W/Km
class gridsim.util.Air

Implementation of air:

  • Thermal capacity: 1005 J/kgK
  • Weight: 1.200 kg/m3
  • Thermal conductivity 0.02587 W/Km
class gridsim.util.Helium

Implementation of helium:

  • Thermal capacity: 5193 J/kgK
  • Weight: 138 kg/m3
  • Thermal conductivity 0.1535 W/Km
class gridsim.util.Silver

Implementation of silver:

  • Thermal capacity: 230 J/kgK
  • Weight: 10500 kg/m3
  • Thermal conductivity 429.0 W/Km
class gridsim.util.Diamond

Implementation of diamond:

  • Thermal capacity: 630 J/kgK
  • Weight: 3510 kg/m3
  • Thermal conductivity 2.2 W/Km
class gridsim.util.Lead

Implementation of lead:

  • Thermal capacity: 129 J/kgK
  • Weight: 11389 kg/m3
  • Thermal conductivity 35.33 W/Km
class gridsim.util.Asphalt

Implementation of asphalt:

  • Thermal capacity: 920. J/kgK
  • Weight: 721 kg/m3
  • Thermal conductivity 0.75 W/Km
class gridsim.util.LightConcrete

Implementation of light concrete:

  • Thermal capacity: 960 J/kgK
  • Weight: 1400 kg/m3
  • Thermal conductivity 0.42 W/Km
class gridsim.util.Plaster

Implementation of plaster:

  • Thermal capacity: 1300 J/kgK
  • Weight: 849 kg/m3
  • Thermal conductivity 0.478 W/Km
class gridsim.util.CommonBrick

Implementation of common brick:

  • Thermal capacity: 900 J/kgK
  • Weight: 1922 kg/m3
  • Thermal conductivity 1.26 W/Km
class gridsim.util.Water

Implementation of water:

  • Thermal capacity: 4190 J/kgK
  • Weight: 1000 kg/m3
  • Thermal conductivity 0.5985 W/Km
class gridsim.util.Glass

Implementation of glass:

  • Thermal capacity: 840 J/kgK
  • Weight: 2579 kg/m3
  • Thermal conductivity 1.0 W/Km
class gridsim.util.DrySoil

Implementation of dry soil:

  • Thermal capacity: 800 J/kgK
  • Weight: unknown (None)
  • Thermal conductivity unknown (None)
class gridsim.util.Ethanol

Implementation of ethanol:

  • Thermal capacity: 2430 J/kgK
  • Weight: 789 kg/m3
  • Thermal conductivity 0.1664 W/Km
class gridsim.util.Carbon

Implementation of carbon:

  • Thermal capacity: 520 J/kgK
  • Weight: 2146 kg/m3
  • Thermal conductivity 0.0146 W/Km
class gridsim.util.WetSoil

Implementation of wet soil:

  • Thermal capacity: 1480 J/kgK
  • Weight: unknown (None)
  • Thermal conductivity unknown (None)
class gridsim.util.Wool

Implementation of wool:

  • Thermal capacity: 1260 J/kgK
  • Weight: 1314 kg/m3
  • Thermal conductivity 0.049 W/Km
class gridsim.util.Porcelain

Implementation of porcelain:

  • Thermal capacity: 1070 J/kgK
  • Weight: 2403 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.DryLeather

Implementation of dry leather:

  • Thermal capacity: 1500 J/kgK
  • Weight: 945 kg/m3
  • Thermal conductivity unknown (None)
class gridsim.util.Aerogel

Implementation of aerogel:

  • Thermal capacity: unknown (None)
  • Weight: unknown (None)
  • Thermal conductivity 0.003 W/Km