From 3cdb53c1e521b449e7547c6d1ccd654bfef9f191 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Mon, 29 Nov 2021 20:59:00 +0100 Subject: [PATCH 1/2] Added method to remove unused materials/elements/isotopes from gdml (https://github.com/mipt-npm/gdml.kt/issues/41) --- gdml-source/babyIAXO.gdml | 4923 +++++++++++++---- .../gdml/GdmlMaterialPostProcessor.kt | 72 + .../kotlin/space/kscience/gdml/gdmlIO.kt | 5 +- .../kotlin/space/kscience/gdml/materials.kt | 2 +- .../kscience/gdml/PredefinedGeometryTest.kt | 19 +- 5 files changed, 4005 insertions(+), 1016 deletions(-) create mode 100644 src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt diff --git a/gdml-source/babyIAXO.gdml b/gdml-source/babyIAXO.gdml index 35fd1d7..e7ebae9 100644 --- a/gdml-source/babyIAXO.gdml +++ b/gdml-source/babyIAXO.gdml @@ -1,1065 +1,3964 @@ - - - -]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - - - - + + + + + + + + + + - - - - - - + + + + + + - - - - - + + + + + - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - + + + + - - - - - - - - - - - - - - - - - - + + + + - - - - - - - - + + + + + + + + + + - - - - - + + + + - - - - - - - - - - - + + + + - - - - - + + + + - - - - - + + + + - - - - - - + + + + - - - - - + + + + + + - - - - - + + + + + + + - - - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + - - - - + + + + - - + + + + + + + + - - - - - - - - - - - - - - - - - + + + - - - - + + + - - - - + + + - - - - - - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + - - - - - - + + + + + + + + + + + + - - - - + + + + + + + + + - - - - + + + - - - - - - + + + - - - - - - + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + - - - - + + + - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + - - - \ No newline at end of file + diff --git a/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt b/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt new file mode 100644 index 0000000..4517a05 --- /dev/null +++ b/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt @@ -0,0 +1,72 @@ +package space.kscience.gdml + +public class GdmlMaterialPostProcessor { + public companion object { + public fun removeUnusedMaterials(gdml: Gdml): Gdml { + + val materialsInUse: MutableSet = mutableSetOf() + val elementsInUse: MutableSet = mutableSetOf() + val isotopesInUse: MutableSet = mutableSetOf() + + fun addMaterialsRecursively(composite: GdmlComposite?) { + // composite may be composed of other composites + composite?.fractions?.forEach { fraction -> + val materialComponent = gdml.materials.get(fraction.ref) + if (materialComponent != null) { + materialsInUse.add(materialComponent) + addMaterialsRecursively(materialComponent) + } else { + val elementComponent = gdml.materials.get(fraction.ref) + if (elementComponent != null) { + elementsInUse.add(elementComponent) + elementComponent.fractions.forEach { elementFraction -> + val isotopeComponent = gdml.materials.get(elementFraction.ref) + isotopesInUse.add(isotopeComponent!!) // element must be mode of isotopes + } + } else { + val isotopeComponent = gdml.materials.get(fraction.ref) + if (isotopeComponent != null) { + isotopesInUse.add(isotopeComponent) + } + } + } + } + } + + gdml.structure.content.forEach { + val volume = gdml.structure.get(it.name) + if (volume != null) { + val material = volume.materialref.resolve(gdml)!! + materialsInUse.add(material) + val composite = gdml.materials.get(material.name) + addMaterialsRecursively(composite) + } + } + + + // materialsInUse now contains all materials in use + /* + println(" - ISOTOPES:") + isotopesInUse.forEach { println(it) } + println(" - ELEMENTS:") + elementsInUse.forEach { println(it) } + println(" - MATERIALS:") + materialsInUse.forEach { println(it) } + */ + + gdml.materials.content.clear() + // Refill materials only with used ones + isotopesInUse.forEach { + gdml.materials.content.add(it) + } + elementsInUse.forEach { + gdml.materials.content.add(it) + } + materialsInUse.forEach { + gdml.materials.content.add(it) + } + + return gdml + } + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/space/kscience/gdml/gdmlIO.kt b/src/commonMain/kotlin/space/kscience/gdml/gdmlIO.kt index 193a193..ed8d73d 100644 --- a/src/commonMain/kotlin/space/kscience/gdml/gdmlIO.kt +++ b/src/commonMain/kotlin/space/kscience/gdml/gdmlIO.kt @@ -125,7 +125,7 @@ internal val gdmlFormat: XML = XML(gdmlModule) { */ public fun Gdml.Companion.decodeFromString(string: String, usePreprocessor: Boolean = false): Gdml = if (usePreprocessor) { - val preprocessor = GdmlPreprocessor( XmlStreaming.newReader(string)) { parseAndEvaluate(it) } + val preprocessor = GdmlPreprocessor(XmlStreaming.newReader(string)) { parseAndEvaluate(it) } gdmlFormat.decodeFromReader(serializer(), preprocessor) } else { gdmlFormat.decodeFromString(serializer(), string) @@ -153,7 +153,8 @@ public fun Gdml.Companion.encodeToWriter(gdml: Gdml, writer: XmlWriter): Unit = */ public fun Gdml.Companion.encodeToString(gdml: Gdml): String { val stringWriter = StringWriter() - val xmlWriter = XmlStreaming.newWriter(stringWriter, gdmlFormat.config.repairNamespaces, gdmlFormat.config.xmlDeclMode) + val xmlWriter = + XmlStreaming.newWriter(stringWriter, gdmlFormat.config.repairNamespaces, gdmlFormat.config.xmlDeclMode) var ex: Throwable? = null try { diff --git a/src/commonMain/kotlin/space/kscience/gdml/materials.kt b/src/commonMain/kotlin/space/kscience/gdml/materials.kt index 1ee19d4..4ffc35c 100644 --- a/src/commonMain/kotlin/space/kscience/gdml/materials.kt +++ b/src/commonMain/kotlin/space/kscience/gdml/materials.kt @@ -92,4 +92,4 @@ public data class GdmlComposite(override var name: String) : GdmlMaterial() { fractions.add(GdmlFraction(n, ref.ref)) } -} \ No newline at end of file +} diff --git a/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt b/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt index bbaf03c..d46b13f 100644 --- a/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt +++ b/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt @@ -3,6 +3,9 @@ package space.kscience.gdml import org.junit.jupiter.api.Test import java.io.File import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals class PredefinedGeometryTest { @@ -54,7 +57,21 @@ class PredefinedGeometryTest { @Test fun readIAXO() { val file = File("gdml-source/babyIAXO.gdml") - val gdml = Gdml.decodeFromStream(file.inputStream(),true) + val gdml = Gdml.decodeFromStream(file.inputStream(), true) println(gdml.world) } + + @Test + fun testRemoveUnusedMaterials() { + val file = File("gdml-source/babyIAXO.gdml") + var gdml = Gdml.decodeFromStream(file.inputStream(), true) + assertNotNull(gdml.materials.get("G4_WATER")) + val gdmlAfter = GdmlMaterialPostProcessor.removeUnusedMaterials(gdml) + // assertNull(gdmlAfter.materials.get("G4_WATER")) TODO: shouldn't this be null? its not on the gdml + // assertNotEquals(gdmlAfter, gdml) TODO: not sure why this doesn't work either + val gdmlAfterAgain = GdmlMaterialPostProcessor.removeUnusedMaterials(gdmlAfter) + assertEquals(gdmlAfter, gdmlAfterAgain) + + println(gdml) + } } \ No newline at end of file From 8d031b60b816d3f820be705ea746add0dbe88d1b Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Thu, 2 Dec 2021 17:08:52 +0100 Subject: [PATCH 2/2] Implemented changes requested in https://github.com/mipt-npm/gdml.kt/pull/42 --- .../kotlin/space/kscience/gdml/Gdml.kt | 2 +- .../gdml/GdmlMaterialPostProcessor.kt | 116 +++++++++--------- .../kscience/gdml/PredefinedGeometryTest.kt | 17 ++- 3 files changed, 65 insertions(+), 70 deletions(-) diff --git a/src/commonMain/kotlin/space/kscience/gdml/Gdml.kt b/src/commonMain/kotlin/space/kscience/gdml/Gdml.kt index dc28a97..6d1cc62 100644 --- a/src/commonMain/kotlin/space/kscience/gdml/Gdml.kt +++ b/src/commonMain/kotlin/space/kscience/gdml/Gdml.kt @@ -111,7 +111,7 @@ public class Gdml : GdmlRegistry { public companion object } -public inline fun Gdml(block: Gdml.() -> Unit): Gdml = Gdml().apply(block) +public inline fun Gdml(block: Gdml.() -> Unit): Gdml = Gdml().apply(block) public inline fun GdmlRef.resolve(root: Gdml): T? = root.getDefine(ref) public inline fun GdmlRef.resolve(root: Gdml): T? = root.getSolid(ref) diff --git a/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt b/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt index 4517a05..a7a784d 100644 --- a/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt +++ b/src/commonMain/kotlin/space/kscience/gdml/GdmlMaterialPostProcessor.kt @@ -1,72 +1,68 @@ package space.kscience.gdml -public class GdmlMaterialPostProcessor { - public companion object { - public fun removeUnusedMaterials(gdml: Gdml): Gdml { +public fun Gdml.removeUnusedMaterials(): Gdml { - val materialsInUse: MutableSet = mutableSetOf() - val elementsInUse: MutableSet = mutableSetOf() - val isotopesInUse: MutableSet = mutableSetOf() + val materialsInUse: MutableSet = mutableSetOf() + val elementsInUse: MutableSet = mutableSetOf() + val isotopesInUse: MutableSet = mutableSetOf() - fun addMaterialsRecursively(composite: GdmlComposite?) { - // composite may be composed of other composites - composite?.fractions?.forEach { fraction -> - val materialComponent = gdml.materials.get(fraction.ref) - if (materialComponent != null) { - materialsInUse.add(materialComponent) - addMaterialsRecursively(materialComponent) - } else { - val elementComponent = gdml.materials.get(fraction.ref) - if (elementComponent != null) { - elementsInUse.add(elementComponent) - elementComponent.fractions.forEach { elementFraction -> - val isotopeComponent = gdml.materials.get(elementFraction.ref) - isotopesInUse.add(isotopeComponent!!) // element must be mode of isotopes - } - } else { - val isotopeComponent = gdml.materials.get(fraction.ref) - if (isotopeComponent != null) { - isotopesInUse.add(isotopeComponent) - } - } + fun addMaterialsRecursively(composite: GdmlComposite?) { + // composite may be composed of other composites + composite?.fractions?.forEach { fraction -> + val materialComponent = this.materials.get(fraction.ref) + if (materialComponent != null) { + materialsInUse.add(materialComponent) + addMaterialsRecursively(materialComponent) + } else { + val elementComponent = this.materials.get(fraction.ref) + if (elementComponent != null) { + elementsInUse.add(elementComponent) + elementComponent.fractions.forEach { elementFraction -> + val isotopeComponent = this.materials.get(elementFraction.ref) + isotopesInUse.add(isotopeComponent!!) // element must be mode of isotopes + } + } else { + val isotopeComponent = this.materials.get(fraction.ref) + if (isotopeComponent != null) { + isotopesInUse.add(isotopeComponent) } } } + } + } - gdml.structure.content.forEach { - val volume = gdml.structure.get(it.name) - if (volume != null) { - val material = volume.materialref.resolve(gdml)!! - materialsInUse.add(material) - val composite = gdml.materials.get(material.name) - addMaterialsRecursively(composite) - } - } + this.structure.content.forEach { + val volume = this.structure.get(it.name) + if (volume != null) { + val material = volume.materialref.resolve(this)!! + materialsInUse.add(material) + val composite = this.materials.get(material.name) + addMaterialsRecursively(composite) + } + } - // materialsInUse now contains all materials in use - /* - println(" - ISOTOPES:") - isotopesInUse.forEach { println(it) } - println(" - ELEMENTS:") - elementsInUse.forEach { println(it) } - println(" - MATERIALS:") - materialsInUse.forEach { println(it) } - */ + // materialsInUse now contains all materials in use + /* + println(" - ISOTOPES:") + isotopesInUse.forEach { println(it) } + println(" - ELEMENTS:") + elementsInUse.forEach { println(it) } + println(" - MATERIALS:") + materialsInUse.forEach { println(it) } + */ - gdml.materials.content.clear() - // Refill materials only with used ones - isotopesInUse.forEach { - gdml.materials.content.add(it) - } - elementsInUse.forEach { - gdml.materials.content.add(it) - } - materialsInUse.forEach { - gdml.materials.content.add(it) - } - - return gdml - } + this.materials.content.clear() + // Refill materials only with used ones + isotopesInUse.forEach { + this.materials.content.add(it) } -} \ No newline at end of file + elementsInUse.forEach { + this.materials.content.add(it) + } + materialsInUse.forEach { + this.materials.content.add(it) + } + + return this +} diff --git a/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt b/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt index d46b13f..e6de40b 100644 --- a/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt +++ b/src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt @@ -3,9 +3,7 @@ package space.kscience.gdml import org.junit.jupiter.api.Test import java.io.File import kotlin.test.assertNotNull -import kotlin.test.assertNull import kotlin.test.assertEquals -import kotlin.test.assertNotEquals class PredefinedGeometryTest { @@ -64,13 +62,14 @@ class PredefinedGeometryTest { @Test fun testRemoveUnusedMaterials() { val file = File("gdml-source/babyIAXO.gdml") - var gdml = Gdml.decodeFromStream(file.inputStream(), true) - assertNotNull(gdml.materials.get("G4_WATER")) - val gdmlAfter = GdmlMaterialPostProcessor.removeUnusedMaterials(gdml) - // assertNull(gdmlAfter.materials.get("G4_WATER")) TODO: shouldn't this be null? its not on the gdml - // assertNotEquals(gdmlAfter, gdml) TODO: not sure why this doesn't work either - val gdmlAfterAgain = GdmlMaterialPostProcessor.removeUnusedMaterials(gdmlAfter) - assertEquals(gdmlAfter, gdmlAfterAgain) + val gdml = Gdml.decodeFromStream(file.inputStream(), true) + val materialToRemove = gdml.materials.get("G4_WATER") + assertNotNull(materialToRemove) + assert(gdml.materials.content.contains(materialToRemove)) + val gdmlMaterialsRemoved = gdml.removeUnusedMaterials() + assert(!gdmlMaterialsRemoved.materials.content.contains(materialToRemove)) + val gdmlMaterialsRemovedAgain = gdmlMaterialsRemoved.removeUnusedMaterials() + assertEquals(gdmlMaterialsRemoved, gdmlMaterialsRemovedAgain) println(gdml) }