Initial commit
parent
0def711fcf
commit
98dd45efbb
|
@ -1,3 +1,6 @@
|
||||||
# flutter_demo_energy
|
# Beispiel: Energieberechnung & Tests
|
||||||
|
|
||||||
|
Aktuell enthält das Repo nur die Business-Logik und die Tests der Energy-App.
|
||||||
|
Dieser Code ist unabhängig von Flutter. Die Tests können daher sowohl mit
|
||||||
|
`flutter test` als auch `dart test` ausgeführt werden.
|
||||||
|
|
||||||
Flutter Beispiel Energie-Berechung
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
# This file configures the analyzer, which statically analyzes Dart code to
|
||||||
|
# check for errors, warnings, and lints.
|
||||||
|
#
|
||||||
|
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
|
||||||
|
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
|
||||||
|
# invoked from the command line by running `flutter analyze`.
|
||||||
|
|
||||||
|
# The following line activates a set of recommended lints for Flutter apps,
|
||||||
|
# packages, and plugins designed to encourage good coding practices.
|
||||||
|
include: package:flutter_lints/flutter.yaml
|
||||||
|
|
||||||
|
linter:
|
||||||
|
# The lint rules applied to this project can be customized in the
|
||||||
|
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
|
||||||
|
# included above or to enable additional rules. A list of all available lints
|
||||||
|
# and their documentation is published at
|
||||||
|
# https://dart-lang.github.io/linter/lints/index.html.
|
||||||
|
#
|
||||||
|
# Instead of disabling a lint rule for the entire project in the
|
||||||
|
# section below, it can also be suppressed for a single line of code
|
||||||
|
# or a specific dart file by using the `// ignore: name_of_lint` and
|
||||||
|
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
|
||||||
|
# producing the lint.
|
||||||
|
rules:
|
||||||
|
# avoid_print: false # Uncomment to disable the `avoid_print` rule
|
||||||
|
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
|
||||||
|
|
||||||
|
# Additional information about this file can be found at
|
||||||
|
# https://dart.dev/guides/language/analysis-options
|
|
@ -0,0 +1,30 @@
|
||||||
|
/// free fall acceleration in m/s²
|
||||||
|
const double g = 9.81;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Potential Energy = [mass] * [height] * g
|
||||||
|
/// with [mass] in kg, [height] in m and result in m²kg/s² = Joule
|
||||||
|
double potential(double mass, double height) {
|
||||||
|
return mass * height * g;
|
||||||
|
}
|
||||||
|
|
||||||
|
double freedPotential(double mass, double hStart, double hEnd) {
|
||||||
|
return potential(mass, hStart) - potential(mass, hEnd);
|
||||||
|
}
|
||||||
|
///
|
||||||
|
/// Kinetic energy = 0.5 [mass] * [velocity] * [velocity]
|
||||||
|
/// with [mass] in kg and velocity in m/s and result in m²kg/s² = Joule
|
||||||
|
double kinetic(double mass, double velocity) {
|
||||||
|
return mass * velocity * velocity / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
double freedKinetic(double mass, double vStart, double vEnd) {
|
||||||
|
return kinetic(mass, vStart) - kinetic(mass, vEnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Amount of freed energy when an object changes its height from [hStart] m to [hEnd] m
|
||||||
|
/// and changes its velocity from [vStart] m/s² to [vEnd] m/s².
|
||||||
|
double freedEnergy(double mass, double hStart, double hEnd, double vStart, double vEnd) {
|
||||||
|
return freedPotential(mass, hStart, hEnd) + freedKinetic(mass, vStart, vEnd);
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
/// energyNeededToHeatAParticularMaterial = tempDiff * specificHeatCapacity (measued in J/kg)
|
||||||
|
/// we have some amount of energy e
|
||||||
|
/// e / energyNeededToHeatAParticularMaterial => amount of material which can be heated (in kg)
|
||||||
|
/// for H2H: 1 l = 1 kg
|
||||||
|
double _kg(double energy, double specHeatCapacity, double tempDiff) {
|
||||||
|
return energy / (tempDiff * specHeatCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Notes
|
||||||
|
/// Eine PKW-Bremscheibe ca. 6.5kg
|
||||||
|
/// LKW Actros: 34kg
|
||||||
|
/// Calculates the amount of water in Liter that can be heated
|
||||||
|
/// from 20°C to 100°C with the given [engergy] in Joule.
|
||||||
|
///
|
||||||
|
double literWaterThatCanBeBoiled(double energy) {
|
||||||
|
const double specHeatCapaH2O = 4180; // J(kg*K)
|
||||||
|
const double tempDiffH20 = 100 - 20; // K
|
||||||
|
return _kg(energy, specHeatCapaH2O, tempDiffH20);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Calculates the amount of iron in kg that can be heated
|
||||||
|
/// from 20°C to ???°C with the given [engergy] in Joule.
|
||||||
|
///
|
||||||
|
double kgIronThatCanBeHeatetUntilRed(double energy) {
|
||||||
|
const double specHeatCapaIron = 500; // J/(kg*K)
|
||||||
|
const double tempDiffIron = 700 - 20; // K
|
||||||
|
return _kg(energy, specHeatCapaIron, tempDiffIron);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
const _factor = 3.6;
|
||||||
|
toKmPerHour(double mPerSecond) => mPerSecond * _factor;
|
||||||
|
tomPerSecond(double kmPerHour) => kmPerHour / _factor;
|
|
@ -0,0 +1,19 @@
|
||||||
|
name: energy
|
||||||
|
description: Demo Application
|
||||||
|
|
||||||
|
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
||||||
|
|
||||||
|
version: 1.0.0+1
|
||||||
|
|
||||||
|
environment:
|
||||||
|
sdk: ">=2.18.0 <3.0.0"
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
flutter:
|
||||||
|
sdk: flutter
|
||||||
|
|
||||||
|
dev_dependencies:
|
||||||
|
test:
|
||||||
|
|
||||||
|
flutter_lints: ^2.0.1
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
import 'package:energy/models/energy.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
const double oneTon = 1000.0; // kg
|
||||||
|
const double tenToStop = 50000.0; // J
|
||||||
|
const double oneTonTenMetersDown = 98100.0; // J
|
||||||
|
|
||||||
|
group('energy', () {
|
||||||
|
test('none', () {
|
||||||
|
expect(freedEnergy(oneTon, 0, 0, 0, 0), 0);
|
||||||
|
expect(freedEnergy(oneTon, 0, 0, 20, 20), 0);
|
||||||
|
});
|
||||||
|
group('potential', () {
|
||||||
|
test('10 m down', () {
|
||||||
|
expect(freedEnergy(oneTon, 10, 0, 0, 0), oneTonTenMetersDown);
|
||||||
|
expect(freedPotential(oneTon, 10, 0), oneTonTenMetersDown);
|
||||||
|
});
|
||||||
|
test('10 m up', () {
|
||||||
|
expect(freedEnergy(oneTon, 0, 10, 0, 0), -oneTonTenMetersDown);
|
||||||
|
expect(freedPotential(oneTon, 0, 10), -oneTonTenMetersDown);
|
||||||
|
});
|
||||||
|
test('20 m down', () {
|
||||||
|
expect(freedEnergy(oneTon, 2 * 10, 0, 0, 0), 2 * oneTonTenMetersDown);
|
||||||
|
expect(potential(oneTon, 2 * 10), 2 * oneTonTenMetersDown);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('kinetic', () {
|
||||||
|
test('10 to 0', () {
|
||||||
|
expect(freedEnergy(oneTon, 0, 0, 10, 0), tenToStop);
|
||||||
|
expect(freedKinetic(oneTon, 10, 0), tenToStop);
|
||||||
|
});
|
||||||
|
test('0 to 10', () {
|
||||||
|
expect(freedEnergy(oneTon, 0, 0, 0, 10), -tenToStop);
|
||||||
|
expect(freedKinetic(oneTon, 0, 10), -tenToStop);
|
||||||
|
});
|
||||||
|
test('20 to 0', () {
|
||||||
|
expect(freedEnergy(oneTon, 0, 0, 20, 0), 4 * tenToStop);
|
||||||
|
expect(freedKinetic(oneTon, 20, 0), 4 * tenToStop);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('sum', () {
|
||||||
|
test('10 down & 10 to 0', () {
|
||||||
|
expect(
|
||||||
|
freedEnergy(oneTon, 10, 0, 10, 0), oneTonTenMetersDown + tenToStop);
|
||||||
|
});
|
||||||
|
test('10 up & 0 to 10', () {
|
||||||
|
expect(
|
||||||
|
freedEnergy(oneTon, 0, 10, 0, 10), -(tenToStop + oneTonTenMetersDown));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('10 down & 0 to 10', () {
|
||||||
|
expect(
|
||||||
|
freedEnergy(oneTon, 10, 0, 0, 10), oneTonTenMetersDown - tenToStop);
|
||||||
|
});
|
||||||
|
test('10 up & 10 to 0', () {
|
||||||
|
expect(
|
||||||
|
freedEnergy(oneTon, 0, 10, 10, 0), tenToStop - oneTonTenMetersDown);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
import 'package:energy/models/heat_capacity.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('Joule to', () {
|
||||||
|
test('red heat 1 kg Iron', () {
|
||||||
|
expect(kgIronThatCanBeHeatetUntilRed(340000), 1);
|
||||||
|
});
|
||||||
|
test('boil 1 l Water', () {
|
||||||
|
expect(literWaterThatCanBeBoiled(334400), 1);
|
||||||
|
});});
|
||||||
|
}
|
Loading…
Reference in New Issue