Skip to content

Commit

Permalink
Merge pull request #42 from lobis/dev
Browse files Browse the repository at this point in the history
Added method to remove unused materials / elements / isotopes
altavir authored Dec 2, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents b57110b + 8d031b6 commit bfd113f
Showing 6 changed files with 4,001 additions and 1,017 deletions.
4,923 changes: 3,911 additions & 1,012 deletions gdml-source/babyIAXO.gdml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/commonMain/kotlin/space/kscience/gdml/Gdml.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : GdmlDefine> GdmlRef<T>.resolve(root: Gdml): T? = root.getDefine(ref)
public inline fun <reified T : GdmlSolid> GdmlRef<T>.resolve(root: Gdml): T? = root.getSolid(ref)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package space.kscience.gdml

public fun Gdml.removeUnusedMaterials(): Gdml {

val materialsInUse: MutableSet<GdmlMaterial> = mutableSetOf()
val elementsInUse: MutableSet<GdmlElement> = mutableSetOf()
val isotopesInUse: MutableSet<GdmlIsotope> = mutableSetOf()

fun addMaterialsRecursively(composite: GdmlComposite?) {
// composite may be composed of other composites
composite?.fractions?.forEach { fraction ->
val materialComponent = this.materials.get<GdmlComposite>(fraction.ref)
if (materialComponent != null) {
materialsInUse.add(materialComponent)
addMaterialsRecursively(materialComponent)
} else {
val elementComponent = this.materials.get<GdmlElement>(fraction.ref)
if (elementComponent != null) {
elementsInUse.add(elementComponent)
elementComponent.fractions.forEach { elementFraction ->
val isotopeComponent = this.materials.get<GdmlIsotope>(elementFraction.ref)
isotopesInUse.add(isotopeComponent!!) // element must be mode of isotopes
}
} else {
val isotopeComponent = this.materials.get<GdmlIsotope>(fraction.ref)
if (isotopeComponent != null) {
isotopesInUse.add(isotopeComponent)
}
}
}
}
}

this.structure.content.forEach {
val volume = this.structure.get<GdmlVolume>(it.name)
if (volume != null) {
val material = volume.materialref.resolve(this)!!
materialsInUse.add(material)
val composite = this.materials.get<GdmlComposite>(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) }
*/

this.materials.content.clear()
// Refill materials only with used ones
isotopesInUse.forEach {
this.materials.content.add(it)
}
elementsInUse.forEach {
this.materials.content.add(it)
}
materialsInUse.forEach {
this.materials.content.add(it)
}

return this
}
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/space/kscience/gdml/gdmlIO.kt
Original file line number Diff line number Diff line change
@@ -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 {
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/space/kscience/gdml/materials.kt
Original file line number Diff line number Diff line change
@@ -92,4 +92,4 @@ public data class GdmlComposite(override var name: String) : GdmlMaterial() {
fractions.add(GdmlFraction(n, ref.ref))
}

}
}
18 changes: 17 additions & 1 deletion src/jvmTest/kotlin/space/kscience/gdml/PredefinedGeometryTest.kt
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package space.kscience.gdml
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.test.assertNotNull
import kotlin.test.assertEquals

class PredefinedGeometryTest {

@@ -54,7 +55,22 @@ 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")
val gdml = Gdml.decodeFromStream(file.inputStream(), true)
val materialToRemove = gdml.materials.get<GdmlMaterial>("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)
}
}

0 comments on commit bfd113f

Please sign in to comment.