View Javadoc
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.Units;
9   import org.djunits.unit.scale.IdentityScale;
10  import org.djunits.unit.scale.LinearScale;
11  import org.djunits.unit.scale.Scale;
12  import org.djunits.unit.si.SIPrefix;
13  import org.djunits.unit.si.SIPrefixes;
14  import org.djunits.unit.si.SIUnit;
15  import org.djunits.unit.system.UnitSystem;
16  
17  /**
18   * Pressure is the force exerted per unit area, measured in pascals (Pa).
19   * <p>
20   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
21   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
22   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
23   * @author Alexander Verbraeck
24   */
25  public class Pressure extends Quantity<Pressure>
26  {
27      /** Constant with value zero. */
28      public static final Pressure ZERO = ofSi(0.0);
29  
30      /** Constant with value one. */
31      public static final Pressure ONE = ofSi(1.0);
32  
33      /** Constant with value NaN. */
34      @SuppressWarnings("checkstyle:constantname")
35      public static final Pressure NaN = ofSi(Double.NaN);
36  
37      /** Constant with value POSITIVE_INFINITY. */
38      public static final Pressure POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
39  
40      /** Constant with value NEGATIVE_INFINITY. */
41      public static final Pressure NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
42  
43      /** Constant with value MAX_VALUE. */
44      public static final Pressure POS_MAXVALUE = ofSi(Double.MAX_VALUE);
45  
46      /** Constant with value -MAX_VALUE. */
47      public static final Pressure NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
48  
49      /** */
50      private static final long serialVersionUID = 600L;
51  
52      /**
53       * Instantiate a Pressure quantity with an SI or base value and a display unit.
54       * @param value the quantity value expressed in the SI or base unit
55       * @param displayUnit the display unit to use
56       * @param useSi use SI value when true, use value in unit when false
57       */
58      public Pressure(final double value, final Pressure.Unit displayUnit, final boolean useSi)
59      {
60          super(value, displayUnit, useSi);
61      }
62  
63      /**
64       * Instantiate a Pressure quantity expressed in the given unit.
65       * @param valueInUnit the quantity value expressed in the given unit
66       * @param unit the unit of the value, also acts as the display unit
67       */
68      public Pressure(final double valueInUnit, final Pressure.Unit unit)
69      {
70          this(valueInUnit, unit, false);
71      }
72  
73      /**
74       * Return a Pressure instance based on an SI value.
75       * @param si the si value
76       * @return the Pressure instance based on an SI value
77       */
78      public static Pressure ofSi(final double si)
79      {
80          return new Pressure(si, Pressure.Unit.SI, true);
81      }
82  
83      /**
84       * Instantiate a Pressure quantity with an SI or base value and a display unit.
85       * @param siValue the quantity value expressed in the SI or base unit
86       * @param displayUnit the display unit to use
87       * @return the Pressure instance based on an SI value with the given display unit
88       */
89      public static Pressure ofSi(final double siValue, final Pressure.Unit displayUnit)
90      {
91          return new Pressure(siValue, displayUnit, true);
92      }
93  
94      @Override
95      public Pressure instantiateSi(final double siValue, final UnitInterface<Pressure> displayUnit)
96      {
97          return new Pressure(siValue, (Pressure.Unit) displayUnit, true);
98      }
99  
100     /**
101      * Returns a Pressure representation of a textual representation of a value with a unit. The String representation that can
102      * be parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are
103      * allowed, but not required, between the value and the unit.
104      * @param text the textual representation to parse into a Pressure
105      * @return the Scalar representation of the value in its unit
106      * @throws IllegalArgumentException when the text cannot be parsed
107      * @throws NullPointerException when the text argument is null
108      */
109     public static Pressure valueOf(final String text)
110     {
111         return Quantity.valueOf(text, ZERO);
112     }
113 
114     /**
115      * Returns a Pressure based on a value expressed in the unit.
116      * @param valueInUnit the value, expressed in the given unit
117      * @param unit the unit of the value, also acts as the display unit
118      * @return ab Pressure representation of the value in its unit
119      */
120     public static Pressure of(final double valueInUnit, final Pressure.Unit unit)
121     {
122         return new Pressure(valueInUnit, unit);
123     }
124 
125     /**
126      * Returns a Pressure based on a value and the textual representation of the unit, which can be localized.
127      * @param valueInUnit the value, expressed in the unit as given by unitString
128      * @param unitString the textual representation of the unit
129      * @return the Scalar representation of the value in its unit
130      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
131      * @throws NullPointerException when the unitString argument is null
132      */
133     public static Pressure of(final double valueInUnit, final String unitString)
134     {
135         return Quantity.of(valueInUnit, unitString, ZERO);
136     }
137 
138     @Override
139     public Pressure.Unit getDisplayUnit()
140     {
141         return (Pressure.Unit) super.getDisplayUnit();
142     }
143 
144     /**
145      * Calculate the division of Pressure and Pressure, which results in a Dimensionless quantity.
146      * @param v quantity
147      * @return quantity as a division of Pressure and Pressure
148      */
149     public Dimensionless divide(final Pressure v)
150     {
151         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
152     }
153 
154     /**
155      * Calculate the multiplication of Pressure and Area, which results in a Force scalar.
156      * @param v scalar
157      * @return scalar as a multiplication of Pressure and Area
158      */
159     public Force multiply(final Area v)
160     {
161         return new Force(this.si() * v.si(), Force.Unit.SI);
162     }
163 
164     /**
165      * Calculate the multiplication of Pressure and Volume, which results in a Energy scalar.
166      * @param v scalar
167      * @return scalar as a multiplication of Pressure and Volume
168      */
169     public Energy multiply(final Volume v)
170     {
171         return new Energy(this.si() * v.si(), Energy.Unit.SI);
172     }
173 
174     /******************************************************************************************************/
175     /********************************************** UNIT CLASS ********************************************/
176     /******************************************************************************************************/
177 
178     /**
179      * Pressure.Unit encodes the units of force exerted per unit area.
180      * <p>
181      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
182      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
183      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
184      * @author Alexander Verbraeck
185      */
186     @SuppressWarnings("checkstyle:constantname")
187     public static class Unit extends AbstractUnit<Pressure>
188     {
189         /** The dimensions of pressure: kg/m.s2. */
190         public static final SIUnit SI_UNIT = SIUnit.of("kg/ms2");
191 
192         /** Pascal. */
193         public static final Pressure.Unit Pa =
194                 new Pressure.Unit("Pa", "Pa", "pascal", IdentityScale.SCALE, UnitSystem.SI_DERIVED, SIPrefixes.getSiPrefix(""));
195 
196         /** The SI or BASE unit. */
197         public static final Pressure.Unit SI = (Unit) Pa.generateSiPrefixes(false, false);
198 
199         /** hectopascal. */
200         public static final Pressure.Unit hPa = Units.resolve(Pressure.Unit.class, "hPa");
201 
202         /** kilopascal. */
203         public static final Pressure.Unit kPa = Units.resolve(Pressure.Unit.class, "kPa");
204 
205         /** standard atmosphere. */
206         public static final Pressure.Unit atm = Pa.deriveUnit("atm", "atmosphere (standard)", 101325.0, UnitSystem.OTHER);
207 
208         /** torr. */
209         public static final Pressure.Unit torr = atm.deriveUnit("torr", "Torr", 1.0 / 760.0, UnitSystem.OTHER);
210 
211         /** technical atmosphere = kgf/cm2. */
212         public static final Pressure.Unit at =
213                 Pa.deriveUnit("at", "atmosphere (technical)", Acceleration.Unit.CONST_GRAVITY / 1.0E-4, UnitSystem.OTHER);
214 
215         /** barye = dyne/cm2. */
216         public static final Pressure.Unit Ba = Pa.deriveUnit("Ba", "barye", 1.0E-5 / 1.0E-4, UnitSystem.CGS);
217 
218         /** bar. */
219         public static final Pressure.Unit bar = Pa.deriveUnit("bar", "bar", 1.0E5, UnitSystem.OTHER);
220 
221         /** millibar. */
222         public static final Pressure.Unit mbar = bar.deriveUnit("mbar", "millibar", 1.0E-3, UnitSystem.OTHER);
223 
224         /** cm Hg. */
225         public static final Pressure.Unit cmHg = Pa.deriveUnit("cmHg", "centimeter mercury", 1333.224, UnitSystem.OTHER);
226 
227         /** mm Hg. */
228         public static final Pressure.Unit mmHg = Pa.deriveUnit("mmHg", "millimeter mercury", 133.3224, UnitSystem.OTHER);
229 
230         /** foot Hg. */
231         public static final Pressure.Unit ftHg = Pa.deriveUnit("ftHg", "foot mercury", 40.63666E3, UnitSystem.IMPERIAL);
232 
233         /** inch Hg. */
234         public static final Pressure.Unit inHg = Pa.deriveUnit("inHg", "inch mercury", 3.386389E3, UnitSystem.IMPERIAL);
235 
236         /** kilogram-force per square millimeter. */
237         public static final Pressure.Unit kgf_mm2 = Pa.deriveUnit("kgf/mm2", "kilogram-force per square millimeter",
238                 Acceleration.Unit.CONST_GRAVITY / 1.0E-6, UnitSystem.OTHER);
239 
240         /** pound-force per square foot. */
241         public static final Pressure.Unit lbf_ft2 = Pa.deriveUnit("lbf/ft2", "pound-force per square foot",
242                 Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY / (Length.Unit.CONST_FT * Length.Unit.CONST_FT),
243                 UnitSystem.IMPERIAL);
244 
245         /** pound-force per square inch. */
246         public static final Pressure.Unit lbf_in2 = Pa.deriveUnit("lbf/in2", "pound-force per square inch",
247                 Mass.Unit.CONST_LB * Acceleration.Unit.CONST_GRAVITY / (Length.Unit.CONST_IN * Length.Unit.CONST_IN),
248                 UnitSystem.IMPERIAL);
249 
250         /** psi = pound-force per square inch. */
251         public static final Pressure.Unit psi = lbf_in2;
252 
253         /** pieze. */
254         public static final Pressure.Unit pz = Pa.deriveUnit("pz", "pi\u00E8ze", 1000.0, UnitSystem.MTS);
255 
256         /**
257          * Create a new Pressure unit.
258          * @param id the id or main abbreviation of the unit
259          * @param name the full name of the unit
260          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
261          * @param unitSystem the unit system such as SI or IMPERIAL
262          */
263         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
264         {
265             super(id, name, scaleFactorToBaseUnit, unitSystem);
266         }
267 
268         /**
269          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
270          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
271          * @param displayAbbreviation the display abbreviation of the unit
272          * @param name the full name of the unit
273          * @param scale the scale to use to convert from this unit to the standard (e.g., SI, BASE) unit
274          * @param unitSystem unit system, e.g. SI or Imperial
275          * @param siPrefix the SI Prefix of this unit
276          */
277         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
278                 final UnitSystem unitSystem, final SIPrefix siPrefix)
279         {
280             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem, siPrefix);
281         }
282 
283         @Override
284         public SIUnit siUnit()
285         {
286             return SI_UNIT;
287         }
288 
289         @Override
290         public Unit getBaseUnit()
291         {
292             return SI;
293         }
294 
295         @Override
296         public Pressure ofSi(final double si, final UnitInterface<Pressure> displayUnit)
297         {
298             return new Pressure(si, (Unit) displayUnit, true);
299         }
300 
301         @Override
302         public Pressure.Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
303                 final double scaleFactor, final UnitSystem unitSystem, final SIPrefix siPrefix)
304         {
305             if (getScale() instanceof LinearScale ls)
306             {
307                 return new Pressure.Unit(textualAbbreviation, displayAbbreviation, name,
308                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem, siPrefix);
309             }
310             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
311         }
312 
313         @Override
314         public Pressure.Unit deriveUnit(final String abbreviation, final String name, final double scaleFactor,
315                 final UnitSystem unitSystem)
316         {
317             return (Unit) super.deriveUnit(abbreviation, name, scaleFactor, unitSystem);
318         }
319 
320     }
321 
322 }