Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Macros to define new variables #102

Merged
merged 20 commits into from
Nov 25, 2024
Merged

Use Macros to define new variables #102

merged 20 commits into from
Nov 25, 2024

Conversation

lllx125
Copy link
Collaborator

@lllx125 lllx125 commented Nov 20, 2024

Now setunits works as a macro that accepts a unit system and reassigns constants exported from AtomicAndPhysicalConstants with value in the proper unit.

Example:
@setunits MKS

This is because @setunits is called in init(), so I can't define constants in a function. However, this makes the constants type unstable.

The code should work properly if we ignore typing.

Copy link
Member

@DavidSagan DavidSagan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes problem with AcceleratorLattice.

@DavidSagan
Copy link
Member

I am getting the error for atoms:

julia> Species("He")
ERROR: type AtomicSpecies has no field mass_in_amu

@mattsignorelli
Copy link
Contributor

mattsignorelli commented Nov 20, 2024 via email

@DavidSagan
Copy link
Member

chargeof is broken:

julia> f = Species("electron")
Species("electron", -1 e, 0.51099895069 MeV c⁻², 0.5 ħ, -9.2847646917e-24 J T⁻¹, 0)

julia> chargeof(f)
ERROR: DimensionError: e and MeV c⁻² are not dimensionally compatible.
Stacktrace:
 [1] #s103#141
   @ ~/.julia/packages/Unitful/GYzMo/src/conversion.jl:7 [inlined]
 [2] var"#s103#141"(::Any, s::Any, t::Any)
   @ Unitful ./none:0
 [3] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
   @ Core ./boot.jl:707
 [4] uconvert(a::Unitful.FreeUnits{(e,), 𝐈 𝐓, nothing}, x::Quantity{Float64, 𝐌, Unitful.FreeUnits{(c⁻², MeV), 𝐌, nothing}})
   @ Unitful ~/.julia/packages/Unitful/GYzMo/src/conversion.jl:72
 [5] (::AcceleratorLattice.var"#6#8"{Unitful.FreeUnits{(e,), 𝐈 𝐓, nothing}})(species::Species)
   @ AcceleratorLattice ~/.julia/dev/AtomicAndPhysicalConstants/src/set_units.jl:140
 [6] top-level scope
   @ REPL[6]:1

@lllx125
Copy link
Collaborator Author

lllx125 commented Nov 22, 2024

Allow macro to be set with keyword parameters

@lllx125 lllx125 requested a review from DavidSagan November 22, 2024 17:55
Copy link
Member

@DavidSagan DavidSagan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest code I get:

julia> using AtomicAndPhysicalConstants
Precompiling AtomicAndPhysicalConstants...
Info Given AtomicAndPhysicalConstants was explicitly requested, output will be shown live 
┌ Warning: Symbol ħ was found in multiple registered unit modules.
│ We will use the one from AtomicAndPhysicalConstants.NewUnits.
└ @ Unitful ~/.julia/packages/Unitful/GYzMo/src/user.jl:715
┌ Warning: Symbol ħ was found in multiple registered unit modules.
│ We will use the one from AtomicAndPhysicalConstants.NewUnits.
└ @ Unitful ~/.julia/packages/Unitful/GYzMo/src/user.jl:715
┌ Warning: Symbol ħ was found in multiple registered unit modules.
│ We will use the one from AtomicAndPhysicalConstants.NewUnits.
└ @ Unitful ~/.julia/packages/Unitful/GYzMo/src/user.jl:715
... And lots more lines like this...

julia> f = Species("electron")
Species("electron", -1 e, 0.51099895069 MeV c⁻², 0.5 ħ, -9.2847646917e-24 J T⁻¹, 0)

julia> chargeof(f)
ERROR: UndefVarError: `chargeof` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

Also if a "null" species can be defined that would be helpful.

@mattsignorelli
Copy link
Contributor

@DavidSagan Don't you need to call @AAPCdef first?

I'm not sure about the hbar thing though

@DavidSagan
Copy link
Member

@mattsignorelli Yes I had forgot that. The package should be configured to throw an error message in this case that tells the user they need to use @AAPCdef.

@DavidSagan
Copy link
Member

Also still getting the error:

julia> Species("He")
ERROR: type AtomicSpecies has no field mass_in_amu

@mattsignorelli
Copy link
Contributor

@mattsignorelli Yes I had forgot that. The package should be configured to throw an error message in this case that tells the user they need to use @AAPCdef.

That wouldn't work because massof and chargeof must not exist anywhere before calling the macro.

@DavidSagan
Copy link
Member

@mattsignorelli Can dummy functions be defined initially with the use statement to throw an error?

@DavidSagan
Copy link
Member

One more point: The default for @AAPCdef should be that C_LIGHT, chargeof, etc be numbers and not have units.

@mattsignorelli
Copy link
Contributor

@mattsignorelli Can dummy functions be defined initially with the use statement to throw an error?

This is dangerous because then every package using AAPC will need to do type piracy on these functions

@DavidSagan
Copy link
Member

One more problem: @AAPCdef works outside of AcceleratorLattice but when I try using AcceleratorLattice (with local changes to AL) I get:

julia> using AcceleratorLattice
Precompiling AcceleratorLattice...
Info Given AcceleratorLattice was explicitly requested, output will be shown live 
ERROR: LoadError: syntax: `global const` declaration not allowed inside function around /Users/dcs16/.julia/dev/AtomicAndPhysicalConstants/src/set_units.jl:140
Stacktrace:
 [1] top-level scope
   @ ~/.julia/dev/AcceleratorLattice/src/AcceleratorLattice.jl:26
 [2] include
   @ ./Base.jl:557 [inlined]

The code in AL that triggers this is:

  function __init__()
    @AAPCdef;
  end

@DavidSagan
Copy link
Member

@mattsignorelli Since AAPC is controlling things, I would not call this type piracy. In this case, it is definitely not dangerous. From the Julia manual:

Sometimes, coupled packages may engage in type piracy to separate features from definitions, especially when the packages were designed by collaborating authors, and when the definitions are reusable.

@mattsignorelli
Copy link
Contributor

function init()
@AAPCdef;
end

This is the problem, @APCdef should not be called in __init__ but in the global scope of accelerator lattice.

However @lllx125 the macro should just define const, not global const

@DavidSagan
Copy link
Member

I removed the __init__ from AcceleratorLattice and the global from APC and the global const declaration problem went away.

@DavidSagan
Copy link
Member

@lllx125 You need to change export @AAPCdef to export @APCdef

@DavidSagan DavidSagan self-requested a review November 25, 2024 18:01
@mattsignorelli mattsignorelli merged commit 5083cdc into main Nov 25, 2024
1 of 3 checks passed
@mattsignorelli mattsignorelli deleted the fix_eval branch November 25, 2024 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants