1 package org.djunits.quantity;
2
3 import org.djunits.quantity.def.Quantity;
4 import org.djunits.unit.AbstractUnit;
5 import org.djunits.unit.UnitInterface;
6 import org.djunits.unit.UnitRuntimeException;
7 import org.djunits.unit.Unitless;
8 import org.djunits.unit.scale.LinearScale;
9 import org.djunits.unit.scale.Scale;
10 import org.djunits.unit.si.SIPrefix;
11 import org.djunits.unit.si.SIUnit;
12 import org.djunits.unit.system.UnitSystem;
13
14 /**
15 * Flow volume is the rate of volume passing through a surface per unit time, measured in cubic meters per second (m3/s).
16 * <p>
17 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18 * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
19 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
20 * @author Alexander Verbraeck
21 */
22 public class FlowVolume extends Quantity<FlowVolume>
23 {
24 /** Constant with value zero. */
25 public static final FlowVolume ZERO = ofSi(0.0);
26
27 /** Constant with value one. */
28 public static final FlowVolume ONE = ofSi(1.0);
29
30 /** Constant with value NaN. */
31 @SuppressWarnings("checkstyle:constantname")
32 public static final FlowVolume NaN = ofSi(Double.NaN);
33
34 /** Constant with value POSITIVE_INFINITY. */
35 public static final FlowVolume POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
36
37 /** Constant with value NEGATIVE_INFINITY. */
38 public static final FlowVolume NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
39
40 /** Constant with value MAX_VALUE. */
41 public static final FlowVolume POS_MAXVALUE = ofSi(Double.MAX_VALUE);
42
43 /** Constant with value -MAX_VALUE. */
44 public static final FlowVolume NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
45
46 /** */
47 private static final long serialVersionUID = 600L;
48
49 /**
50 * Instantiate a FlowVolume quantity with an SI or base value and a display unit.
51 * @param value the quantity value expressed in the SI or base unit
52 * @param displayUnit the display unit to use
53 * @param useSi use SI value when true, use value in unit when false
54 */
55 public FlowVolume(final double value, final FlowVolume.Unit displayUnit, final boolean useSi)
56 {
57 super(value, displayUnit, useSi);
58 }
59
60 /**
61 * Instantiate a FlowVolume quantity expressed in the given unit.
62 * @param valueInUnit the quantity value expressed in the given unit
63 * @param unit the unit of the value, also acts as the display unit
64 */
65 public FlowVolume(final double valueInUnit, final FlowVolume.Unit unit)
66 {
67 this(valueInUnit, unit, false);
68 }
69
70 /**
71 * Return a FlowVolume instance based on an SI value.
72 * @param si the si value
73 * @return the FlowVolume instance based on an SI value
74 */
75 public static FlowVolume ofSi(final double si)
76 {
77 return new FlowVolume(si, FlowVolume.Unit.SI, true);
78 }
79
80 /**
81 * Instantiate a FlowVolume quantity with an SI or base value and a display unit.
82 * @param siValue the quantity value expressed in the SI or base unit
83 * @param displayUnit the display unit to use
84 * @return the FlowVolume instance based on an SI value with the given display unit
85 */
86 public static FlowVolume ofSi(final double siValue, final FlowVolume.Unit displayUnit)
87 {
88 return new FlowVolume(siValue, displayUnit, true);
89 }
90
91 @Override
92 public FlowVolume instantiateSi(final double siValue, final UnitInterface<FlowVolume> displayUnit)
93 {
94 return new FlowVolume(siValue, (FlowVolume.Unit) displayUnit, true);
95 }
96
97 /**
98 * Returns a FlowVolume representation of a textual representation of a value with a unit. The String representation that
99 * can be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
100 * allowed, but not required, between the value and the unit.
101 * @param text the textual representation to parse into a FlowVolume
102 * @return the Scalar representation of the value in its unit
103 * @throws IllegalArgumentException when the text cannot be parsed
104 * @throws NullPointerException when the text argument is null
105 */
106 public static FlowVolume valueOf(final String text)
107 {
108 return Quantity.valueOf(text, ZERO);
109 }
110
111 /**
112 * Returns a FlowVolume based on a value expressed in the unit.
113 * @param valueInUnit the value, expressed in the given unit
114 * @param unit the unit of the value, also acts as the display unit
115 * @return ab FlowVolume representation of the value in its unit
116 */
117 public static FlowVolume of(final double valueInUnit, final FlowVolume.Unit unit)
118 {
119 return new FlowVolume(valueInUnit, unit);
120 }
121
122 /**
123 * Returns a FlowVolume based on a value and the textual representation of the unit, which can be localized.
124 * @param valueInUnit the value, expressed in the unit as given by unitString
125 * @param unitString the textual representation of the unit
126 * @return the Scalar representation of the value in its unit
127 * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
128 * @throws NullPointerException when the unitString argument is null
129 */
130 public static FlowVolume of(final double valueInUnit, final String unitString)
131 {
132 return Quantity.of(valueInUnit, unitString, ZERO);
133 }
134
135 @Override
136 public FlowVolume.Unit getDisplayUnit()
137 {
138 return (FlowVolume.Unit) super.getDisplayUnit();
139 }
140
141 /**
142 * Calculate the division of FlowVolume and FlowVolume, which results in a Dimensionless quantity.
143 * @param v quantity
144 * @return quantity as a division of FlowVolume and FlowVolume
145 */
146 public Dimensionless divide(final FlowVolume v)
147 {
148 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
149 }
150
151 /**
152 * Calculate the multiplication of FlowVolume and Duration, which results in a Volume scalar.
153 * @param v scalar
154 * @return scalar as a multiplication of FlowVolume and Duration
155 */
156 public Volume multiply(final Duration v)
157 {
158 return new Volume(this.si() * v.si(), Volume.Unit.SI);
159 }
160
161 /**
162 * Calculate the division of FlowVolume and Frequency, which results in a Volume scalar.
163 * @param v scalar
164 * @return scalar as a division of FlowVolume and Frequency
165 */
166 public Volume divide(final Frequency v)
167 {
168 return new Volume(this.si() / v.si(), Volume.Unit.SI);
169 }
170
171 /**
172 * Calculate the division of FlowVolume and Volume, which results in a Frequency scalar.
173 * @param v scalar
174 * @return scalar as a division of FlowVolume and Volume
175 */
176 public Frequency divide(final Volume v)
177 {
178 return new Frequency(this.si() / v.si(), Frequency.Unit.SI);
179 }
180
181 /**
182 * Calculate the division of FlowVolume and Area, which results in a Speed scalar.
183 * @param v scalar
184 * @return scalar as a division of FlowVolume and Area
185 */
186 public Speed divide(final Area v)
187 {
188 return new Speed(this.si() / v.si(), Speed.Unit.SI);
189 }
190
191 /**
192 * Calculate the division of FlowVolume and Speed, which results in a Area scalar.
193 * @param v scalar
194 * @return scalar as a division of FlowVolume and Speed
195 */
196 public Area divide(final Speed v)
197 {
198 return new Area(this.si() / v.si(), Area.Unit.SI);
199 }
200
201 /**
202 * Calculate the multiplication of FlowVolume and Density, which results in a FlowMass scalar.
203 * @param v scalar
204 * @return scalar as a multiplication of FlowVolume and Density
205 */
206 public FlowMass multiply(final Density v)
207 {
208 return new FlowMass(this.si() * v.si(), FlowMass.Unit.SI);
209 }
210
211 /******************************************************************************************************/
212 /********************************************** UNIT CLASS ********************************************/
213 /******************************************************************************************************/
214
215 /**
216 * FlowVolume.Unit encodes the units of volume flow.
217 * <p>
218 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
219 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
220 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
221 * @author Alexander Verbraeck
222 */
223 @SuppressWarnings("checkstyle:constantname")
224 public static class Unit extends AbstractUnit<FlowVolume>
225 {
226 /** The dimensions of the flow volume is m3/s. */
227 public static final SIUnit SI_UNIT = SIUnit.of("m3/s");
228
229 /** m3/s. */
230 public static final FlowVolume.Unit m3_s =
231 new FlowVolume.Unit("m3/s", "cubic meter per second", 1.0, UnitSystem.SI_DERIVED);
232
233 /** The SI or BASE unit. */
234 public static final FlowVolume.Unit SI = m3_s;
235
236 /** m^3/min. */
237 public static final FlowVolume.Unit m3_min =
238 m3_s.deriveUnit("m3/min", "cubic meter per minute", 1.0 / 60.0, UnitSystem.SI_ACCEPTED);
239
240 /** m^3/hour. */
241 public static final FlowVolume.Unit m3_h =
242 m3_s.deriveUnit("m3/h", "cubic meter per hour", 1.0 / 3600.0, UnitSystem.SI_ACCEPTED);
243
244 /** m^3/day. */
245 public static final FlowVolume.Unit m3_day =
246 m3_h.deriveUnit("m3/day", "cubic meter per day", 1.0 / 24.0, UnitSystem.SI_ACCEPTED);
247
248 /** L/s. */
249 public static final FlowVolume.Unit L_s = m3_s.deriveUnit("L/s", "liter per second", 1E-3, UnitSystem.SI_ACCEPTED);
250
251 /** L/min. */
252 public static final FlowVolume.Unit L_min =
253 L_s.deriveUnit("L/min", "liter per minute", 1.0 / 60.0, UnitSystem.SI_ACCEPTED);
254
255 /** L/hour. */
256 public static final FlowVolume.Unit L_h = L_s.deriveUnit("L/h", "liter per hour", 1.0 / 3600.0, UnitSystem.SI_ACCEPTED);
257
258 /** L/day. */
259 public static final FlowVolume.Unit L_day =
260 L_h.deriveUnit("L/day", "liter per day", 1.0 / 24.0, UnitSystem.SI_ACCEPTED);
261
262 /** ft^3/s. */
263 public static final FlowVolume.Unit ft3_s =
264 m3_s.deriveUnit("ft3/s", "cubic foot per second", Volume.Unit.CONST_CUBIC_FOOT, UnitSystem.IMPERIAL);
265
266 /** ft^3/min. */
267 public static final FlowVolume.Unit ft3_min =
268 ft3_s.deriveUnit("ft3/min", "cubic foot per minute", 1.0 / 60.0, UnitSystem.IMPERIAL);
269
270 /** in^3/s. */
271 public static final FlowVolume.Unit in3_s =
272 m3_s.deriveUnit("in3/s", "cubic inch per second", Volume.Unit.CONST_CUBIC_INCH, UnitSystem.IMPERIAL);
273
274 /** in^3/min. */
275 public static final FlowVolume.Unit in3_min =
276 in3_s.deriveUnit("in3/min", "cubic inch per minute", 1.0 / 60.0, UnitSystem.IMPERIAL);
277
278 /** gallon/s (US). */
279 public static final FlowVolume.Unit gal_US_s =
280 m3_s.deriveUnit("gal(US)/s", "US gallon per second", Volume.Unit.CONST_GALLON_US, UnitSystem.US_CUSTOMARY);
281
282 /** gallon/min (US). */
283 public static final FlowVolume.Unit gal_US_min =
284 gal_US_s.deriveUnit("gal(US)/min", "US gallon per minute", 1.0 / 60.0, UnitSystem.US_CUSTOMARY);
285
286 /** gallon/hour (US). */
287 public static final FlowVolume.Unit gal_US_h =
288 gal_US_s.deriveUnit("gal(US)/h", "US gallon per hour", 1.0 / 3600.0, UnitSystem.US_CUSTOMARY);
289
290 /** gallon/day (US). */
291 public static final FlowVolume.Unit gal_US_day =
292 gal_US_h.deriveUnit("gal(US)/day", "US gallon per day", 1.0 / 24.0, UnitSystem.US_CUSTOMARY);
293
294 /**
295 * Create a new FlowVolume unit.
296 * @param id the id or main abbreviation of the unit
297 * @param name the full name of the unit
298 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
299 * @param unitSystem the unit system such as SI or IMPERIAL
300 */
301 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
302 {
303 super(id, name, scaleFactorToBaseUnit, unitSystem);
304 }
305
306 /**
307 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
308 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
309 * @param displayAbbreviation the display abbreviation of the unit
310 * @param name the full name of the unit
311 * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
312 * @param unitSystem unit system, e.g. SI or Imperial
313 * @param siPrefix the SI Prefix of this unit
314 */
315 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
316 final UnitSystem unitSystem, final SIPrefix siPrefix)
317 {
318 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
319 }
320
321 @Override
322 public SIUnit siUnit()
323 {
324 return SI_UNIT;
325 }
326
327 @Override
328 public Unit getBaseUnit()
329 {
330 return SI;
331 }
332
333 @Override
334 public FlowVolume ofSi(final double si, final UnitInterface<FlowVolume> displayUnit)
335 {
336 return new FlowVolume(si, (Unit) displayUnit, true);
337 }
338
339 @Override
340 public FlowVolume.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
341 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
342 {
343 if (getScale() instanceof LinearScale ls)
344 {
345 return new FlowVolume.Unit(textualAbbreviation, displayAbbreviation, name,
346 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
347 }
348 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
349 }
350
351 @Override
352 public FlowVolume.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
353 final UnitSystem unitSystem)
354 {
355 return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
356 }
357
358 }
359
360 }