-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4878 from BrianMarre/topic-customInputToPyPIConGPU
custom user input in PyPIConGPU
- Loading branch information
Showing
12 changed files
with
344 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
This file is part of the PIConGPU. | ||
Copyright 2024 PIConGPU contributors | ||
Authors: Brian Edward Marre | ||
License: GPLv3+ | ||
""" | ||
|
||
from .rendering import RenderedObject | ||
|
||
import typeguard | ||
import typing | ||
|
||
|
||
class CustomUserInput(RenderedObject): | ||
""" | ||
container for easy passing of additional input as dict from user script to rendering context of simulation input | ||
if additional | ||
""" | ||
|
||
tags: typing.Optional[list[str]] = None | ||
""" | ||
list of tags | ||
""" | ||
|
||
rendering_context: typing.Optional[dict[str, typing.Any]] = None | ||
""" | ||
accumulation variable of added dictionaries | ||
""" | ||
|
||
def __checkDoesNotChangeExistingKeyValues(self, firstDict, secondDict): | ||
for key in firstDict.keys(): | ||
if (key in secondDict) and (firstDict[key] != secondDict[key]): | ||
raise ValueError("Key " + str(key) + " exist already, and specified values differ.") | ||
|
||
@typeguard.typechecked | ||
def addToCustomInput(self, custom_input: dict[str, typing.Any], tag: str): | ||
""" | ||
append dictionary to custom input dictionary | ||
""" | ||
if tag == "": | ||
raise ValueError("tag must not be empty string!") | ||
if not custom_input: | ||
raise ValueError("custom input must contain at least 1 key") | ||
|
||
if (self.tags is None) and (self.rendering_context is None): | ||
self.tags = [tag] | ||
self.rendering_context = custom_input | ||
else: | ||
self.__checkDoesNotChangeExistingKeyValues(self.rendering_context, custom_input) | ||
|
||
if tag in self.tags: | ||
raise ValueError("duplicate tag!") | ||
|
||
self.rendering_context.update(custom_input) | ||
self.tags.append(tag) | ||
|
||
def get_tags(self) -> list[str]: | ||
return self.tags | ||
|
||
def _get_serialized(self) -> dict: | ||
return self.rendering_context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
share/picongpu/pypicongpu/schema/customrenderingcontext.CustomRenderingContext.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"$id": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.customrenderingcontext.CustomRenderingContext", | ||
"description": "additional input provided by user directly to the PyPIConGPU simulation object for use in custom templates", | ||
"type": "object", | ||
"properties": { | ||
"tags":{ | ||
"description": "list of unique strings identifying the version/content of the custom input", | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"minItems": 1 | ||
} | ||
}, | ||
"required": [ | ||
"tags" | ||
], | ||
"unevaluatedProperties": true | ||
} |
6 changes: 6 additions & 0 deletions
6
share/picongpu/pypicongpu/schema/customuserinput.CustomUserInput.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$id": "https://registry.hzdr.de/crp/picongpu/schema/picongpu.pypicongpu.customuserinput.CustomUserInput", | ||
"description": "container class for passing dictionaries from the user script to the PyPIConGPU renderer", | ||
"type": "object", | ||
"unevaluatedProperties": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
This file is part of the PIConGPU. | ||
Copyright 2024 PIConGPU contributors | ||
Authors: Brian Edward Marre | ||
License: GPLv3+ | ||
""" | ||
|
||
from picongpu.pypicongpu import customuserinput | ||
|
||
import unittest | ||
|
||
|
||
class TestCustomUserInput(unittest.TestCase): | ||
# test standard workflow is possible and data+tag is passed on | ||
def test_standard_case_works(self): | ||
c = customuserinput.CustomUserInput() | ||
data1 = {"test_data_1": 1} | ||
data2 = {"test_data_2": 2} | ||
|
||
tag1 = "tag_1" | ||
tag2 = "tag_2" | ||
|
||
c.addToCustomInput(data1, tag1) | ||
c.addToCustomInput(data2, tag2) | ||
|
||
rendering_context = c.get_rendering_context() | ||
|
||
self.assertEqual(rendering_context["test_data_1"], 1) | ||
self.assertEqual(rendering_context["test_data_2"], 2) | ||
|
||
tags = c.get_tags() | ||
self.assertIn(tag1, tags) | ||
self.assertIn(tag2, tags) | ||
|
||
def test_wrong_tags(self): | ||
c = customuserinput.CustomUserInput() | ||
|
||
data1 = {"test_data_1": 1} | ||
data2 = {"test_data_2": 2} | ||
|
||
tag1_1 = "tag_1" | ||
tag1_2 = "tag_1" | ||
|
||
# first add must succeed | ||
c.addToCustomInput(data1, tag1_1) | ||
with self.assertRaisesRegex(ValueError, "duplicate tag!"): | ||
c.addToCustomInput(data2, tag1_2) | ||
|
||
with self.assertRaisesRegex(ValueError, "tag must not be empty"): | ||
c.addToCustomInput(data2, "") | ||
|
||
def test_wrong_custom_input(self): | ||
c = customuserinput.CustomUserInput() | ||
|
||
data1_1 = {"test_data_1": 1} | ||
data1_2 = {"test_data_1": 2} | ||
empty_data = {} | ||
|
||
tag1 = "tag_1" | ||
tag2 = "tag_2" | ||
|
||
with self.assertRaisesRegex(ValueError, "custom input must contain at least 1 key"): | ||
c.addToCustomInput(empty_data, tag1) | ||
|
||
c.addToCustomInput(data1_1, tag1) | ||
with self.assertRaisesRegex(ValueError, "Key test_data_1 exist already, and specified values differ."): | ||
c.addToCustomInput(data1_2, tag2) | ||
|
||
# test same key with same value is allowed | ||
c.addToCustomInput(data1_1, tag2) |
Oops, something went wrong.