Skip to content

Setting kinetic profiles does not remove pressure profile, and this leads to errors #1995

@dpanici

Description

@dpanici

If I say assign kinetic profiles to an eq that already has a pressure profile, we don't remove the pressure profile currently. This creates errors later if used in optimizations involving quantities reliant on the pressure normalization bc (I think)

  • the normalization scales for the FixKinetic, in its build call to compute_scale_factors, see it has a pressure profile and so does not assign the kinetic normalizations
  • then immediately after it attempts to grab the kinetic normalization scale, however it does not exist and so an error is thrown

We could either

  • always compute both p scale and kinetic scale factors if those profiles are present
  • or remove the pressure profile when kinetic profiles are assigned. I vaguely recall that we said we wanted to allow this for whatever reason, but I don't remember why.

example code:

from desc.examples import get
from desc.profiles import PowerSeriesProfile

eq = get("SOLOVEV")
eq.electron_density=PowerSeriesProfile()
eq.electron_temperature=PowerSeriesProfile()
eq.ion_temperature=PowerSeriesProfile()
eq.atomic_number=PowerSeriesProfile()
# eq.pressure=None # no error if we uncomment this line

eq.solve()

yields error

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[2], line 10
      7 eq.ion_temperature=PowerSeriesProfile()
      8 eq.atomic_number=PowerSeriesProfile()
---> 10 eq.solve()

File [~/Research/DESC/desc/equilibrium/equilibrium.py:2280](http://localhost:8888/lab/tree/~/Research/DESC/desc/equilibrium/equilibrium.py#line=2279), in Equilibrium.solve(self, objective, constraints, optimizer, ftol, xtol, gtol, maxiter, x_scale, options, verbose, copy)
   2265 warnif(
   2266     self.N > self.N_grid or self.M > self.M_grid or self.L > self.L_grid,
   2267     msg="Equilibrium has one or more spectral resolutions "
   (...)   2271     + "to avoid this warning.",
   2272 )
   2273 errorif(
   2274     self.bdry_mode == "poincare",
   2275     NotImplementedError,
   2276     "Solving equilibrium with poincare XS as BC is not supported yet "
   2277     + "on master branch.",
   2278 )
-> 2280 things, result = optimizer.optimize(
   2281     self,
   2282     objective,
   2283     constraints,
   2284     ftol=ftol,
   2285     xtol=xtol,
   2286     gtol=gtol,
   2287     x_scale=x_scale,
   2288     verbose=verbose,
   2289     maxiter=maxiter,
   2290     options=options,
   2291     copy=copy,
   2292 )
   2294 return things[0], result

File [~/Research/DESC/desc/optimize/optimizer.py:222](http://localhost:8888/lab/tree/~/Research/DESC/desc/optimize/optimizer.py#line=221), in Optimizer.optimize(self, things, objective, constraints, ftol, xtol, gtol, ctol, x_scale, verbose, maxiter, options, copy)
    219 timer.start("Initializing the optimization")
    220 if not is_linear_proj:
    221     objective, nonlinear_constraint, x_scale = (
--> 222         get_combined_constraint_objectives(
    223             eq,
    224             constraints,
    225             objective,
    226             things,
    227             x_scale,
    228             self.method,
    229             method,
    230             verbose,
    231             options,
    232         )
    233     )
    234 else:
    235     nonlinear_constraint = None

File [~/Research/DESC/desc/optimize/optimizer.py:518](http://localhost:8888/lab/tree/~/Research/DESC/desc/optimize/optimizer.py#line=517), in get_combined_constraint_objectives(eq, constraints, objective, things, x_scale, opt_method, method, verbose, options)
    516     objective.build(verbose=verbose)
    517 if linear_constraint is not None and not linear_constraint.built:
--> 518     linear_constraint.build(verbose=verbose)
    519 if nonlinear_constraint is not None and not nonlinear_constraint.built:
    520     nonlinear_constraint.build(verbose=verbose)

File [~/Research/DESC/desc/backend.py:146](http://localhost:8888/lab/tree/~/Research/DESC/desc/backend.py#line=145), in execute_on_cpu.<locals>.wrapper(*args, **kwargs)
    143 @functools.wraps(func)
    144 def wrapper(*args, **kwargs):
    145     with jax.default_device(jax.devices("cpu")[0]):
--> 146         return func(*args, **kwargs)

File [~/Research/DESC/desc/objectives/objective_funs.py:339](http://localhost:8888/lab/tree/~/Research/DESC/desc/objectives/objective_funs.py#line=338), in ObjectiveFunction.build(self, use_jit, verbose)
    337         if verbose > 0:
    338             print("Building objective: " + objective.name)
--> 339         objective.build(use_jit=self.use_jit, verbose=verbose)
    340     self._dim_f += objective.dim_f
    341 if self._dim_f == 1:

File [~/Research/DESC/desc/objectives/linear_objectives.py:2233](http://localhost:8888/lab/tree/~/Research/DESC/desc/objectives/linear_objectives.py#line=2232), in FixElectronDensity.build(self, use_jit, verbose)
   2231 if self._normalize:
   2232     scales = compute_scaling_factors(eq)
-> 2233     self._normalization = scales["n"]
   2234 super().build(use_jit=use_jit, verbose=verbose)

KeyError: 'n'

Metadata

Metadata

Assignees

Labels

P3Highest Priority, someone is/should be actively working on this

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions