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