pyflange.io
¶
Reading and writing flange fatigue cases in the PyFlange JSON format.
This module implements the PyFlange JSON case format, documented in detail in the JSON format reference. In particular it provides:
- Loaders, that convert a JSON case file (or the equivalent dictionary) into
PyFlange objects:
load_fatiguecase,load_fatiguecase_samplerand the lower-levelcreate_*functions. - Exporters, that convert PyFlange objects back into dictionaries in the same
format (
*_to_dictfunctions) and write dictionaries to JSON or Excel files (dict_to_json,dict_to_excel). - Monte Carlo helpers:
sample_fatiguecase, that runs a simulation and returns a table of realizations, andcreate_realization, that rebuilds a single realization from that table.
Format versions
The current format version is 0.2.0 (FORMAT_VERSION). Files without a
format_version field are assumed to be version 0.1.0 (the format used
by the former Forrester tool) and are upgraded on the fly by
upgrade_case, so they keep producing the same results.
Units
All values are expressed in SI units (m, N, Pa, rad), except where stated otherwise in the format reference.
Example
Functions:
-
format_version–Returns the format version of a case dictionary.
-
upgrade_case–Converts a case dictionary to the current format version.
-
load_inputfile–Loads a JSON input file to a dictionary.
-
create_bolt–Creates a bolt object from a dictionary of parameters.
-
create_washer–Creates a washer object from a dictionary of parameters.
-
create_nut–Creates a nut object from a dictionary of parameters.
-
create_flangesegment–Creates a flange segment from a dictionary of parameters.
-
create_fatiguecurve–Creates a fatigue curve object from a dictionary of parameters.
-
create_markovmatrix–Creates a Markov matrix object from a dictionary of parameters.
-
create_fatiguecase–Creates a BoltFatigueAnalysis object from a case dictionary.
-
load_fatiguecase–Loads a BoltFatigueAnalysis object from a JSON file.
-
create_distribution–Creates a scipy distribution from a dictionary of parameters.
-
create_sampler–Creates a pyflange sampler from a dictionary of parameters.
-
create_gap_sampler–Creates a random gap sampler from a dictionary of parameters.
-
create_fatiguecase_sampler–Creates a sampler of random fatigue cases.
-
load_fatiguecase_sampler–Loads a fatigue case sampler from a JSON file.
-
sample_fatiguecase–Runs a Monte Carlo simulation of a fatigue case.
-
create_realization–Rebuilds a single Monte Carlo realization.
-
bolt_to_dict–Converts a bolt object to a dictionary in the JSON format.
-
washer_to_dict–Converts a washer object to a dictionary in the JSON format.
-
nut_to_dict–Converts a nut object to a dictionary in the JSON format.
-
gap_to_dict–Converts a gap object to a dictionary in the JSON format.
-
flangesegment_to_dict–Converts a flange segment to a dictionary in the JSON format.
-
fatiguecurve_to_dict–Converts a fatigue curve object to a dictionary in the JSON format.
-
markovmatrix_to_dict–Converts a Markov matrix object to a dictionary in the JSON format.
-
fatiguecase_to_dict–Converts a fatigue analysis to a dictionary in the JSON format.
-
dict_to_json–Writes a dictionary to a JSON file.
-
dict_to_excel–Writes a dictionary to an Excel file.
Attributes:
-
FORMAT_VERSION–str: The version of the JSON format written by this module.
-
SUPPORTED_FORMAT_VERSIONS–tuple: The JSON format versions that this module can read.
-
LEGACY_DS_REF_MEAN–float: Mean fatigue reference stress range implicitly used by format 0.1.0
-
FLANGE_SEGMENT_TYPES–tuple: The flange segment types supported by the JSON format.
-
DEFAULT_FLANGE_SEGMENT_TYPE–str: The flange segment type used when
FlangeSegment.typeis omitted -
SAMPLE_COLUMNS–list: The columns of the table returned by
sample_fatiguecase.
FORMAT_VERSION
module-attribute
¶
str: The version of the JSON format written by this module.
SUPPORTED_FORMAT_VERSIONS
module-attribute
¶
tuple: The JSON format versions that this module can read.
LEGACY_DS_REF_MEAN
module-attribute
¶
float: Mean fatigue reference stress range implicitly used by format 0.1.0 samplers, in Pa. Format 0.1.0 files get it assigned when upgraded.
FLANGE_SEGMENT_TYPES
module-attribute
¶
tuple: The flange segment types supported by the JSON format.
DEFAULT_FLANGE_SEGMENT_TYPE
module-attribute
¶
str: The flange segment type used when FlangeSegment.type is omitted
(IEC 61400-6 AMD1 model).
SAMPLE_COLUMNS
module-attribute
¶
SAMPLE_COLUMNS = ['BoltPreload', 'GapShapeFactor', 'TiltAngle', 'GapAngle', 'GapHeight', 'FatigueClass', 'MomentRangeCoefficient', 'Damage', 'AllowableDamage', 'FatigueLife', 'SMF', 'Utilization']
list: The columns of the table returned by sample_fatiguecase.
format_version
¶
Returns the format version of a case dictionary.
Parameters:
-
data(dict) –A case dictionary, as read from a JSON case file.
Returns:
-
str–The value of the
format_versionfield, or"0.1.0"if the field is missing.
upgrade_case
¶
Converts a case dictionary to the current format version.
The input dictionary is not modified: a converted copy is returned.
Upgrading a version 0.1.0 case:
- sets
format_versionto the current version; - adds
DS_ref_mean = 62 MPatoStats.FatigueClassFactor, if theStatssection is present, which is the value implicitly used by the version0.1.0Monte Carlo sampler.
Parameters:
-
data(dict) –A case dictionary in any supported format version.
Returns:
-
dict–An equivalent case dictionary in the current format version.
Raises:
-
ValueError–If the format version is not supported.
load_inputfile
¶
Loads a JSON input file to a dictionary.
The content is returned as it is, without any format upgrade or
validation. Use upgrade_case to convert it to the current version.
Parameters:
-
file_path(str) –Path of the JSON file.
Returns:
-
dict–The content of the JSON file.
create_bolt
¶
Creates a bolt object from a dictionary of parameters.
Parameters:
-
**params(Any, default:{}) –The bolt parameters. The
typekey selects the bolt kind:"StandardMetricBolt"(keysdesignation,material_grade,shank_length,shank_diameter_ratio,stud) or"MetricBolt"(keysnominal_diameter,thread_pitch,yield_stress,ultimate_tensile_stress,elastic_modulus,poissons_ratio,shank_length,shank_diameter_ratio,stud).
Returns:
-
MetricBolt–The bolt object.
Raises:
-
ValueError–If the bolt type is not supported.
create_washer
¶
Creates a washer object from a dictionary of parameters.
Parameters:
-
params(dict | None) –The washer parameters, or
Nonefor no washer. Thetypekey selects the washer kind:"ISOFlatWasher"(keydesignation) or"FlatWasher"(keysouter_diameter,inner_diameter,thickness,elastic_modulus,poissons_ratio).
Returns:
-
FlatWasher | None–The washer object, or
None.
Raises:
-
ValueError–If the washer type is not supported.
create_nut
¶
Creates a nut object from a dictionary of parameters.
Parameters:
-
**params(Any, default:{}) –The nut parameters. The
typekey selects the nut kind:"ISOHexNut"or"RoundNut"(keydesignation), or"HexNut"(keysnominal_diameter,thickness,inscribed_diameter,circumscribed_diameter,bearing_diameter,elastic_modulus,poissons_ratio).
Returns:
-
HexNut–The nut object.
Raises:
-
ValueError–If the nut type is not supported.
create_flangesegment
¶
Creates a flange segment from a dictionary of parameters.
Given a dictionary containing the parameters for a flange segment object
in the PyFlange JSON format (FlangeSegment section), returns the
corresponding flange segment. Only L-flange segments are supported.
The bolt preload is given either as preload_ratio (fraction of the bolt
tensile yield force, As * fy) or as an absolute force Fv, but not both.
Parameters:
-
**params(Any, default:{}) –Flange segment parameters. Expected keys are 'a', 'b', 's', 't', 'R', 'central_angle', 'Zg', 'bolt', 'preload_ratio' or 'Fv', 'Do', 'washer', 'nut', 'gap', 'tilt_angle', 'E', 'G', 's_ratio', 'r', 'k_shell', 'PPL', 'DFT', 'settlement_factor' and, optionally, 'type' (default
DEFAULT_FLANGE_SEGMENT_TYPE). Other keys are ignored.
Returns:
-
PolynomialLFlangeSegment–The flange segment object (possibly a
PolynomialLFlangeSegmentED2).
Raises:
-
ValueError–If an unsupported type is provided, or if the preload is not specified exactly once.
create_fatiguecurve
¶
Creates a fatigue curve object from a dictionary of parameters.
Parameters:
-
**params(Any, default:{}) –Fatigue curve parameters (
BoltFatigueCurvesection). The optionaltypekey selects the curve kind:"BoltFatigueCurve"(default): keysBoltNominalDiameter,DS_ref,gamma_M."SingleSlopeFatigueCurve": keysm,DS_ref,N_ref."DoubleSlopeFatigueCurve": keysm1,m2,DS_ref,N_ref, whereDS_refandN_refdefine the knee point.
Returns:
-
FatigueCurve–The fatigue curve object.
Raises:
-
ValueError–If the curve type is not supported.
create_markovmatrix
¶
Creates a Markov matrix object from a dictionary of parameters.
Parameters:
-
**params(Any, default:{}) –Markov matrix parameters (
MarkovMatrixsection). Expected keys are 'range', 'mean', 'cycles', 'duration'.
Returns:
-
MarkovMatrix–The Markov matrix object.
create_fatiguecase
¶
Creates a BoltFatigueAnalysis object from a case dictionary.
Parameters:
-
data(dict) –A case dictionary, in any supported format version.
-
SMF(float, default:None) –Stress multiplication factor. If omitted, the
SMFvalue of the case is used, or 1.0 if the case has none.
Returns:
-
BoltFatigueAnalysis–The fatigue analysis object.
load_fatiguecase
¶
Loads a BoltFatigueAnalysis object from a JSON file.
Given the path of a JSON file containing a flange fatigue case in the PyFlange JSON format, returns the corresponding BoltFatigueAnalysis object.
Parameters:
-
file_path(str) –The path to the JSON file.
-
SMF(float, default:None) –Stress multiplication factor. If omitted, the
SMFvalue of the case is used, or 1.0 if the case has none.
Returns:
-
BoltFatigueAnalysis–The loaded fatigue analysis object.
create_distribution
¶
Creates a scipy distribution from a dictionary of parameters.
Parameters:
-
**params(Any, default:{}) –Distribution parameters. The
Distributionkey selects the distribution kind:"Normal": keysMean,CoV."LogNormal": keysMean,CoV."Weibull": keysShape,Scale."Beta": keysAlpha,Betaand optionallyx0,x1(bounds of the definition interval, default 0 and 1).
Returns:
-
rv_frozen–A frozen scipy continuous distribution.
Raises:
-
ValueError–If an unsupported distribution type is provided.
create_sampler
¶
Creates a pyflange sampler from a dictionary of parameters.
Parameters:
-
**params(Any, default:{}) –Sampler parameters. All the distributions supported by
create_distributionare accepted, plus"Product", which samples the product of two independent variables described by theDist1andDist2keys.
Returns:
-
Generator–A sampler yielding random values.
Raises:
-
ValueError–If an unsupported distribution type is provided.
create_gap_sampler
¶
Creates a random gap sampler from a dictionary of parameters.
Parameters:
-
flange_diameter(float) –The outer diameter of the flange.
-
**params(Any, default:{}) –Gap sampler parameters (
Stats.Gapsection). TheJointDistributionkey selects the kind of sampler:"IEC 61400-6:2020 AMD1": keysFlatnessTolerance,Angle(distribution),ShapeFactor(distribution)."IEC 61400-6:2020 ED2": keyFlatnessTolerance."ClaytonCopula": keysTheta,Height(distribution),Angle(distribution),ShapeFactor(distribution).
Returns:
-
Generator–A sampler yielding random
pyflange.flangesegments.Gapobjects.
Raises:
-
ValueError–If an unsupported joint distribution is provided.
create_fatiguecase_sampler
¶
Creates a sampler of random fatigue cases.
The random flange segments are of the same type as fseg and share its
deterministic parameters. Their shell stiffness is always interpolated
from the FEA data (see pyflange.flangesegments.shell_stiffness) as a
function of the random gap angle.
Parameters:
-
fseg(PolynomialLFlangeSegment) –The base flange segment object.
-
markov_matrix(MarkovMatrix) –The base Markov matrix object.
-
**params(Any, default:{}) –Statistical parameters (
Statssection, current format version), for preload, plastic loss, DFT, gap, tilt, load range factor, fatigue class factor, allowable damage and stress multiplication factor.
Returns:
-
Generator–A generator that yields randomized BoltFatigueAnalysis objects.
Raises:
-
ValueError–If an unsupported distribution type is provided for any parameter.
-
KeyError–If a required parameter is missing.
load_fatiguecase_sampler
¶
Loads a fatigue case sampler from a JSON file.
Parameters:
-
file_path(str) –The path to the JSON file containing the fatigue case and its
Statssection.
Returns:
-
Generator–A generator that yields randomized BoltFatigueAnalysis objects.
sample_fatiguecase
¶
Runs a Monte Carlo simulation of a fatigue case.
Parameters:
-
data(dict) –A case dictionary, including the
Statssection. -
size(int) –Number of realizations to generate.
-
seed(int, default:None) –Seed of the random number generator. If given, the numpy global random generator is re-seeded, so that the simulation is reproducible. If omitted, the current random state is used.
-
progress(callable, default:None) –A function called as
progress(i)after the generation of the i-th realization (1-based).
Returns:
-
DataFrame–A table with one row per realization and the columns listed in
SAMPLE_COLUMNS. Lengths are in m, forces in N, angles in rad, stresses in Pa, fatigue life in the time units of the Markov matrix duration.FatigueClassis the knee-point stress range of the sampled bolt S-N curve andMomentRangeCoefficientthe factor applied to the load ranges.
create_realization
¶
Rebuilds a single Monte Carlo realization.
Given the case that generated a sample and one row of the table returned
by sample_fatiguecase (or of the corresponding CSV file), rebuilds the
random fatigue case that produced that row.
Parameters:
-
data(dict) –The case dictionary that generated the sample.
-
record(Mapping) –One realization, with the keys listed in
SAMPLE_COLUMNS(e.g. apandas.Seriesrow).
Returns:
-
BoltFatigueAnalysis–The rebuilt fatigue case.
bolt_to_dict
¶
Converts a bolt object to a dictionary in the JSON format.
Parameters:
-
bolt(MetricBolt) –The bolt object.
Returns:
-
dict–The bolt parameters, with type
"MetricBolt".
Raises:
-
ValueError–If the bolt type is not supported.
washer_to_dict
¶
Converts a washer object to a dictionary in the JSON format.
Parameters:
-
washer(FlatWasher | None) –The washer object.
Returns:
-
dict | None–The washer parameters, with type
"FlatWasher", orNoneifwasherisNone.
Raises:
-
ValueError–If the washer type is not supported.
nut_to_dict
¶
Converts a nut object to a dictionary in the JSON format.
Parameters:
-
nut(HexNut) –The nut object.
Returns:
-
dict–The nut parameters, with type
"HexNut".
Raises:
-
ValueError–If the nut type is not supported.
gap_to_dict
¶
Converts a gap object to a dictionary in the JSON format.
Parameters:
-
gap(Gap) –The gap object.
Returns:
-
dict–The gap parameters.
Raises:
-
ValueError–If
gapis not a Gap object.
flangesegment_to_dict
¶
Converts a flange segment to a dictionary in the JSON format.
Parameters:
-
fseg(PolynomialLFlangeSegment) –The flange segment object.
-
verbose(bool, default:False) –If True, the dictionary also contains a report of derived and intermediate quantities (preload after losses, polynomial reference points and coefficients, transfer functions, stiffnesses, etc.). These extra keys are ignored when the dictionary is loaded back. Defaults to False.
Returns:
-
dict–The flange segment parameters.
Raises:
-
ValueError–If the flange segment type is not supported.
fatiguecurve_to_dict
¶
Converts a fatigue curve object to a dictionary in the JSON format.
Parameters:
-
sncurve(FatigueCurve) –A
BoltFatigueCurve,SingleSlopeFatigueCurveorDoubleSlopeFatigueCurveobject.
Returns:
-
dict–The fatigue curve parameters. For a
BoltFatigueCurve, the knee point of the design curve is reported too, asm1,m2,DS12andN_ref.
Raises:
-
ValueError–If the fatigue curve type is not supported.
markovmatrix_to_dict
¶
Converts a Markov matrix object to a dictionary in the JSON format.
Parameters:
-
mkvm(MarkovMatrix) –The Markov matrix object.
Returns:
-
dict–The Markov matrix parameters.
Raises:
-
ValueError–If
mkvmis not a MarkovMatrix object.
fatiguecase_to_dict
¶
Converts a fatigue analysis to a dictionary in the JSON format.
The returned dictionary contains the case input (it can be loaded back
with create_fatiguecase) and the analysis results.
Parameters:
-
fcase(BoltFatigueAnalysis) –The fatigue analysis.
-
verbose(bool, default:False) –If True, the flange segment report (see
flangesegment_to_dict), the bolt stress Markov matrix and the fatigue life are included too. Defaults to False.
Returns:
-
dict–The fatigue case data and results (
damage,allowable_damage,SMFand, if verbose,FatigueLife).
Raises:
-
ValueError–If
fcaseis not a BoltFatigueAnalysis object.
dict_to_json
¶
Writes a dictionary to a JSON file.
Numpy scalars and arrays are converted to plain Python numbers and lists.
Parameters:
-
d(dict) –The dictionary to be written.
-
file_path(str) –The path to the JSON file to be created.
dict_to_excel
¶
Writes a dictionary to an Excel file.
This utility function takes a dictionary and an Excel file name as inputs, flattens the dictionary into a list of chained-key-value pairs, and writes it to an Excel file in a two-column table, where the first column contains the keys and the second column contains the corresponding values. Each value cell is also given an Excel name equal to its key, so that it can be referenced by name from other workbooks.
Example
Given the following dictionary: d = { "a": 10, "b": { "c": 20, "d": 30 } }
This function will write it to Excel in the following format: | KEY | VALUE | |-----|-------| | a | 10 | | b.c | 20 | | b.d | 30 |
Parameters:
-
d(dict) –The dictionary to be written.
-
file_path(str) –The path to the Excel file to be created.