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

Document that constants can only contain 1D array #1412

Open
unalmis opened this issue Nov 27, 2024 · 5 comments
Open

Document that constants can only contain 1D array #1412

unalmis opened this issue Nov 27, 2024 · 5 comments
Labels
add warning Adds warning to prevent/detect bug documentation Add documentation or better warnings etc. P2 Medium Priority, not urgent but should be on the near-term agend

Comments

@unalmis
Copy link
Collaborator

unalmis commented Nov 27, 2024

Otherwise you get weird optimization errors.
Linking relevant comments:
#1288 (comment)
#1290 (comment)

@unalmis unalmis added documentation Add documentation or better warnings etc. add warning Adds warning to prevent/detect bug labels Nov 27, 2024
@dpanici dpanici added the P2 Medium Priority, not urgent but should be on the near-term agend label Dec 2, 2024
@dpanici
Copy link
Collaborator

dpanici commented Dec 2, 2024

Can you post a MWE? so we can see the error, and also try it with the omnigenity optimization?

@dpanici
Copy link
Collaborator

dpanici commented Dec 4, 2024

There may be something more than just having a 1D array, like it may have to also be used in a specific way for the error to occur. I say this because in PlasmaVesselDistance we have a 2D array in constants and I have not noticed this issue (the issue being, running the optimization twice in one python session causes some error relating to array size right?)

@unalmis
Copy link
Collaborator Author

unalmis commented Dec 4, 2024

My second linked comment would seem to disagree with that, as even if I don't use the constants array, for example by setting the objective.compute method to be a one liner that returns an array filled with the value 1.0, the same issue occurs.

The MWE is to take any objective function, for example ripple on #1290 , change the compute function to whatever you want, and the issue is reproduced if constants holds anything other than 1d arrays.

@unalmis
Copy link
Collaborator Author

unalmis commented Dec 4, 2024

issue being, running the optimization twice in one python session causes some error relating to array size right?

The error has always been the same as #1288 . The array size issue I mention there is my interpretation of jax's oblivious error message, which seems to have been a useful interpretation because ensuring constants contains only 1d arrays, in particular no 2d or tuples, avoided one way the issue pops up.

@dpanici
Copy link
Collaborator

dpanici commented Dec 4, 2024

# this has NO error
import desc.examples
from desc.objectives import ObjectiveFunction, PlasmaVesselDistance
from desc.grid import LinearGrid
from desc.optimize import Optimizer
eq = desc.examples.get("HELIOTRON")

constraints = ()  
# circular surface
a = 0.5
R0 = 10
surf = eq.surface.copy()
surf.change_resolution(M=1, N=1)

grid = LinearGrid(M=eq.M , N=eq.N, NFP=eq.NFP)
obj = PlasmaVesselDistance(
    surface=surf,
    eq=eq,
    target=0.25,
    surface_grid=grid,
    plasma_grid=grid,
    use_signed_distance=True,surface_fixed=True
)
objective = ObjectiveFunction((obj,))

optimizer = Optimizer("lsq-exact")
(eq, ), _ = optimizer.optimize(
    (eq, ), objective, constraints, verbose=0, maxiter=2, ftol=0, xtol=1e-9
)

eq = eq.copy()
obj = PlasmaVesselDistance(
    surface=surf,
    eq=eq,
    target=0.25,
    surface_grid=grid,
    plasma_grid=grid,
    use_signed_distance=True,surface_fixed=True
)
objective = ObjectiveFunction((obj,))
(eq, ), _ = optimizer.optimize(
    (eq, ), objective, constraints, verbose=0, maxiter=2, ftol=0, xtol=1e-9
)


print(objective.objectives[0].constants["surface_coords"].shape)

The above snippet outputs this (no error) on master (and on ripple too):

DESC version 0.12.3+1138.g533793e27,using JAX backend, jax version=0.4.31, jaxlib version=0.4.30, dtype=float64
Using device: CPU, with 4.63 GB available memory
(175, 3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add warning Adds warning to prevent/detect bug documentation Add documentation or better warnings etc. P2 Medium Priority, not urgent but should be on the near-term agend
Projects
None yet
Development

No branches or pull requests

2 participants