Skip to content

Commit

Permalink
fix instabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
longemen3000 committed Jan 26, 2022
1 parent 6aaec21 commit 7a26760
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 115 deletions.
46 changes: 46 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Pages = ["api.md"]
```@docs
ThermoState.spec
ThermoState.state
ThermoState.get_spec
ThermoState.has_spec
ThermoState.state_type
```

## Utilities
Expand All @@ -27,6 +32,44 @@ ThermoState.default_units

## Defined Properties

The following properties are accepted by an state and have an accesor function:

| Property |Units |Accessor function |Keywords for `spec` and `state` |
|----------------------|----------|-----------------------|--------------------------------|
| Molar volume |m3/mol |`mol_volume` | `v`, `mol_v` |
| Mass volume |m3/kg |`mass_volume` | `mass_v` |
| Total volume |m3 |`total_volume` | `total_v`, `V` |
| Molar density |mol/m3 |`mol_density` | `ρ`, `mol_ρ`, `rho`, `mol_rho` |
| Mass density |mol/kg |`mass_density` | `mass_ρ`, `mass_rho` |
| Temperature |K |`temperature` | `t`, `T` |
| Pressure |Pa |`pressure` | `p`, `P` |
| Molar enthalpy |J/mol |`mol_enthalpy` | `h`, `mol_h` |
| Mass enthalpy |J/kg |`mass_enthalpy` | `mass_h` |
| Total enthalpy |J |`total_enthalpy` | `total_h` |
| Molar Gibbs |J/mol |`mol_gibbs` | `g`, `mol_g` |
| Mass Gibbs |J/kg |`mass_gibbs` | `mass_g` |
| Total Gibbs |J |`total_gibbs` | `total_g` |
| Molar Helmholtz |J/mol |`mol_helmholtz` | `a`, `mol_a` |
| Mass Helmholtz |J/kg |`mass_helmholtz` | `mass_a` |
| Total Helmholtz |J |`total_helmholtz` | `total_a` |
| Molar Internal Energy|J/mol |`mol_internal_energy` | `a`, `mol_a` |
| Mass Internal Energy |J/kg |`mass_internal_energy` | `mass_a` |
| Total Internal Energy|J |`total_internal_energy`| `total_a` |
| Molar entropy |J/(mol K) |`mol_entropy` | `s`, `mol_s` |
| Mass entropy |J/(kg K) |`mass_entropy` | `mass_s` |
| Total entropy |J/K |`total_entropy` | `total_s` |
| Amount of Moles |mol |`moles` | `moles` |
| Amount of Mass |kg |`mass` | `mass` |
| Molar Fraction |no units |`mol_fraction` | `xn` |
| Mass Fraction |no units |`mass_fraction` | `xm` |
| Molar Numbers |mol |`mol_number` | `n` |
| Mass Numbers |kg |`mass_number` | `m` |
| Molar Vapor Fraction |no units |`mol_vapor_fraction` | `vfrac` |
| Mass Vapor Fraction |no units |`mass_vapor_fraction` | `quality` |
| Current Phase |no units |`phase` | `phase`, `sat` |
| Aditional Options |no units |`options` | `options` |
---

### PVT Properties
```@docs
ThermoState.temperature
Expand Down Expand Up @@ -58,15 +101,18 @@ ThermoState.total_internal_energy
```

### Mass and Molar Properties
```@docs
ThermoState.mol_fraction
ThermoState.mass_fraction
ThermoState.mol_number
ThermoState.mass_number
ThermoState.moles
ThermoState.mass
ThermoState.molar_mass
```

