Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ authors = ["Mathieu Aucejo <79322171+maucejo@users.noreply.github.com> and contr

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Scanf = "6ef1bc8b-493b-44e1-8d40-549aa65c4b41"

[compat]
Dates = "1.11.0"
Glob = "1.3.1"
Printf = "1.11.0"
Scanf = "0.5.4"
Test = "1.11.0"
Expand Down
13 changes: 9 additions & 4 deletions src/datasets/dataset164.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ Universal Dataset Number: 164
function parse_dataset164(io)
# Record 1: FORMAT(I10,20A1,I10)
r1 = readline(io)
units, description, temperature_mode = @scanf(r1, "%10d%20s%10d", Int, String, Int)[2:end]
units, description, temperature_mode = @scanf(r1, "%10d%20c%10d", Int, String, Int)[2:end]
description = strip(description)

# Record 2: FORMAT(3D25.17)
conversion_length, conversion_force, conversion_temperature = @scanf(readline(io), "%25e%25e%25e", Float64, Float64, Float64)[2:end]

conversion_temperature_offset = @scanf(readline(io), "%25e", Float64)[2]
r2 = readline(io)
r2 = replace(r2, 'D' => 'E')
conversion_length, conversion_force, conversion_temperature = @scanf(r2, "%25e%25e%25e", Float64, Float64, Float64)[2:end]

r3 = readline(io)
r3 = replace(r3, 'D' => 'E')
conversion_temperature_offset = @scanf(r3, "%25e", Float64)[2]

# Read trailing " -1"
readline(io)
Expand Down
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit1.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
1Meter (newton) 2
1.00000000000000000D+00 1.00000000000000000D+00 1.00000000000000000D+00
2.73150000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit2.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
2Foot (pound f) 2
3.28083989501312345D+00 2.24808943099710489D-01 1.80000000000000000D+00
4.59670000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit3.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
3Meter (kilogram f) 2
1.00000000000000000D+00 1.01971621297792824D-01 1.00000000000000000D+00
2.73150000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit4.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
4Foot (poundal) 2
3.28083989501312345D+00 7.23301385120989426D+00 1.80000000000000000D+00
4.59670000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit5.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
5mm (milli-newton) 2
1.00000000000000000D+03 1.00000000000000000D+03 1.00000000000000000D+00
2.73150000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit6.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
6cm (centi-newton) 2
1.00000000000000000D+02 1.00000000000000000D+02 1.00000000000000000D+00
2.73150000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit7.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
7Inch (pound f) 2
3.93700787401574814D+01 2.24808943099710489D-01 1.80000000000000000D+00
4.59670000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit8.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
8mm (kilogram f) 2
1.00000000000000000D+03 1.01971621297792824D-01 1.00000000000000000D+00
2.73150000000000000D+02
-1
6 changes: 6 additions & 0 deletions src/example_unit_datasets/Unit9.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-1
164
10mm (newton) 2
1.00000000000000000D+03 1.00000000000000000D+00 1.00000000000000000D+00
2.73150000000000000D+02
-1
131 changes: 100 additions & 31 deletions src/unit_conversion.jl
Original file line number Diff line number Diff line change
@@ -1,53 +1,110 @@
"""
convert_to_si!(ds)
convert_to_si!(ds, ds164)
convert_to_si!(ds, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)

Converts the units of the given UFF dataset `ds` to SI units in place.
Note that the mass is defined as F/a and its units will be determined by F & a

**Input**
- `ds`: A UFF dataset object that contains dimensional data to be converted.
- `ds`: A vector of UFF datasets that contains dimensional data to be converted.
- `ds164`: (Optional) A Dataset164 object that provides conversion factors.
- `conversion_length`: (Optional) Conversion factor for length units.
- `conversion_force`: (Optional) Conversion factor for force units.
- `conversion_temperature`: (Optional) Conversion factor for temperature units.
- `temperature_offset`: (Optional) Offset to be applied for temperature conversion.

convert_to_si!(ds) reads the last encountered 164 dataset and uses its conversion factors, defaults to SI
convert_to_si!(ds, ds164) ignores the dataset164 datasets in the vector of datasets and uses the argument ds164
convert_to_si!(ds, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
creates a ds164 and acts the same as convert_to_si!(ds, ds164)

**Output**
- `ds`: Dataset with its data converted to SI units.
"""
function convert_to_si!(ds, ds164)
conversion_length = ds164.conversion_length
conversion_force = ds164.conversion_force
conversion_temperature = ds164.conversion_temperature
temperature_offset = ds164.temperature_offset
function convert_to_si!(dsconvert_to_si!(ds, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 273.15))
ds164 = Dataset164(1,"User Defined", 2, conversion_length, conversion_force, conversion_temperature, temperature_offset)
convert_to_si!(ds, ds164)
end

convert_to_si!(ds, conversion_length, conversion_force, conversion_temperature, temperature_offset)
function convert_to_si!(datasets::Vector{UFFDataset})
# This function uses the dataset164 in the vector of datasets to
# perform the conversion, with a default of SI until the first
# dataset164 is read
ds164 = Dataset164(1,"SI", 2, 1.0, 1.0, 1.0, 273.15)
for ds in datasets
if replace(string(:Dataset58), "Dataset" => "") in supported_datasets()
ds164 = convert_to_si!(ds, ds164)
else
@warn "File type $(ds.type) not support for unit conversions"
end
end
return nothing
end

function convert_to_si!(ds::Dataset15, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(datasets::Vector{UFFDataset}, ds164::Dataset164)
# This function uses the ds164 as the 2nd argument to
# perform the conversion, regardless of the dataset164's that are in
# the vector of datasets
for ds in datasets
if replace(string(:Dataset58), "Dataset" => "") in supported_datasets()
_ = convert_to_si!(ds, ds164)
else
@warn "File type $(ds.type) not support for unit conversions"
end
end
return nothing
end

ds.node_coords ./= conversion_length
function convert_to_si!(ds::Dataset15, ds164)

ds.node_coords ./= ds164.conversion_length
return ds164
end

function convert_to_si!(ds::Dataset18, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset18, ds164)

ds.cs_origin ./= conversion_length
ds.cs_x ./= conversion_length
ds.cs_xz ./= conversion_length
ds.cs_origin ./= ds164.conversion_length
ds.cs_x ./= ds164.conversion_length
ds.cs_xz ./= ds164.conversion_length
return ds164
end


function convert_to_si!(ds::Dataset82, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset82, ds164)

return nothing
return ds164
end

function convert_to_si!(ds::Dataset151, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset151, ds164)

return nothing
return ds164
end

function convert_to_si!(ds::Dataset58, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset55, ds164)

# Convert data vector
# Implemented for data types 8, 11, 12, 9, 13, 15

factor = 1.
# Data Type
if any(ds.spec_dtype .== (0, 1))
factor /= 1.
elseif any(ds.spec_dtype .== (8, 11, 12))
factor /= ds164.conversion_length
elseif any(ds.spec_dtype .== (4, 9))
factor /= ds164.conversion_force
elseif any(ds.spec_dtype .== (2, 15))
factor /= (ds164.conversion_force/ds164.conversion_length^2)
else
@warn "Conversion factor for $(ds.spec_dtype) not implemented, please submit PR"
end

ds.data .*= factor
return ds164
end

function convert_to_si!(ds::Dataset58, ds164)

# Convert data vector
# Implemented for ordinate data types 8, 11, 12, 9, 13, 15
Expand All @@ -63,42 +120,54 @@ function convert_to_si!(ds::Dataset58, conversion_length = 1., conversion_force
if any(ds.ord_spec_dtype .== (0, 1))
factor /= 1.
elseif any(ds.ord_spec_dtype .== (8, 11, 12))
factor /= conversion_length
factor /= ds164.conversion_length
elseif any(ds.ord_spec_dtype .== (9, 13))
factor /= conversion_force
factor /= ds164.conversion_force
elseif any(ds.ord_spec_dtype .== (15))
factor /= (conversion_force/conversion_length^2)
factor /= (ds164.conversion_force/ds164.conversion_length^2)
else
@warn "Conversion factor for $(ds.ord_spec_dtype) not implemented, please submit PR"
end

# Ordinate Denominator
if any(ds.ord_denom_spec_dtype .== (0, 1))
factor /= 1.
factor *= 1.
elseif any(ds.ord_denom_spec_dtype .== (8, 11, 12))
factor /= conversion_length
factor *= ds164.conversion_length
elseif any(ds.ord_denom_spec_dtype .== (9, 13))
factor /= conversion_force
factor *= ds164.conversion_force
elseif any(ds.ord_denom_spec_dtype .== (15))
factor /= (conversion_force/conversion_length^2)
factor *= (ds164.conversion_force/ds164.conversion_length^2)
else
@warn "Conversion factor for $(ds.ord_denom_spec_dtype) not implemented, please submit PR"
end

ds.data .*= factor
return ds164
end

function convert_to_si!(ds::Dataset164, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset164, ds164)

return nothing
return ds
end

function convert_to_si!(ds::Dataset2411, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset1858, ds164)

ds.node_coords .*= conversion_length
return ds164
end

function convert_to_si!(ds::Dataset2412, conversion_length = 1., conversion_force = 1., conversion_temperature = 1., temperature_offset = 0.)
function convert_to_si!(ds::Dataset2411, ds164)

return nothing
ds.node_coords ./= conversion_length
end

function convert_to_si!(ds::Dataset2412, ds164)

return ds164
end

function convert_to_si!(ds::Dataset2414, ds164)

# To Do
return ds164
end
59 changes: 59 additions & 0 deletions test/datasets/dataset151_164_58_55.unv
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-1
151
Test Universal File
File specific to show unit conversion capability of UFFFiles
Test for Ideas
17-Oct-25 14:58:08 28 0 0
None None
Compatible to SDRC
23-Oct-25 14:38:11
-1
-1
164
7Inch (pound f) 2
3.93700787401574814D+01 2.24808943099710489D-01 1.80000000000000000D+00
4.59670000000000000D+02
-1
-1
58
NONE
NONE
28-Jan-21 14:01:42
NONE
NONE
4 1 1 0 NONE 1 1 NONE 22 1
5 10 1 0.00000e+00 2.50000e-01 0.00000e+00
18 0 0 0 NONE NONE
12 1 0 0 NONE NONE
13 0 1 0 NONE NONE
0 0 0 0 NONE NONE
1.73331e-04 0.00000e+00 -5.45225e-07 -1.32963e-05 -1.37127e-06 -6.95395e-06
-3.70260e-06 -1.21236e-06 -1.33769e-06 -5.04498e-07 1.33122e-06 -4.35957e-06
-1.94365e-06 -9.69129e-07 -1.16043e-06 -3.54780e-06 -1.51112e-06 -3.34955e-07
9.80896e-07 -1.77917e-06
-1
-1
55
None, ConjMAC= 0.987, MPC= 0.894
NONE
NONE
NONE
NONE
1 3 2 8 5 3
2 6 1 1
-9.0604E-01 2.5080E+01 -9.6099E-07 -1.2959E-05 3.2587E-04 -1.2360E-05
1
2.2180E-07 1.7824E-06 1.2057E-08 -1.1953E-05 1.3622E-07 1.8514E-07
2
9.0184E-07 1.4043E-06 -1.0507E-08 -1.2645E-05 1.2502E-07 -1.7648E-07
3
7.6630E-07 2.6104E-06 -5.7568E-08 -1.2168E-05 6.9899E-08 9.3158E-08
4
6.6829E-07 2.6144E-06 -4.6781E-08 -1.2152E-05 1.0911E-07 2.6736E-07
5
7.2950E-07 3.1019E-06 -2.5970E-07 -1.1832E-05 6.3576E-08 -1.1018E-07
6
8.1909E-07 3.2610E-06 -2.1364E-07 -1.2211E-05 2.0570E-08 7.6194E-08
7
2.6237E-07 3.9112E-06 -9.6099E-07 -1.2959E-05 5.2119E-07 6.6091E-07
-1
Loading