-
Notifications
You must be signed in to change notification settings - Fork 1
9.1.0-rc.1 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
9.1.0-rc.1 #8
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| namespace OpenTap.Plugins.Demo.Battery | ||
| { | ||
| [Display("Battery", "This DUT represents the battery itself.")] | ||
| public class BatteryDut : Dut | ||
| { | ||
| #region Settings | ||
|
|
||
| [Display("Capacity", "A larger cell size will result in faster charging and discharging.")] | ||
| [Unit("Ah")] | ||
| public double Capacity { get; set; } = 0.3; | ||
|
|
||
| [Display("Base Voltage", "The battery voltage when discharged.")] | ||
| [Unit("V")] | ||
| public double BaseVoltage { get; set; } = 3.0; | ||
|
|
||
| [Display("Charged Voltage", "The battery voltage when charged.")] | ||
| [Unit("V")] | ||
| public double ChargedVoltage { get; set; } = 4.2; | ||
|
|
||
| [Display("Initial Charge")] | ||
| [Unit("Ah")] | ||
| public double InitialCharge { get; set; } = 0.01; | ||
|
|
||
| #endregion | ||
|
|
||
| internal BatteryModel Model { get; private set; } | ||
| public BatteryDut() | ||
| { | ||
| Name = "Bat"; | ||
| Rules.Add(() => Capacity >= 0, "Capacity must be greater than 0", nameof(Capacity)); | ||
| Rules.Add(() => BaseVoltage >= 0, "Base Voltage must be greater than 0", nameof(BaseVoltage)); | ||
| Rules.Add(() => ChargedVoltage >= BaseVoltage, "Charted Voltage must be greater than 0", nameof(ChargedVoltage)); | ||
| Model = new BatteryModel(); | ||
| } | ||
|
|
||
| public override void Open() | ||
| { | ||
| Model = new BatteryModel(initialCharge_Ah: InitialCharge, capacity_Ah: Capacity, chargedVoltage: ChargedVoltage, baseVoltage: BaseVoltage); | ||
| base.Open(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| using System; | ||
|
|
||
| namespace OpenTap.Plugins.Demo.Battery | ||
| { | ||
| /// <summary> | ||
| /// This is a relatively inaccurate physical model of a Lithium-ion-like battery. | ||
| /// </summary> | ||
| class BatteryModel | ||
| { | ||
| // --- Physical constants --- | ||
| private const double Rgas = 8.314; // J/mol·K | ||
| private const double Tref = 298.15; // 25°C in Kelvin | ||
|
|
||
| // --- Simulation | ||
| private double baseVoltage = 3.0; | ||
| private double chargedVoltage = 4.2; | ||
|
|
||
| // --- Nominal parameters --- | ||
| private readonly double nominalCapacity; // Ah | ||
| private readonly double R25; // Internal resistance (Ohm) at 25°C | ||
| private readonly double beta; // Temperature coefficient for resistance | ||
| private readonly double Ea; // Activation energy (J/mol) | ||
| private readonly double selfDischarge25; // Self-discharge rate at 25°C (fraction/min) | ||
|
|
||
| // --- State variables --- | ||
| public double Charge_Ah { get; private set; } // Current stored charge | ||
| public double Voltage_V { get; private set; } // Terminal voltage | ||
| public double Current_A { get; private set; } // Charging (+) or discharging (-) | ||
| public double Voc { get; private set; } // Voltage open-circuit | ||
| public double SOC { get; private set; } // State of Charge (0–1) | ||
| public double CoulombicEfficiency { get; private set; } | ||
|
|
||
| public BatteryModel( | ||
| double capacity_Ah = 3.0, | ||
| double initialCharge_Ah = 1.5, | ||
| double internalResistance25 = 0.05, | ||
| double temperatureCoeff = 0.04, | ||
| double activationEnergy = 35000.0, | ||
| double selfDischargeRate25 = 0.0001, | ||
| double baseVoltage = 3.0, | ||
| double chargedVoltage = 4.2) | ||
| { | ||
| nominalCapacity = capacity_Ah; | ||
| Charge_Ah = initialCharge_Ah; | ||
| R25 = internalResistance25; | ||
| beta = temperatureCoeff; | ||
| Ea = activationEnergy; | ||
| selfDischarge25 = selfDischargeRate25; | ||
| this.baseVoltage = baseVoltage; | ||
| this.chargedVoltage = chargedVoltage; | ||
|
|
||
| double capacity = nominalCapacity * 1; | ||
| SOC = Charge_Ah / capacity; | ||
| SOC = Math.Min(Math.Max(SOC, 0.0), 1.0); | ||
| Voc = baseVoltage + (chargedVoltage - baseVoltage) * SOC + 0.05 * Math.Sin(5 * SOC); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Update the battery state given an applied terminal voltage, temperature, and timestep. | ||
| /// </summary> | ||
| /// <param name="appliedVoltage">Applied terminal voltage (V)</param> | ||
| /// <param name="dt_min">Timestep (minutes)</param> | ||
| /// <param name="temperature_C">Cell temperature (°C)</param> | ||
| /// <param name="current_limit">Current limited by the generator. </param> | ||
| public void Update(double appliedVoltage, double dt_min, double temperature_C, double current_limit) | ||
| { | ||
| double T_K = temperature_C + 273.15; | ||
|
|
||
| // external resistance | ||
| double R_external = 0.02; | ||
|
|
||
| // --- Temperature-dependent parameters --- | ||
| double R_internal = R25 * Math.Exp(beta * (25 - temperature_C)); | ||
| double capacity = nominalCapacity * (1 - 0.002 * Math.Abs(temperature_C - 25)); | ||
| double k_self = selfDischarge25 * Math.Exp(0.05 * (temperature_C - 25)); | ||
|
|
||
| // --- Compute open-circuit voltage based on SOC --- | ||
| SOC = Charge_Ah / capacity; | ||
| SOC = Math.Min(Math.Max(SOC, 0.0), 1.0); | ||
| Voc = baseVoltage + (chargedVoltage - baseVoltage) * SOC + 0.05 * Math.Sin(5 * SOC); | ||
|
|
||
| // --- Solve current from voltage equation --- | ||
| // I is clamped by +/- current_limit. | ||
| // appliedVoltage = Voc - I * R_internal | ||
| Current_A = Math.Max(Math.Min((Voc- appliedVoltage) / (R_internal + R_external), current_limit), -current_limit); | ||
|
|
||
| // --- Temperature-dependent efficiency (Arrhenius relation) --- | ||
| CoulombicEfficiency = Math.Exp(-Ea / Rgas * (1.0 / T_K - 1.0 / Tref)); | ||
| CoulombicEfficiency = Math.Min(Math.Max(CoulombicEfficiency, 0.7), 1.0); | ||
|
|
||
| // --- Effective current (charging losses) --- | ||
| double effectiveCurrent = Current_A; | ||
| if (Current_A > 0) // Charging | ||
| effectiveCurrent *= CoulombicEfficiency; | ||
|
|
||
| // --- Update charge (Ah) --- | ||
| Charge_Ah += -effectiveCurrent * dt_min / 60.0; | ||
|
|
||
| // --- Apply self-discharge --- | ||
| Charge_Ah -= Charge_Ah * k_self * dt_min; | ||
|
|
||
| // --- Clamp charge --- | ||
| Charge_Ah = Math.Min(Math.Max(Charge_Ah, 0.0), capacity); | ||
|
|
||
| // --- Update terminal voltage --- | ||
| Voltage_V = appliedVoltage; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.