### Other Properties
```@docs
ThermoState.options
ThermoState.phase
ThermoState.quality
Expand Down
7 changes: 1 addition & 6 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CurrentModule = ThermoState
ThermoState.jl is a basic block for interfacing and specifying thermodynamic models. this package proposes and provides tools to create a common property interface of the form:

```julia
property(model,state,unit)
property(model,state::ThermodynamicState,unit)::ThermodynamicState
```

This package proposes the following conventions for naming properties:
Expand All @@ -17,10 +17,5 @@ This package proposes the following conventions for naming properties:

- `total_$PROPERTY` is a property of units U. (total Helmholtz energy has joule units)

See all defined properties in the API section
```@index
```




46 changes: 46 additions & 0 deletions docs/src/quickstart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Quick Start

Lets suppose a mix of water (18.01 `g/mol`) and ethanol (46.07 `g/mol`)

```julia
using Unitful,ThermoState
#m = mass amount
#n = molar amount
#xn = molar fraction
#xm = mass fraction
mw = [18.01,46.07]
st = state(t = 300u"°C",mass_rho=805u"g/L",n =[5.1,4.2]u"mol")
```
- What's is the molecular weight of the mix?

```julia-repl
julia> molar_mass(FromState(),st,mw)
0.03068225806451613
```
The result is in `kg/mol`, we could use `g/mol` instead:
```julia-repl
julia> molar_mass(FromState(),st,u"g/mol",mw)
30.68225806451613
```


- How many moles do we have in our mix?

```julia-repl
julia> moles(FromState(),st,mw)
9.3
```

For this specific combination of properties and state, we don't need a molecular weight vector:
```julia-repl
julia> moles(FromState(),st)
9.3
```

- What's the volume of the mix in `inch³`
```julia-repl
julia> total_volume(FromState(),st,u"inch^3",mw)
21.63083261951725
```


2 changes: 1 addition & 1 deletion src/ThermoState.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export Spec,ThermodynamicState,VariableSpec
export spec,state

#spec utilities
export value,specification,values,get_spec
export specification,get_spec,has_spec,amount_type
#FromState Model
export FromState

Expand Down
55 changes: 51 additions & 4 deletions src/fromspecs_props.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
##TODO: CHECK UNITS

const MolarEnergyUnits = Unitful.Units{U,(Unitful.𝐍^-1)*Unitful.𝐌*Unitful.𝐋^2/Unitful.𝐓^2,A} where A where U
const MassEnergyUnits = Unitful.Units{U,Unitful.𝐋^2/Unitful.𝐓^2,A} where A where U
const MolUnits = Unitful.Units{U,Unitful.𝐍,A} where A where U
Expand Down Expand Up @@ -34,23 +32,39 @@ function mass(model::FromState,st::ThermodynamicState,unit::T=u"kg",mw=nothing)
return convert_unit(u"kg",unit,val)
end

function mass(model::FromState,st::ThermodynamicState,mw::T) where T
return mass(model,st,u"kg",mw)
end

function moles(model::FromState,st::ThermodynamicState,unit::T=u"mol",mw=nothing) where T <: MolUnits
val = moles2(st,mw)
return convert_unit(u"mol",unit,val)
end

function molar_mass(model::FromState,st::ThermodynamicState,unit=u"kg/mol",mw=nothing)
function moles(model::FromState,st::ThermodynamicState,mw::T) where T
return moles(model,st,u"mol",mw)
end

function molar_mass(model::FromState,st::ThermodynamicState,unit::T = u"kg/mol",mw=nothing) where T<:Unitful.Units
val = kg_per_mol2(st,mw)
return convert_unit(u"kg/mol",unit,val)
end

function molar_mass(model::FromState,st::ThermodynamicState,mw::T) where T
return molar_mass(model,st,u"kg/mol",mw)
end

for (op,sp) in zip((:mol_helmholtz, :mol_gibbs, :mol_internal_energy, :mol_enthalpy),INTENSIVE_ENERGY_UNITS)
@eval begin
function $op(model::FromState,st::ThermodynamicState,unit::T=u"J/mol",mw=nothing) where T <: MolarEnergyUnits
sval = throw_get_spec($sp,st)
val = to_spec(st,sval,mw,MOLAR())
return convert_unit(u"J/mol",unit,val)
end

function $op(model::FromState,st::ThermodynamicState,mw::T) where T
return $op(model,st,u"J/mol",mw)
end
end
end

Expand All @@ -60,7 +74,10 @@ for (op,sp) in zip((:mass_helmholtz, :mass_gibbs, :mass_internal_energy, :mass_e
sval = throw_get_spec($sp,st)
val = to_spec(st,sval,mw,MASS())
return convert_unit(u"J/kg",unit,val)
end
end
function $op(model::FromState,st::ThermodynamicState,mw::T) where T
return $op(model,st,u"J/kg",mw)
end
end
end

Expand All @@ -71,45 +88,74 @@ for (op,sp) in zip((:total_helmholtz, :total_gibbs, :total_internal_energy, :tot
val = to_spec(st,sval,mw,TOTAL())
return convert_unit(u"J",unit,val)
end
function $op(model::FromState,st::ThermodynamicState,mw::T) where T
return $op(model,st,u"J",mw)
end
end
end



function mol_entropy(model::FromState,st::ThermodynamicState,unit::T=u"J/(K*mol)",mw=nothing) where T <: MolEntropyUnits
sval = throw_get_spec(Entropy,st)
val = to_spec(st,sval,mw,MOLAR())
return convert_unit(u"J/(mol*K)",unit,val)
end

function mol_entropy(model::FromState,st::ThermodynamicState,mw::T) where T
return mol_entropy(model,st,u"J/(mol*K)",mw)
end

function mass_entropy(model::FromState,st::ThermodynamicState,unit::T=u"J/(K*kg)",mw=nothing) where T <: MassEntropyUnits
sval = throw_get_spec(Entropy,st)
val = to_spec(st,sval,mw,MASS())
return convert_unit(u"J/(kg*K)",unit,val)
end

function mass_entropy(model::FromState,st::ThermodynamicState,mw::T) where T
return mass_entropy(model,st,u"J/(kg*K)",mw)
end

function total_entropy(model::FromState,st::ThermodynamicState,unit::T=u"J/(K)",mw=nothing) where T <: EntropyUnits
sval = throw_get_spec(Entropy,st)
val = to_spec(st,sval,mw,TOTAL())
return convert_unit(u"J/(K)",unit,val)
end

function total_entropy(model::FromState,st::ThermodynamicState,mw::T) where T
return total_entropy(model,st,u"J/(K)",mw)
end

function total_volume(model::FromState,st::ThermodynamicState,unit::T=u"m^3",mw=nothing) where T <: Unitful.VolumeUnits
sval = throw_get_spec(VolumeAmount,st)
val = to_spec(st,sval,mw,VolumeAmount{TOTAL,VOLUME}())
return convert_unit(u"m^3",unit,val)
end

function total_volume(model::FromState,st::ThermodynamicState,mw::T) where T
return total_volume(model,st,u"m^3",mw)
end

function mass_volume(model::FromState,st::ThermodynamicState,unit::T=u"(m^3)/kg",mw=nothing) where T <: MassVolumeUnits
sval = throw_get_spec(VolumeAmount,st)
val = to_spec(st,sval,mw,VolumeAmount{MASS,VOLUME}())
return convert_unit(u"m^3/kg",unit,val)
end

function mass_volume(model::FromState,st::ThermodynamicState,mw::T) where T
return mass_volume(model,st,u"m^3/kg",mw)
end

function mol_volume(model::FromState,st::ThermodynamicState,unit::T=u"(m^3)/mol",mw=nothing) where T <: MolVolumeUnits
sval = throw_get_spec(VolumeAmount,st)
val = to_spec(st,sval,mw,VolumeAmount{MOLAR,VOLUME}())
return convert_unit(u"m^3/mol",unit,val)
end

function mol_volume(model::FromState,st::ThermodynamicState,mw::T) where T
return mol_volume(model,st,u"m^3/mol",mw)
end

function mass_density(model::FromState,st::ThermodynamicState,unit::T=u"kg/m^3",mw=nothing) where T <: MassDensityUnits
sval = throw_get_spec(VolumeAmount,st)
val = to_spec(st,sval,mw,VolumeAmount{MASS,DENSITY}())
Expand Down Expand Up @@ -201,3 +247,4 @@ default_units(::typeof(mass_cp)) = u"J/(kg*K)"
default_units(::typeof(mass_cv)) = u"J/(kg*K)"
default_units(::typeof(sound_speed)) = u"m/s"


2 changes: 1 addition & 1 deletion src/spec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct Spec{T <: AbstractSpec,U}
val::U
end

value(s::Spec) = s.val
value(s::T) where T<:Spec = s.val
specification(s::Spec) = s.type

"""
Expand Down
Loading

0 comments on commit 7a26760

Please sign in to comment.