Closed jacquelinegarrahan closed 2 years ago
Hi Jacqueline,
If I understand correctly, you have a NumPy array representing rho(x,y) the 2d distribution for particles, not a file?
There is some support for that already, please see distgen/examples/example_dists.ipynb -> 2d image.
Here I load an image using a distgen helper function. That should create the 2d NumPy array needed (the other objects returned there aren't used). Then the xy_distribution is defined in the dictionary input:
'xy_dist':{ 'type' : 'image2d', 'min_x': {'value':-2.0, 'units':'mm'}, 'max_x': {'value':+2.0, 'units':'mm'}, 'min_y': {'value':-2.0, 'units':'mm'}, 'max_y': {'value':+2.0, 'units':'mm'}, 'P':Pxy}
The min{x,y} and max{x,y} define the length scale corresponding to the data in Pxy. With that input, create a Generator object and print it:
The yaml conversation isn't so pretty for the NumPy array, but basically I believe what you need is stored in gen['xy_dist:P'] (short form for gen['xy_dist]['P']), and you should be able to update that dict item as you see fit.
Can you try that?
I've also noticed that if file isn't defined in the yaml :
xy_dist:
type: file2d
And subsequently assigned using a key:
G["xy_dist:file"]
The following error occurs during run:
Traceback (most recent call last):
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/prefect/engine/task_runner.py", line 876, in get_task_run_state
value = prefect.utilities.executors.run_task_with_timeout(
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/prefect/utilities/executors.py", line 467, in run_task_with_timeout
return task.run(*args, **kwargs) # type: ignore
File "/Users/jgarra/sandbox/distgen-impact-cu-inj-ex/distgen_impact_cu_inj_ex/flow/flow2.py", line 72, in run_distgen
output_variables = distgen_model.evaluate(distgen_input_variables)
File "/Users/jgarra/sandbox/distgen-impact-cu-inj-ex/distgen_impact_cu_inj_ex/model.py", line 76, in evaluate
self._G.run()
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/generator.py", line 412, in run
beam = self.beam()
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/generator.py", line 315, in beam
dist = get_dist('xy', dist_params['xy'], verbose=verbose)
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/dist.py", line 106, in get_dist
dist = File2d("x","y",verbose=verbose, **params)
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/dist.py", line 2375, in __init__
self.check_inputs(params)
File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/dist.py", line 139, in check_inputs
assert req in params, f'Required input parameter {req} to {self.__class__.__name__}.__init__(**kwargs) was not found.'
AssertionError: Required input parameter file to File2d.__init__(**kwargs) was not found.
This doesn't occur if a dummy file is assigned in the yaml.
@ColwynGulliford Thanks for your message- that structure is exactly what I was looking for. I'll test it out now
No problem, I was just looking a little further and it looks like the Pxy object is a Pint Quantity (NumPy array + units):
If so, let me see how best to set it...
Ok, I believe that it should work as you require. Here's a distribution I made from a file:
Now instead of the file I used image2d as described above, that is I load the distribution first to a NumPy array and then create the Generator. I then manipulate the NumPy array, and stretch the x scale by setting
gen['xy_dist:min_x:value'], gen['xy_dist:max_x:value'], as well as setting gen['xy_dist:P']:
So I think it works as needed - nice to have tried this, I hadn't used it quite that way before!
Thanks for your help @ColwynGulliford ! This works well for my application :)
Glad to hear!
Hi Colwyn,
I'm wondering if there is a way to directly assign a numpy array as the xy distribution on the Generator object rather than loading from a file. I see this
image2d
dytpe forget_dist
here. Am I able to assign this directly using a key?Thanks!