powersim

package module
v0.0.0-...-5763448 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 5 Imported by: 0

README

Power Simulation

Power simulation can simulate power consumers and calculate total energy consumption. For example model the power consumers in a house hold and calculate the consumed energy.

The simulation is written in Go and the simulation function RunSim return's a Go Channel that the caller can subscribe on. For every time step in the simulation the simulation state is reported on the Channel.

Example

// create one consumer running for 10 minutes
// every morning at 07.00 with power of 1000 W
c, err := NewCronConsumer(CronConsumer{
    Power:       1000,
    Duration:    time.Minute * 10,
    Sched:       "30 7 * * *",
    Description: "Hair dryer",
})

if err != nil {
    panic(err)
}

ctx := context.TODO()

// run the simulation from 2022-01-01 to 2022-01-02
ch := RunSim(ctx, Options{
    Consumers: []Consumer{c},
    StartTime: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
    EndTime:   time.Date(2022, 1, 2, 0, 0, 0, 0, time.UTC),
    Dt:        time.Second,
})

e := 0

// read the output from the simulation as it progresses
for r := range ch {
    // fmt.Println(r.T) // this will be the current time in the simulation
    // fmt.Println(r.P) // this will be the current power at time T in the simulation
    // fmt.Println(r.E) // this will be the accumulated energy consumption over the simulation
    e = r.E
}

fmt.Println(e)
// Output: 600000

Documentation

Overview

Example
// create one consumer running for 10 minutes
// every morning at 07.00 with power of 1000 W
c, err := NewCronConsumer(CronConsumer{
	Power:       1000,
	Duration:    time.Minute * 10,
	Sched:       "30 7 * * *",
	Description: "Hair dryer",
})

if err != nil {
	panic(err)
}

ctx := context.TODO()

// run the simulation from 2022-01-01 to 2022-01-02
ch := RunSim(ctx, Options{
	Consumers: []Consumer{c},
	StartTime: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
	EndTime:   time.Date(2022, 1, 2, 0, 0, 0, 0, time.UTC),
	Dt:        time.Second,
})

e := 0

// read the output from the simulation as it progresses
for r := range ch {
	// fmt.Println(r.T) // this will be the current time in the simulation
	// fmt.Println(r.P) // this will be the current power at time T in the simulation
	// fmt.Println(r.E) // this will be the accumulated energy consumption over the simulation
	e = r.E
}

fmt.Println(e)
Output:

600000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunSim

func RunSim(ctx context.Context, options Options) chan Result

RunSim runs a simulation over the period of time specified in the options, ctx respects cancellation.

Types

type Consumer

type Consumer interface {
	GetDescription() string
	GetPower(e Environment) int
}

type CronConsumer

type CronConsumer struct {
	Power       int
	Sched       string
	Duration    time.Duration
	Description string
	CronShed    cron.Schedule
	// contains filtered or unexported fields
}

func NewCronConsumer

func NewCronConsumer(c CronConsumer) (*CronConsumer, error)

NewCronConsumer return's a new cron consumer. The cron schedule specifies when the consumer is turned on. Cron schedule syntax "minute hour dayOfMonth month dayOfWeek" The duration determines how long the consumer in on and the power deterines the consumers constant power.

func (*CronConsumer) GetDescription

func (s *CronConsumer) GetDescription() string

func (*CronConsumer) GetPower

func (s *CronConsumer) GetPower(e Environment) int

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

type Options

type Options struct {
	Consumers []Consumer
	StartTime time.Time
	EndTime   time.Time
	Dt        time.Duration
}

Options represents simulation options. Dt represents the time increment and resolution used in the simulation The simulation takes any Consumer that implements the Consumer interface

type Result

type Result struct {
	P int
	E int
	T time.Time
}

Result contains P (power in Watt), E (energy in Joule) and T time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL