Skip to content

Commit 137a695

Browse files
Merge branch 'dev' into df/#1131-psdm-update-for-tap-water-demand
# Conflicts: # CHANGELOG.md
2 parents c1abc22 + 6f83769 commit 137a695

32 files changed

+269
-230
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Adding timeseries for voltage values [#1128](https://github.com/ie3-institute/PowerSystemDataModel/issues/1128)
1313
- Added Staudt to list of reviewers [#1190](https://github.com/ie3-institute/PowerSystemDataModel/issues/1190)
1414
- Extend ValidationUtils for validating ThermalGrids [#1216](https://github.com/ie3-institute/PowerSystemDataModel/issues/1216)
15+
- Enhance `TimeSeriesSource` with method to retrieve the previous value before a given key [#1182](https://github.com/ie3-institute/PowerSystemDataModel/issues/1182)
1516
- Extend ValidationUtils for validating ThermalGrids [#1216](https://github.com/ie3-institute/PowerSystemDataModel/issues/1216)
1617
- Attribute `pThermalRated` for `ThermalStorage`s [#679](https://github.com/ie3-institute/PowerSystemDataModel/issues/679)
1718
- Attributes `housingType` and `numberInhabitants` for `ThermalHouse`s [#1131](https://github.com/ie3-institute/PowerSystemDataModel/issues/1131)
@@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3031
- Renamed timeseries mapping `participant` column to `asset` [#1191](https://github.com/ie3-institute/PowerSystemDataModel/issues/1191)
3132
- Removed attribute `dsm` from `LoadInput` [#1195](https://github.com/ie3-institute/PowerSystemDataModel/issues/1195)
3233
- Fix spotless deprecations [#1123](https://github.com/ie3-institute/PowerSystemDataModel/issues/1223)
34+
- Refactored `CongestionResult`, removed `ModelResultEntity` [#1234](https://github.com/ie3-institute/PowerSystemDataModel/issues/1234)
3335

3436
## [5.1.0] - 2024-06-24
3537

docs/readthedocs/models/result/grid/congestion.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Congestion
44

5-
Representation of a congestion result for a given subnet.
5+
Representation of a congestion result for a given asset.
66

77
## Attributes, Units and Remarks
88

@@ -18,30 +18,30 @@ Representation of a congestion result for a given subnet.
1818
* - time
1919
- ZonedDateTime
2020
- date and time for the produced result
21-
22-
* - subgrid
21+
22+
* - inputModel
2323
-
24-
- Sub grid number
25-
26-
* - vMin
27-
- p.u.
28-
- minimal voltage of the subnet
29-
30-
* - vMax
31-
- p.u.
32-
- maximal voltage of the subnet
33-
34-
* - voltage
24+
- uuid for the associated input model
25+
26+
* - inputModelType
3527
-
36-
- Boolean indicator, if a voltage congestion occurred
37-
38-
* - line
28+
- the type of the input model (e.g. node, line, etc.)
29+
30+
* - subgrid
3931
-
40-
- Boolean indicator, if a line congestion occurred
32+
- Sub grid number
4133
42-
* - transformer
43-
-
44-
- Boolean indicator, if a transformer congestion occurred
34+
* - value
35+
- Percent
36+
- the actual value that was calculated in relation to its base value
37+
38+
* - min
39+
- Percent
40+
- minimal limit value
41+
42+
* - max
43+
- Percent
44+
- maximal limit value
4545
```
4646

4747
## Caveats

src/main/java/edu/ie3/datamodel/io/factory/result/CongestionResultFactory.java

+23-13
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
*/
66
package edu.ie3.datamodel.io.factory.result;
77

8-
import static edu.ie3.util.quantities.PowerSystemUnits.PU;
8+
import static tech.units.indriya.unit.Units.PERCENT;
99

10+
import edu.ie3.datamodel.exceptions.FactoryException;
11+
import edu.ie3.datamodel.exceptions.ParsingException;
1012
import edu.ie3.datamodel.io.factory.EntityData;
1113
import edu.ie3.datamodel.models.result.CongestionResult;
14+
import edu.ie3.datamodel.models.result.CongestionResult.InputModelType;
15+
import edu.ie3.datamodel.utils.Try;
1216
import java.time.ZonedDateTime;
1317
import java.time.format.DateTimeFormatter;
1418
import java.util.List;
1519
import java.util.Set;
20+
import java.util.UUID;
1621
import javax.measure.quantity.Dimensionless;
1722
import tech.units.indriya.ComparableQuantity;
1823

1924
public class CongestionResultFactory extends ResultEntityFactory<CongestionResult> {
25+
private static final String TYPE = "type";
2026
private static final String SUBGRID = "subgrid";
21-
private static final String VMIN = "vMin";
22-
private static final String VMAX = "vMax";
23-
private static final String VOLTAGE = "voltage";
24-
private static final String LINE = "line";
25-
private static final String TRANSFORMER = "transformer";
27+
private static final String VALUE = "value";
28+
private static final String MIN = "min";
29+
private static final String MAX = "max";
2630

2731
public CongestionResultFactory() {
2832
super(CongestionResult.class);
@@ -34,19 +38,25 @@ public CongestionResultFactory(DateTimeFormatter dateTimeFormatter) {
3438

3539
@Override
3640
protected List<Set<String>> getFields(Class<?> entityClass) {
37-
return List.of(newSet(TIME, SUBGRID, VMIN, VMAX, VOLTAGE, LINE, TRANSFORMER));
41+
return List.of(newSet(TIME, INPUT_MODEL, TYPE, SUBGRID, MIN, MAX));
3842
}
3943

4044
@Override
4145
protected CongestionResult buildModel(EntityData data) {
4246
ZonedDateTime zdtTime = timeUtil.toZonedDateTime(data.getField(TIME));
47+
UUID inputModel = data.getUUID(INPUT_MODEL);
48+
49+
InputModelType type =
50+
Try.of(() -> InputModelType.parse(data.getField(TYPE)), ParsingException.class)
51+
.transformF(FactoryException::new)
52+
.getOrThrow();
53+
4354
int subgrid = data.getInt(SUBGRID);
44-
ComparableQuantity<Dimensionless> vMin = data.getQuantity(VMIN, PU);
45-
ComparableQuantity<Dimensionless> vMax = data.getQuantity(VMAX, PU);
46-
boolean voltage = data.getBoolean(VOLTAGE);
47-
boolean line = data.getBoolean(LINE);
48-
boolean transformer = data.getBoolean(TRANSFORMER);
4955

50-
return new CongestionResult(zdtTime, subgrid, vMin, vMax, voltage, line, transformer);
56+
ComparableQuantity<Dimensionless> value = data.getQuantity(VALUE, PERCENT);
57+
ComparableQuantity<Dimensionless> min = data.getQuantity(MIN, PERCENT);
58+
ComparableQuantity<Dimensionless> max = data.getQuantity(MAX, PERCENT);
59+
60+
return new CongestionResult(zdtTime, inputModel, type, subgrid, value, min, max);
5161
}
5262
}

src/main/java/edu/ie3/datamodel/io/factory/result/ConnectorResultFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import javax.measure.quantity.ElectricCurrent;
2121
import tech.units.indriya.ComparableQuantity;
2222

23-
public class ConnectorResultFactory extends ModelResultFactory<ConnectorResult> {
23+
public class ConnectorResultFactory extends ResultEntityFactory<ConnectorResult> {
2424

2525
private static final String IAMAG = "iAMag";
2626
private static final String IAANG = "iAAng";

src/main/java/edu/ie3/datamodel/io/factory/result/FlexOptionsResultFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import javax.measure.quantity.Power;
1515
import tech.units.indriya.ComparableQuantity;
1616

17-
public class FlexOptionsResultFactory extends ModelResultFactory<FlexOptionsResult> {
17+
public class FlexOptionsResultFactory extends ResultEntityFactory<FlexOptionsResult> {
1818

1919
private static final String P_REF = "pRef";
2020
private static final String P_MIN = "pMin";

src/main/java/edu/ie3/datamodel/io/factory/result/ModelResultFactory.java

-23
This file was deleted.

src/main/java/edu/ie3/datamodel/io/factory/result/NodeResultFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import javax.measure.quantity.Dimensionless;
1616
import tech.units.indriya.ComparableQuantity;
1717

18-
public class NodeResultFactory extends ModelResultFactory<NodeResult> {
18+
public class NodeResultFactory extends ResultEntityFactory<NodeResult> {
1919
private static final String VMAG = "vMag";
2020
private static final String VANG = "vAng";
2121

src/main/java/edu/ie3/datamodel/io/factory/result/ResultEntityFactory.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public abstract class ResultEntityFactory<T extends ResultEntity>
2222
extends EntityFactory<T, EntityData> {
2323

2424
protected static final String TIME = "time";
25+
protected static final String INPUT_MODEL = "inputModel";
2526

2627
protected final TimeUtil timeUtil;
2728

src/main/java/edu/ie3/datamodel/io/factory/result/SwitchResultFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.time.format.DateTimeFormatter;
1212
import java.util.*;
1313

14-
public class SwitchResultFactory extends ModelResultFactory<SwitchResult> {
14+
public class SwitchResultFactory extends ResultEntityFactory<SwitchResult> {
1515

1616
private static final String CLOSED = "closed";
1717

src/main/java/edu/ie3/datamodel/io/factory/result/SystemParticipantResultFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Factory class for creating {@link SystemParticipantResult} entities from provided {@link
2424
* EntityData} data objects.
2525
*/
26-
public class SystemParticipantResultFactory extends ModelResultFactory<SystemParticipantResult> {
26+
public class SystemParticipantResultFactory extends ResultEntityFactory<SystemParticipantResult> {
2727

2828
private static final String POWER = "p";
2929
private static final String REACTIVE_POWER = "q";

src/main/java/edu/ie3/datamodel/io/factory/result/ThermalResultFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import javax.measure.quantity.Temperature;
2323
import tech.units.indriya.ComparableQuantity;
2424

25-
public class ThermalResultFactory extends ModelResultFactory<ThermalUnitResult> {
25+
public class ThermalResultFactory extends ResultEntityFactory<ThermalUnitResult> {
2626
private static final String Q_DOT = "qDot";
2727
private static final String INDOOR_TEMPERATURE = "indoorTemperature";
2828
private static final String ENERGY = "energy";

src/main/java/edu/ie3/datamodel/io/source/TimeSeriesSource.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ protected Try<TimeBasedValue<V>, FactoryException> createTimeBasedValue(
5050
public abstract IndividualTimeSeries<V> getTimeSeries(ClosedInterval<ZonedDateTime> timeInterval)
5151
throws SourceException;
5252

53-
public abstract Optional<V> getValue(ZonedDateTime time) throws SourceException;
53+
public abstract Optional<V> getValue(ZonedDateTime time);
54+
55+
public abstract Optional<TimeBasedValue<V>> getPreviousTimeBasedValue(ZonedDateTime time);
5456

5557
/**
5658
* Method to return all time keys after a given timestamp.

src/main/java/edu/ie3/datamodel/io/source/csv/CsvTimeSeriesSource.java

+9
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ public Optional<V> getValue(ZonedDateTime time) {
128128
return timeSeries.getValue(time);
129129
}
130130

131+
@Override
132+
public Optional<TimeBasedValue<V>> getPreviousTimeBasedValue(ZonedDateTime time) {
133+
return timeSeries.getPreviousTimeBasedValue(time);
134+
}
135+
136+
public Optional<TimeBasedValue<V>> getNextTimeBasedValue(ZonedDateTime time) {
137+
return timeSeries.getNextTimeBasedValue(time);
138+
}
139+
131140
@Override
132141
public List<ZonedDateTime> getTimeKeysAfter(ZonedDateTime time) {
133142
return timeSeries.getTimeKeysAfter(time);

src/main/java/edu/ie3/datamodel/io/source/sql/SqlTimeSeriesSource.java

+40-8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class SqlTimeSeriesSource<V extends Value> extends TimeSeriesSource<V> {
4848

4949
private final String queryTimeInterval;
5050
private final String queryTimeKeysAfter;
51+
private final String queryForValueBefore;
5152
private final String queryTime;
5253

5354
public SqlTimeSeriesSource(
@@ -63,15 +64,16 @@ public SqlTimeSeriesSource(
6364
final ColumnScheme columnScheme = ColumnScheme.parse(valueClass).orElseThrow();
6465
this.tableName = sqlDataSource.databaseNamingStrategy.getTimeSeriesEntityName(columnScheme);
6566

67+
String schemaName = sqlDataSource.schemaName;
68+
6669
String dbTimeColumnName =
6770
sqlDataSource.getDbColumnName(factory.getTimeFieldString(), tableName);
6871

69-
this.queryFull = createQueryFull(sqlDataSource.schemaName, tableName);
70-
this.queryTimeInterval =
71-
createQueryForTimeInterval(sqlDataSource.schemaName, tableName, dbTimeColumnName);
72-
this.queryTimeKeysAfter =
73-
createQueryForTimeKeysAfter(sqlDataSource.schemaName, tableName, dbTimeColumnName);
74-
this.queryTime = createQueryForTime(sqlDataSource.schemaName, tableName, dbTimeColumnName);
72+
this.queryFull = createQueryFull(schemaName, tableName);
73+
this.queryTimeInterval = createQueryForTimeInterval(schemaName, tableName, dbTimeColumnName);
74+
this.queryTimeKeysAfter = createQueryForTimeKeysAfter(schemaName, tableName, dbTimeColumnName);
75+
this.queryForValueBefore = createQueryForValueBefore(schemaName, tableName, dbTimeColumnName);
76+
this.queryTime = createQueryForTime(schemaName, tableName, dbTimeColumnName);
7577
}
7678

7779
/**
@@ -179,6 +181,14 @@ public Optional<V> getValue(ZonedDateTime time) {
179181
return Optional.of(timeBasedValues.stream().toList().get(0).getValue());
180182
}
181183

184+
@Override
185+
public Optional<TimeBasedValue<V>> getPreviousTimeBasedValue(ZonedDateTime time) {
186+
return getTimeBasedValueSet(
187+
queryForValueBefore, ps -> ps.setTimestamp(1, Timestamp.from(time.toInstant())))
188+
.stream()
189+
.max(TimeBasedValue::compareTo);
190+
}
191+
182192
@Override
183193
public List<ZonedDateTime> getTimeKeysAfter(ZonedDateTime time) {
184194
return dataSource
@@ -278,8 +288,30 @@ private String createQueryForTimeKeysAfter(
278288
}
279289

280290
/**
281-
* Creates a basic query to retrieve an entry for the given time series uuid and time with the
282-
* following pattern: <br>
291+
* Creates a base query to retrieve all time keys after a given time for given time series with
292+
* the following pattern: <br>
293+
* {@code <base query> WHERE time_series = $timeSeriesUuid AND <time column> < ?;}
294+
*
295+
* @param schemaName the name of the database schema
296+
* @param tableName the name of the database table
297+
* @param timeColumnName the name of the column holding the timestamp info
298+
* @return the query string
299+
*/
300+
private String createQueryForValueBefore(
301+
String schemaName, String tableName, String timeColumnName) {
302+
return createBaseQueryString(schemaName, tableName)
303+
+ WHERE
304+
+ TIME_SERIES
305+
+ " = '"
306+
+ timeSeriesUuid.toString()
307+
+ "' AND "
308+
+ timeColumnName
309+
+ " < ?"
310+
+ "ORDER BY time DESC LIMIT 1;";
311+
}
312+
/**
313+
* Creates a base query to retrieve all time keys before a given time for given time series with
314+
* the following pattern: <br>
283315
* {@code <base query> WHERE time_series = $timeSeriesUuid AND <time column>=?;}
284316
*
285317
* @param schemaName the name of the database schema

0 commit comments

Comments
 (0)