Closed AlexisRenchon closed 2 years ago
Don't bother about it yet though, I'm going to try a few things to fix it within DAMMmodel
, I'll keep you updated
You should never do include
of a package, using
instead. Otherwise Im not sure.
I did include
to to use my Module
locally, what is the right way? like this?
] activate .
using DAMMmodel
This gave me the error
ERROR: KeyError: key Symbol("Moles CO₂") not found
as well.
my Module look like this:
module DAMMmodel
using Statistics
using LsqFit
using DataFrames
using SparseArrays
using UnicodeFun
using GLMakie
using Unitful: R, L, mol, K, kJ, °C, m, g, cm, hr, mg, s, μmol
using UnitfulMoles: molC
using Unitful, UnitfulMoles
@compound CO₂
include(joinpath("constructors", "constants.jl"))
include(joinpath("constructors", "constructors.jl"))
include(joinpath("functions", "maths", "DAMM.jl"))
include(joinpath("functions", "maths", "DAMMfit.jl"))
include(joinpath("functions", "maths", "DAMMmat.jl"))
include(joinpath("functions", "viz", "DAMMplot.jl"))
include(joinpath("functions", "viz", "DAMMviz.jl"))
include(joinpath("functions", "maths", "qbins.jl"))
export DAMM, DAMMfit, DAMMmat, DAMMplot, DAMMviz, qbins, sDAMMmat, sDAMMmatq
end
I tried to add molCO2
to the export list, but it didn't help
if I run the Module (with include or just running all lines) then do using DAMMmodel
, everything works... it seems that @compound doesn't work when inside a package only, and I have no idea why...
if I do
using Unitful, UnitfulMoles
@compound CO2
after loading the package, then it works.
do you know of any other package using @compound? If yes, maybe they implement it differently and I could fix my issue doing it similarly?
I also tried to put @compound CO2
inside the function that uses it, but that creates a bug.
According to @briochemc : It's probable that the @compound macro was only intended for REPL use and that it does not follow the instructions in https://painterqubits.github.io/Unitful.jl/stable/extending/#Extending-Unitful
Well seeing I wrote it... I was trying to make it work everywhere I just didn't know how at the time. I was really new to Julia and this is a pretty horrific level of nested macros.
But it absolutely can work anywhere, and I tested it with your package and using
, but with your latest changes it doesn't work. Maybe we can find out why.
You can use @compound
or use @unit
(as it does) inside a function, because you cant define types inside a function - thats just how julia works.
Do you have the version of my package that worked? - was probably 0.1.7
Did you change anything in the module when you tried it?
I didn't change anything basically... I did mess around with the module DAMMmodel
to try to make it work, but I didn't change the function that uses mol CO2.
I am not sure what you did when you say "and I tested it with your package and using" but it sounds like you made it work and it should still work
I didn't merge that PR without your package working, that was the point ;)
(But actually this problem is from more detailed use that I didn't check, now I look at it)
I dont know what version now because I devved it and just pulled your update.
Here is I think what the problem is - specifically with gram conversion:
using DAMMmodel, Unitful
uconvert(u"g", 2DAMMmodel.molCO₂)
Seems like a small bug in @compound
The only thing that might be weird in my package in the module in DAMMmodel.jl
It was my bad that I didn't know how to test it properly... but the good thing in that I learned something, always do ] activate .
to test locally, not include()
the function using Unitful is here https://github.com/CUPofTEAproject/DAMMmodel.jl/blob/master/src/functions/maths/DAMM.jl
and the lines 36-38 where I use UnitfulMoles stuff:
Rₛₘₛ = @. Rₛₘ * Eₛ # Respiration, effective depth 10 cm, mg C cm⁻² hr⁻¹
Rₛₛₘ = Rₛₘₛ .|> molC*m^-2*s^-1
Rₛ = Rₛₛₘ .|> μmolCO₂*m^-2*s^-1
(It works fine except when @compound CO2 is inside the module as I put it in https://github.com/CUPofTEAproject/DAMMmodel.jl/blob/master/src/DAMMmodel.jl)
Its hard to test because just including this module doesn't make the error:
module TestModule
using Unitful, UnitfulMoles
@compound CO2
end
uconvert(u"g", 1TestModule.molCO₂)
It has to be a package...
do you just run the module? or do you do ] activate .
?
everything works for me when I run the module in Julia REPL, it only bugs if I do ] activate .
(@v1.7) pkg> activate .
Activating project at `~/MyPackages/DAMMmodel`
julia> using DAMMmodel
[ Info: Precompiling DAMMmodel [7af7f183-cd03-4039-bf6b-67ea3eb1648d]
julia> DAMM([18.2 0.3], (1e9, 64.0, 3.46e-8, 2.0e-3, 0.4, 0.0125))
ERROR: KeyError: key Symbol("Moles CO₂") not found
julia> include("DAMMmodel.jl")
Main.DAMMmodel
julia> using Main.DAMMmodel
julia> DAMM([18.2 0.3], (1e9, 64.0, 3.46e-8, 2.0e-3, 0.4, 0.0125))
1-element Vector{Float64}:
1.8836935508519232
Ok I think the problem is you have to register your package with Unitful.jl for all the macros to work with a package.
UnitfulMoles.jl has this code, it doesn' totally make sense to me but maybe you can swap in DAMMmodel for UnitfulMoles.
const localunits = Unitful.basefactors
function __init__()
merge!(Unitful.basefactors, localunits)
Unitful.register(UnitfulMoles)
end
ok I'll try to add that in my module, 2 min
Hmm doesn't seem to work with UnitfulMoles defining compounds.
At least I know what the problem is now - I'll define a test compound in UnitfulMoles to be sure it works doing it in a package.
it did work for me!
(@v1.7) pkg> activate .
Activating project at `~/MyPackages/DAMMmodel`
julia> using DAMMmodel
[ Info: Precompiling DAMMmodel [7af7f183-cd03-4039-bf6b-67ea3eb1648d]
julia> DAMM([18.2 0.3], (1e9, 64.0, 3.46e-8, 2.0e-3, 0.4, 0.0125))
1-element Vector{Float64}:
1.8836935508519232
Sweet! Thats great. We should add this to the readme so its clear you need to register with unitful for this to work from inside a package.
wait before closing, see if it works when I register
Ok. I've added this to the README, thanks for your patience working through this!
Thank you for helping me!
I really like what Unitful
and UnitfulMoles
bring to work with units. I've never done something like that in programming before.
I hope CliMA ESM will use it and will allow using DAMMmodel
as a module for their LSM.
Yeah it's pretty wild that this actually works. But do make sure to test your results independently sometimes ;)
I don't really work with mols of things anymore so mostly just use Unitful.jl, but yes its a huge game changer for physical/mechanistic models.
I think just this will work for you:
function __init__()
Unitful.register(DAMMmodel)
end
ok the registered package worked, we can close
I will test
function __init__()
Unitful.register(DAMMmodel)
end
yes, just
function __init__()
Unitful.register(DAMMmodel)
end
works
0.1.1 works locally for me:
But it doesn't work when registered
I am not sure what is happening