Fixing all Examples#42
Conversation
rogeriojorge
left a comment
There was a problem hiding this comment.
A couple of changes asked for
| @patch('essos.objective_functions.BiotSavart', return_value=DummyField()) | ||
| @patch('essos.objective_functions.perturb_curves', side_effect=lambda curves, sampler, key=None, perturbation_type=None: curves) | ||
| def test_perturbed_field_and_coils_from_dofs(self, pc, bs, coils, curves): | ||
| @patch('essos.objective_functions.perturb_curves_systematic') |
There was a problem hiding this comment.
Does this perturb_curves_systematic exist? I'm also checking in the objective_functions.py and it appears in line 206, but it is never imported. Doesn't this error out for you?
The same for perturb_curves_statistic, pertubred_field_from_dofs, field_from_dofs, and coils_from_dofs, as the current objective_functions.py only imports perturb_curves and has moved toward object-based losses
| class PytreeCoils: | ||
| def __init__(self, gamma, gamma_dash, gamma_dashdash, currents, quadpoints): | ||
| self.gamma = gamma | ||
| self.gamma_dash = gamma_dash | ||
| self.gamma_dashdash = gamma_dashdash | ||
| self.currents = currents | ||
| self.quadpoints = quadpoints | ||
|
|
||
| def __len__(self): | ||
| return self.gamma.shape[0] | ||
|
|
||
| def tree_flatten(self): | ||
| children = (self.gamma, self.gamma_dash, self.gamma_dashdash, self.currents, self.quadpoints) | ||
| return children, None | ||
|
|
||
| @classmethod | ||
| def tree_unflatten(cls, aux_data, children): | ||
| return cls(*children) | ||
|
|
There was a problem hiding this comment.
Doesn't this error out for you? Seems like PytreeCoils is missing attributes used by the tested losses. loss_coil_surface_distance() needs coils.stellsym and coils.nfp; loss_linkingnumber() and loss_lorentz_force_coils() need coils.curves.quadpoints. What about using a real Coils object in these tests or add a small .curves.quadpoints, .stellsym, and .nfp to the dummy object?
|
|
||
| # Load coils and field | ||
| json_file = os.path.join(os.path.dirname(__file__), '../input_files', 'ESSOS_biot_savart_LandremanPaulQA.json') | ||
| json_file = os.path.join(os.path.dirname(__name__), 'input_files', 'ESSOS_biot_savart_LandremanPaulQA.json') |
There was a problem hiding this comment.
Didn't we see that this kind of change breaks for other users? Do you really need it?
|
|
||
| # Load coils and field | ||
| wout_file = os.path.join(os.path.dirname(__file__), "../input_files", "wout_LandremanPaul2021_QA_reactorScale_lowres.nc") | ||
| wout_file = os.path.join(os.path.dirname(__name__), "input_files", "wout_LandremanPaul2021_QA_reactorScale_lowres.nc") |
|
|
||
| #Load electric field | ||
| Er_file=os.path.join(os.path.dirname(__file__), '../input_files','Er.h5') | ||
| Er_file=os.path.join(os.path.dirname(__name__), 'input_files','Er.h5') |
| R_axis=field.r_axis | ||
| Z_axis=field.z_axis | ||
| #Ideally here one would differentiate in time through diffrax !TODO | ||
| r_cross=jnp.sqrt(jnp.square(jnp.sqrt(jnp.square(xyz[:,0])+jnp.square(xyz[:,1]))-R_axis+1.e-12)+jnp.square(xyz[:,2]-Z_axis+1.e-12)) |
There was a problem hiding this comment.
I thought xyz is (nparticles, ntimes, 3). Shouldn't this be xyz[:,:,0]?
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| MAXTIME_TRACING = 1e-4 | ||
| NUMBER_COILS_PER_HALF_FIELD_PERIOD = 3 | ||
| NUMBER_OF_FIELD_PERIODS = 2 | ||
| MODEL = 'GuidingCenterAdaptative' |
There was a problem hiding this comment.
Is this used later? Seems like it calls particles.to_full_orbit(field) inside the loss. What about importing/using the existing loss_particle_radial_drift() from objective_functions.py instead of redefining it locally?
There was a problem hiding this comment.
I kept the exmaples with independent functions on purpose, for people to see what the example is doing more clearly wihtout having to do to the code @rogeriojorge .
| coils = copy_coils_from_field(field) | ||
| base_key = jax.random.key(key) | ||
| split_keys = jax.random.split(base_key, 2) | ||
| perturb_curves_systematic(coils, sampler, key=split_keys[0]) |
There was a problem hiding this comment.
This is not assigned anywhere? so systematic perturbations are ignored? Should it be coils = perturb_curves_systematic(...) before applying the statistical perturbations?
…Tree traversal, fixing 'None is not a valid value for jnp.array' crash in tracing (from_simsopt and from_json paths)
Guard Coils.dofs_currents against None/bool sentinels (fixes tracing crash on #42)
|
The only example not working is with from_simsopt method. but I dopnt have a solution for this yet. I would merge this and solve that after. The change fo PR #43 works also on older diffrax version. @rogeriojorge @EstevaoMGomes |
rogeriojorge
left a comment
There was a problem hiding this comment.
A couple of small things, and then it's ready to merge
| @patch("essos.objective_functions.perturb_curves_systematic", create=True) | ||
| @patch("essos.objective_functions.perturb_curves_statistic", create=True) |
There was a problem hiding this comment.
I think this is masking the fact that in objective_functions perturb_curves_systematic and perturb_curves_statistic are not imported but still used, which leads to error. Why not use the API already imported there, in objective_functions?
coils = perturb_curves(
coils,
sampler,
key=split_keys[0],
perturbation_type="systematic",
)
coils = perturb_curves(
coils,
sampler,
key=split_keys[1],
perturbation_type="statistical",
)
| coils = perturb_curves_systematic(coils, sampler, key=split_keys[0]) | ||
| coils = perturb_curves_statistic(coils, sampler, key=split_keys[1]) |
There was a problem hiding this comment.
See previous comment:
coils = perturb_curves(
coils,
sampler,
key=split_keys[0],
perturbation_type="systematic",
)
coils = perturb_curves(
coils,
sampler,
key=split_keys[1],
perturbation_type="statistical",
)
| R_axis=field.r_axis | ||
| Z_axis=field.z_axis | ||
| #Ideally here one would differentiate in time through diffrax !TODO | ||
| r_cross=jnp.sqrt(jnp.square(jnp.sqrt(jnp.square(xyz[:,0])+jnp.square(xyz[:,1]))-R_axis+1.e-12)+jnp.square(xyz[:,2]-Z_axis+1.e-12)) |
| ax3.plot(tracing.times, trajectory[:, 3]*SPEED_OF_LIGHT/jnp.sqrt(tracing.energy[i]/mass*2.), label=f'Particle {i+1}') | ||
| for i in subset_indices: | ||
| trajectory = trajectories[i] | ||
| ax1.plot(trajectory[:,0], trajectory[:,1], trajectory[:,2], label=f'Particle {i+1}') |
There was a problem hiding this comment.
I think for this to work you need
ax1 = fig.add_subplot(221, projection="3d")
There was a problem hiding this comment.
The same in the other collision examples
This is a pull request to clean examples and make them work with PR #36 to PR #40. Due to the new changed requesting during pull request some of them stopped working. So I had to add some changes to coils and surface classes to make them work again. There is however, still a problem with examples using stochastic optimization and the classifier object.