From 98dd45efbb04e11db4260340f7cb5bee60abae46 Mon Sep 17 00:00:00 2001 From: Christoph Giess Date: Tue, 14 Feb 2023 08:06:33 +0100 Subject: [PATCH] Initial commit --- README.md | 7 ++-- analysis_options.yaml | 29 ++++++++++++++++ lib/models/energy.dart | 30 ++++++++++++++++ lib/models/heat_capacity.dart | 31 +++++++++++++++++ lib/models/velocity.dart | 3 ++ pubspec.yaml | 19 +++++++++++ test/energy_test.dart | 64 +++++++++++++++++++++++++++++++++++ test/heat_capacity_test.dart | 12 +++++++ 8 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 analysis_options.yaml create mode 100644 lib/models/energy.dart create mode 100644 lib/models/heat_capacity.dart create mode 100644 lib/models/velocity.dart create mode 100644 pubspec.yaml create mode 100644 test/energy_test.dart create mode 100644 test/heat_capacity_test.dart diff --git a/README.md b/README.md index bc2eb52..ff84705 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..61b6c4d --- /dev/null +++ b/analysis_options.yaml @@ -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 diff --git a/lib/models/energy.dart b/lib/models/energy.dart new file mode 100644 index 0000000..55cc0b1 --- /dev/null +++ b/lib/models/energy.dart @@ -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); +} \ No newline at end of file diff --git a/lib/models/heat_capacity.dart b/lib/models/heat_capacity.dart new file mode 100644 index 0000000..2f92dee --- /dev/null +++ b/lib/models/heat_capacity.dart @@ -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); +} + diff --git a/lib/models/velocity.dart b/lib/models/velocity.dart new file mode 100644 index 0000000..665ff7e --- /dev/null +++ b/lib/models/velocity.dart @@ -0,0 +1,3 @@ +const _factor = 3.6; +toKmPerHour(double mPerSecond) => mPerSecond * _factor; +tomPerSecond(double kmPerHour) => kmPerHour / _factor; diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..acbc66c --- /dev/null +++ b/pubspec.yaml @@ -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 + diff --git a/test/energy_test.dart b/test/energy_test.dart new file mode 100644 index 0000000..f9b1e41 --- /dev/null +++ b/test/energy_test.dart @@ -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); + }); + }); + }); +} diff --git a/test/heat_capacity_test.dart b/test/heat_capacity_test.dart new file mode 100644 index 0000000..067c20a --- /dev/null +++ b/test/heat_capacity_test.dart @@ -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); + });}); +}