-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathproperty_meta.py
More file actions
602 lines (487 loc) · 20.2 KB
/
Copy pathproperty_meta.py
File metadata and controls
602 lines (487 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#################################################################################
# The Institute for the Design of Advanced Energy Systems Integrated Platform
# Framework (IDAES IP) was produced under the DOE Institute for the
# Design of Advanced Energy Systems (IDAES).
#
# Copyright (c) 2018-2026 by the software owners: The Regents of the
# University of California, through Lawrence Berkeley National Laboratory,
# National Technology & Engineering Solutions of Sandia, LLC, Carnegie Mellon
# University, West Virginia University Research Corporation, et al.
# All rights reserved. Please see the files COPYRIGHT.md and LICENSE.md
# for full copyright and license information.
#################################################################################
"""
These classes handle the metadata aspects of classes representing
property packages.
Implementors of property packages need to do the following:
1. Create a new class that inherits from
:class:`idaes.core.property_base.PhysicalParameterBlock`, which in turn
inherits from :class:`HasPropertyClassMetadata`, in this module.
2. In that class, implement the `define_metadata()` method, inherited from
:class:`HasPropertyClassMetadata`. This method is called
automatically, once, when the `get_metadata()` method is first invoked.
An empty metadata object (an instance of :class:`PropertyClassMetadata`)
will be passed in, which the method should populate with information about
properties and default units.
Example::
from idaes.core.property_base import PhysicalParameterBlock
class MyPropParams(PhysicalParameterBlock):
@classmethod
def define_metadata(cls, meta):
meta.add_default_units({foo.U.TIME: 'fortnights',
foo.U.MASS: 'stones'})
meta.add_properties({'under_sea': {'units': 'leagues'},
'tentacle_size': {'units': 'yards'}})
meta.add_required_properties({'under_sea': 'leagues',
'tentacle_size': 'yards'})
# Also, of course, implement the non-metadata methods that
# do the work of the class.
"""
# TODO: Missing docstrings
# pylint: disable=missing-function-docstring
from pyomo.environ import units
from pyomo.core.base.units_container import _PyomoUnit, InconsistentUnitsError
from pyomo.common.deprecation import deprecation_warning
from idaes.core.util.exceptions import PropertyPackageError
from idaes.core.base.property_set import StandardPropertySet, PropertySetBase
import idaes.logger as idaeslog
__author__ = "Dan Gunter <dkgunter@lbl.gov>, Andrew Lee"
_log = idaeslog.getLogger(__name__)
class HasPropertyClassMetadata(object):
"""Interface for classes that have PropertyClassMetadata."""
_metadata = None
@classmethod
def get_metadata(cls):
"""Get property parameter metadata.
If the metadata is not defined, this will instantiate a new
metadata object and call `define_metadata()` to set it up.
If the metadata is already defined, it will be simply returned.
Returns:
PropertyClassMetadata: The metadata
"""
if cls._metadata is None:
pcm = PropertyClassMetadata()
cls.define_metadata(pcm)
cls._metadata = pcm
# Check that the metadata was actually populated
# Check requires looking at private attributes
# pylint: disable-next=protected-access
if pcm._properties is None or pcm._default_units is None:
raise PropertyPackageError(
"Property package did not populate all expected metadata."
)
return cls._metadata
@classmethod
def define_metadata(cls, pcm):
"""Set all the metadata for properties and units.
This method should be implemented by subclasses.
In the implementation, they should set information into the
object provided as an argument.
Args:
pcm (PropertyClassMetadata): Add metadata to this object.
Returns:
None
"""
raise NotImplementedError()
class UnitSet(object):
"""
Object defining the set of recognised quantities in IDAES and their base units.
Units of measurement are defined by setting units for the seven base SI quantities
(amount, current, length, luminous intensity, mass, temperature and time), from which units
for all other quantities are derived. The units of the seven base quantities must be provided
when instantiating the UnitSet, otherwise base SI units are assumed.
Units can be accesses by via either a property on the UnitSet (e.g., UnitSet.TIME) or
via an index on the UnitSet (e.g., UnitSet["time"]).
"""
_base_quantities = {
"AMOUNT": units.mol,
"CURRENT": units.ampere,
"LENGTH": units.meter,
"LUMINOUS_INTENSITY": units.candela,
"MASS": units.kilogram,
"TEMPERATURE": units.kelvin,
"TIME": units.seconds,
}
def __init__(self):
self._time = units.seconds
self._length = units.meter
self._mass = units.kilogram
self._amount = units.mole
self._temperature = units.kelvin
self._current = units.ampere
self._luminous_intensity = units.candela
def set_units(
self,
amount: _PyomoUnit = units.mol,
current: _PyomoUnit = units.ampere,
length: _PyomoUnit = units.meter,
luminous_intensity: _PyomoUnit = units.candela,
mass: _PyomoUnit = units.kilogram,
temperature: _PyomoUnit = units.kelvin,
time: _PyomoUnit = units.seconds,
):
"""
Set desired units of measurement for the seven base quantities.
Args:
amount: units for amount (default = moles)
current: units for current (default = Amperes)
length: units for length (default = meters)
luminous_intensity: units for luminous intensity (default = candela)
mass: units for mass (default = kilograms)
temperature: units for temperature (default = Kelvins)
time: units for time (default = seconds)
Returns:
None
"""
self._time = time
self._length = length
self._mass = mass
self._amount = amount
self._temperature = temperature
self._current = current
self._luminous_intensity = luminous_intensity
# Check that valid units were assigned
for q, expected_dim in self._base_quantities.items():
u = getattr(self, q)
if not isinstance(u, _PyomoUnit):
# Check for non-unit inputs from user
raise PropertyPackageError(
f"Unrecognized units of measurement for quantity {q} ({u})"
)
# Check for expected dimensionality
try:
# Try to convert user-input to SI units of expected dimensions
units.convert(u, expected_dim)
except InconsistentUnitsError:
# An error indicates a mismatch in units or the units registry
raise PropertyPackageError(
f"Invalid units of measurement for quantity {q} ({u}). "
"Please ensure units provided are valid for this quantity and "
"use the Pyomo unit registry."
)
def __getitem__(self, key: str):
try:
# Check to catch cases where luminous intensity has a space
return getattr(self, key.upper().replace(" ", "_"))
except AttributeError:
raise PropertyPackageError(
f"Unrecognised quantity {key}. Please check that this is a recognised quantity "
"defined in idaes.core.base.property_meta.UnitSet."
)
def unitset_is_consistent(self, other: "UnitSet"):
"""
Checks that defined units of measurement for base quantities are consistent with those
in other UnitSet.
Args:
other: UnitSet to check for consistency with
Returns:
Bool indicating whether units are consistent
"""
return all(getattr(self, q) is getattr(other, q) for q in self._base_quantities)
@property
def TIME(self):
return self._time
@property
def LENGTH(self):
return self._length
@property
def MASS(self):
return self._mass
@property
def AMOUNT(self):
return self._amount
@property
def TEMPERATURE(self):
return self._temperature
@property
def CURRENT(self):
return self._current
@property
def LUMINOUS_INTENSITY(self):
return self._luminous_intensity
# Length based
@property
def AREA(self):
return self._length**2
@property
def VOLUME(self):
return self._length**3
@property
def VOLUME_MASS(self):
return self._length**3 * self._mass**-1
@property
def VOLUME_MOLE(self):
return self._length**3 * self._amount**-1
# Backward compatibility name
@property
def MOLAR_VOLUME(self):
msg = "The unit name MOLAR_VOLUME is being deprecated in favor of VOLUME_MOL."
deprecation_warning(msg=msg, logger=_log, version="2.3.0", remove_in="2.14.0")
return self.VOLUME_MOLE
# Flows
@property
def FLOW_MASS(self):
return self._mass * self._time**-1
@property
def FLOW_MOLE(self):
return self._amount * self._time**-1
@property
def FLOW_VOL(self):
return self._length**3 * self._time**-1
@property
def FLUX_MASS(self):
return self._mass * self._time**-1 * self._length**-2
@property
def FLUX_MOLE(self):
return self._amount * self._time**-1 * self._length**-2
@property
def FLUX_ENERGY(self):
return self._mass * self._time**-3
# Velocity, Acceleration and Force
@property
def VELOCITY(self):
return self._length * self._time**-1
@property
def ACCELERATION(self):
return self._length * self._time**-2
@property
def FORCE(self):
return self._length * self._mass * self._time**-2
# Pressures
@property
def PRESSURE(self):
return self._mass * self._length**-1 * self._time**-2
@property
def GAS_CONSTANT(self):
return (
self._mass
* self._length**2
* self._time**-2
* self._temperature**-1
* self._amount**-1
)
# Densities & Concentrations
@property
def DENSITY_MASS(self):
return self._mass * self._length**-3
@property
def DENSITY_MOLE(self):
return self._amount * self._length**-3
@property
def MOLALITY(self):
return self._amount * self._mass
@property
def MOLECULAR_WEIGHT(self):
return self._mass / self._amount
# Energy
@property
def ENERGY(self):
return self._mass * self._length**2 * self._time**-2
@property
def ENERGY_MASS(self):
return self._length**2 * self._time**-2
@property
def ENERGY_MOLE(self):
return self._mass * self._length**2 * self._time**-2 * self._amount**-1
@property
def POWER(self):
return self._mass * self._length**2 * self._time**-3
@property
def VOLTAGE(self):
return self._mass * self._length**2 * self._time**-3 * self._current**-1
# Heat Related
@property
def HEAT_CAPACITY_MASS(self):
return self._length**2 * self._time**-2 * self._temperature**-1
@property
def HEAT_CAPACITY_MOLE(self):
return (
self._mass
* self._length**2
* self._time**-2
* self._temperature**-1
* self._amount**-1
)
@property
def HEAT_TRANSFER_COEFFICIENT(self):
return self._mass * self._time**-3 * self._temperature**-1
# Entropy
@property
def ENTROPY(self):
return self._mass * self._length**2 * self._time**-2 * self._temperature**-1
@property
def ENTROPY_MASS(self):
return self._length**2 * self._time**-2 * self._temperature**-1
@property
def ENTROPY_MOLE(self):
return (
self._mass
* self._length**2
* self._time**-2
* self._temperature**-1
* self._amount**-1
)
# Transport Properties
@property
def DIFFUSIVITY(self):
return self._length**2 * self._time**-1
@property
def DYNAMIC_VISCOSITY(self):
return self._mass * self._length**-1 * self._time**-1
@property
def KINEMATIC_VISCOSITY(self):
return self._length**2 * self._time**-1
@property
def SURFACE_TENSION(self):
return self._mass * self._time**-2
@property
def THERMAL_CONDUCTIVITY(self):
return self._mass * self._length * self._time**-3 * self._temperature**-1
class PropertyClassMetadata(object):
"""
Container for metadata about the property class, which includes
default units and properties.
Example usage::
foo = PropertyClassMetadata()
foo.add_default_units(time = pyo.units.fortnights,
mass = pyo.units.stones)
foo.add_properties({'under_sea': {'method': 'submarine', 'units': 'leagues', 'required': False, 'supported': True},
'tentacle_size': {'method': 'kraken', 'units': 'yards', 'required': True, 'supported': True}})
"""
def __init__(self):
# TODO: Deprecate in favour of common units property
self._default_units = UnitSet()
# Assume a default PropertySet to begin with. Property packages can replace this
# with more specialized forms if required
self._properties = StandardPropertySet(parent=self)
def define_property_set(self, propset: PropertySetBase):
"""
Define the type of property set to use for this package.
Args:
propset: PropertySet class (must derive from PropertySetBase)
Returns:
None
"""
if not issubclass(propset, PropertySetBase):
raise PropertyPackageError(
f"{propset} does not derive from IDAES PropertySetBase class."
)
self._properties = propset(parent=self)
@property
def default_units(self):
# TODO: Deprecate in favour of common units property
return self._default_units
@property
def derived_units(self):
# TODO: Deprecate in favour of common units property
return self._default_units
@property
def properties(self):
return self._properties
def add_default_units(self, u: dict):
"""
Set units of measurement for base quantities used in this property package. Units
should be provided as a dict with keys being the seven base quantities and values
being Pyomo unit expressions. These will be used to update the UnitSet associated
with this property package.
Args:
u (dict): Key=property, Value=units
Returns:
None
Raises:
TypeError if definitions for unexpected quantities are found
"""
# TODO: Could look at replacing dict with defined arguments
# This would be a big API change
try:
self._default_units.set_units(**u)
except TypeError:
raise TypeError(
"Unexpected argument for base quantities found when creating UnitSet. "
"Please ensure that units are only defined for the seven base quantities."
)
def add_properties(self, p: dict):
"""Add properties to the metadata.
For each property, the value should be another dict which may contain
the following keys:
- 'units': (optional) units of measurement for the property.
- 'indices': (optional) list of sub-property indices for this property. If None, use default set, if False unindexed.
- 'method': (optional, only if 'indices' is None or False) the name of a method to construct the
property as a str, or None if the property will be
constructed by default.
- 'supported': (optional, only if 'indices' is None or False) bool indicating if this property is
supported by this package.
- 'required': (optional, only if 'indices' is None or False) bool indicating if this property is
required by this package.
- 'valid_range': (optional, only if 'indices' is None or False) 2-tuple containing range of validity for
property values (lower, upper).
- 'initialize': (optional) dict indicating 'method', 'required', 'supported' and 'valid_range' values for sub-properties by index.
Args:
p (dict): Key=property, Value=dict
Returns:
None
"""
# TODO: Deprecate in favour of directly updating or adding metadata
for k, v in p.items():
units = v.pop("units", None)
try:
try:
n, i = self._properties.get_name_and_index(k)
except ValueError:
msg = (
f"The property name {k} in property metadata is not a recognized "
"standard property name defined in this PropertySet. Please refer "
"to IDAES standard names in the IDAES documentation. You can use "
"the define_custom_properties() rather than the add_properties() "
"method to define metadata for this property. You can also use a "
"different property set by calling the define_property_set() method."
)
deprecation_warning(
msg=msg, logger=_log, version="2.0.0", remove_in="2.14.0"
)
n = k
i = None
getattr(self._properties, n)[i].update_property(**v)
except AttributeError:
# TODO: Deprecate this and make it raise an exception if an unknown property is encountered
# Force users to explicitly declare new/custom properties
self._properties.define_property(name=k, **v, units=units)
def define_custom_properties(self, p: dict):
"""Add custom properties to the metadata.
For each property, the value should be another dict which may contain
the following keys:
- 'units': (optional) units of measurement for the property.
- 'indices': (optional) list of sub-property indices for this property. If None, use default set, if False unindexed.
- 'method': (optional, only if 'indices' is None or False) the name of a method to construct the
property as a str, or None if the property will be
constructed by default.
- 'supported': (optional, only if 'indices' is None or False) bool indicating if this property is
supported by this package.
- 'required': (optional, only if 'indices' is None or False bool indicating if this property is
required by this package.
- 'initialize': (optional) dict indicating 'method', 'required' and 'supported' values for sub-properties by index.
Args:
p (dict): Key=property, Value=dict
Returns:
None
"""
for k, v in p.items():
self._properties.define_property(name=k, **v)
def add_required_properties(self, p: str):
# TODO: Deprecate
"""Add required properties to the metadata.
Update 'required' attribute of specified properties.
Note that argument must be a dict for backwards compatibility.
Args:
p (dict): Key=property, Value=(ignored)
Returns:
None
"""
for k in p.keys():
try:
self._properties[k].set_required(True)
except KeyError:
self._properties.define_property(name=k, supported=False, required=True)
def get_derived_units(self, units: str):
# TODO: Deprecate in favour of common units property
return self.derived_units[units]