Git Product home page Git Product logo

gdml.kt's Introduction

Gdml bindings for Kotlin

JetBrains Research DOI Maven Central

Kotlin JS IR supported

Multiplatform bindings for Gdml geometry specification. Utilized kotlinx.serialization to read and writing Gdml configurations.

In the future, it will be possible to add additional module to launch GEANT4 simulations from Kotlin code.

Artifact:

This module artifact: space.kscience:gdml:0.4.0.

Gradle:

repositories {
    mavenCentral()
    maven { url "https://repo.kotlin.link" }
}

dependencies {
    implementation 'space.kscience:gdml:0.4.0'
}

Gradle Kotlin DSL:

repositories {
    mavenCentral()
    maven("https://repo.kotlin.link")
}

dependencies {
    implementation("space.kscience:gdml:0.4.0")
}

Usage

Read:

import space.kscience.gdml.decodeFromString

val gdmlString: String
val gdml = Gdml.decodeFromString(gdmlString)

write:

import space.kscience.gdml.encodeToString

val gdml = Gdml{}
val gdmlString = gdml.encodeToString()

gdml.kt's People

Contributors

altavir avatar elinorre avatar lobis avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

gdml.kt's Issues

Volume declaration order violation (should not be able to reference what is next)

Package don't make true order of volume structure.
Code:

            structure {
                val matref = GDMLRef<GDMLMaterial>("G4_AIR")
                val worldBox = [email protected]("World", 100, 100, 100)
                val volWorld = volume("world", matref, worldBox.ref()){}
                val segment = [email protected]("InnerTube", 20, 5.0){
                    rmin = 17
                    deltaphi = 60
                    aunit = "degree"
                }
                val vol = volume("vol1", matref, segment.ref()) {}
                [email protected] = volWorld.ref()
                [email protected] {
                    physVolume(vol.ref()) {
                        positionref = ref("box_position")
                        rotationref = ref("Rot1")
                    }
                }
            }

Result --- volume vol1 define after its using:

   <structure>
        <volume name="world">
            <physvol>
                <volumeref ref="vol1"></volumeref>
                <positionref ref="box_position"></positionref>
                <rotationref ref="Rot1"></rotationref>
            </physvol>
            <materialref ref="G4_AIR"></materialref>
            <solidref ref="World"></solidref>
        </volume>
        <volume name="vol1"> 
            <materialref ref="G4_AIR"></materialref>
            <solidref ref="InnerTube"></solidref>
        </volume>
    </structure>

In my gdml dsl i sorted tag by its children:

    override fun compareTo(other: VOLUME): Int {
        if (dependenciesVolumeList.contains(other.name)){
            return 1
        }
        if (other.dependenciesVolumeList.contains(this.name)){
            return -1
        }
        return 0
    }

Implement grouping of physical volumes

I would be very useful to be able to define groups of physical volumes. At the moment, in order to create multiple instances of a composite physical volume, I am using a for loop in the ´world´.

This works fine when you only need to change the position of the composite volumes, however, if you also need to rotate the composite volume, things get complicated. If you just apply the composite volume rotation to every individual physical volume, then you would also need to rotate the position vector accordingly, which can get messy.

I would be nice to have some kind of object that groups ´physVolume´ objects and that can placed as a physical volume, taking rotation and position parameters, but in this case it handlers the rotation as one would expect, rotating all the child volumes and changing their relative position so that the final result is a rotated composite volume.

Volumes placed with a loop only changing position:
norotation

Same volumes after changing global rotation (but not rotating position)
rotationY90_nochangeposition

Problems with nested assembly

I am currently writing the complete geometry of our experiment using this tool. Currently the latest version is available here https://github.com/lobis/gdml/ and I plan to include it in this repository as a test once it is finished (if you agree of course).

I am trying to use all the features of Kotlin and the DSL in order to make it easier to build and to increase readability, any advice would be very welcome.

The problem I am encountering is that I am using nested assembly to organize my volumes. In my case I have defined a "veto" which is included into a "vetoLayer" which is also included into a "vetoGroup" (multiple stacked layers). I hope it is easy to see in https://github.com/lobis/gdml/blob/fb485903b4e31a98697e34bad756db40a333afdd/src/main/kotlin/main.kt I have marked the relevant portions with comments/TODO.

When I create them using the "vetoGroup" approach (assembly inside assembly inside assembly) I cannot see the volumes in root (you can run the included Gdml.C script to test this). I don't think I am doing something strange, if it works with 2 assemblies nested, it should work with 3, right? From the event viewer it looks like everything is already, the volumes appear, they also appear in the .gdml file.

Perhaps this is a limitation of root itself? Please, tell me if I am missing something.

Thanks, this tool is enabling me to create very complex geometries very easily!

Remove unused materials definitions from final gdml

Currently materials loaded from a URL via loadMaterials (src/jvmMain/kotlin/space/kscience/gdml/loadMaterials.kt) are added to the <materials> section of the final GDML.

This creates a nuisance when using the GDML in Geant4 as Geant4 will (only on some physics lists such as decay) load cross sections for all isotopes which can take a few extra minutes in my experience.

The solution would be to identify materials used in the geometry and only keep those, while also keeping child materials/isotopes/elements. It would also reduce the size of the file and make it more readable.

I will try to implement this if nobody is available.

Edit: I am currently working on this.

Make `name` field optional to simplify syntax

When defining geometries I often name my solid/volume the same as the variable that holds the object (e.g. val foo = solids.box("foo", 1,1,1)). I am finding myself doing too much "copy and paste", perhaps there can be a default value such as the name of the object (reflection) or in case this is not available some random not in use name.

Also I recommend looking into https://geant4.web.cern.ch/sites/geant4.web.cern.ch/files/geant4/collaboration/working_groups/geometry/docs/textgeom/textgeom.pdf, the Geant4 Text Geometry plain text description (which can be exported to GDML). In this description you can define physical volumes without the need of defining its solid / volumes first. I found this description very useful and user friendly to write simple geometries, perhaps we could integrate some of the best parts of this description.

Text Geometry: Tube physical volumes defined in a single line

:VOLU target TUBE 0 50 100 G4_Ge
:PLACE target 1 world R00 0. 0. 0.

Example where this happens a lot:
https://github.com/mipt-npm/gdml.kt/blob/e01cd969ae621ba4661ef8d51fcb72e0a28efc1e/gdml-script/src/test/kotlin/babyIAXO.gdml.kts

Platform independence in tests

The test uses a hardcoded path separator, only working on Windows. This patch makes it platform independent.

Btw. there is a typo in the package name.

Index: src/jvmTest/kotlin/sicentifik/gdml/BMNTest.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/jvmTest/kotlin/sicentifik/gdml/BMNTest.kt	(revision a8296c66747217dc06d366680ff522c0c5ee6b85)
+++ src/jvmTest/kotlin/sicentifik/gdml/BMNTest.kt	(date 1576593574631)
@@ -36,7 +36,7 @@
     @Test
     fun testRead() {
         val url = URL("https://drive.google.com/open?id=1w5e7fILMN83JGgB8WANJUYm8OW2s0WVO")
-        val file = File("gdml-source\\[email protected]")
+        val file = File("gdml-source${File.separatorChar}[email protected]")
         //val file = File("D:\\Work\\Projects\\gdml.kt\\gdml-source\\cubes.gdml")
         val stream = if(file.exists()){
             file.inputStream()

Change materials position in .gdml file for ROOT compatibility

As of version 0.4.0-dev-3 the <materials/> xml child containing all materials definitions appears at the bottom of the exported .gdml file. This is a problem when visualizing the geometry via the ROOT TGeoManager since it will ignore the materials and everything will appear colorless, unless the materials appear at the top.

The decision to place the materials at the end makes the most sense otherwise, it increases the readability. Including the whole materials.xml file right at the beginning can be a bit annoying, I suggest implementing an optional parameter to the loadMaterialsFromUrl method that when specified, copies only the materials actually referenced in the geometry (and all its components), this would greatly reduce the visual clutter (but may present problems with versioning the materials, that's why we should keep the option to just copy the whole file).

Add remaining primitives

  • reflectedSolid
  • union
  • subtraction
  • intersection
  • box
  • twistedbox
  • twistedtrap
  • twistedtrd
  • paraboloid
  • sphere
  • ellipsoid
  • tube
  • twistedtubs
  • cutTube
  • cone
  • polycone
  • para
  • trd
  • trap
  • torus
  • orb
  • polyhedra
  • xtru
  • hype
  • eltube
  • tet
  • arb8
  • tessellated

Add naming restriction for naming

In order to comply with vague GEANT requirements.

Seems like this one is the restriction:
The practical restrictions of NCName are that it cannot contain several symbol characters like :, @, $, %, &, /, +, ,, ;, whitespace characters or different parenthesis. Furthermore an NCName cannot begin with a number, dot or minus character although they can appear later in an NCName.

Additional dependencies requiered

Most likely this is a mistake on my part due to my little experience with Java/Kotlin/Gradle, but I had trouble adding this repository to my Kotlin project.

After following the readme I add the following to my build.gradle.kts:

repositories {
    maven("https://repo.kotlin.link")
}

dependencies {
    implementation("space.kscience:gdml:0.3.0")
}

If I only add these dependencies I get the following error after running import space.kscience.gdml.decodeFromString:

Execution failed for task ':compileKotlin'.
> Could not resolve all files for configuration ':compileClasspath'.
   > Could not find net.devrieze:xmlutil-serialization:0.81.0.
     Searched in the following locations:
       - https://jcenter.bintray.com/net/devrieze/xmlutil-serialization/0.81.0/xmlutil-serialization-0.81.0.pom
       - https://repo.maven.apache.org/maven2/net/devrieze/xmlutil-serialization/0.81.0/xmlutil-serialization-0.81.0.pom
       - https://repo.kotlin.link/net/devrieze/xmlutil-serialization/0.81.0/xmlutil-serialization-0.81.0.pom
     Required by:
         project : > space.kscience:gdml:0.3.0 > space.kscience:gdml-jvm:0.3.0

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

Looks like I need additional dependencies. I solved the problem by adding

repositories {
    jcenter()
    maven("https://repo.kotlin.link")
    maven("https://dl.bintray.com/pdvrieze/maven")
}

dependencies {
    implementation("net.devrieze:xmlutil-serialization:0.81.0")
    implementation("space.kscience:gdml:0.3.0")
}

From my understanding jcenter() is something that would be common enough to be added by default, but the other dependency should be either mentioned on the readme or come bundled with the project somehow.

Error parsing materials XML

This is probably some silly mistake but I think this XML is correct, yet it gives an error when being parsed via loadMaterialsFromUrl("https://raw.githubusercontent.com/rest-for-physics/materials/d7b68502d6d7812a084d49f979ad833a036773e3/materials.xml").

https://github.com/rest-for-physics/materials/blob/d7b68502d6d7812a084d49f979ad833a036773e3/materials.xml

Caused by: nl.adaptivity.xmlutil.XmlException: Failure to parse children into string at [row,col {unknown-source}]: [9,9]

What is wrong with my XML? Thanks.

G4GDML: VALIDATION ERROR! when parsing Gdml in Geant4

I have seen the following errors when trying to parse the geometry with Geant4. To parse the geometry I have just done:

// Geant4 code
#include "iostream"
#include "G4GDMLParser.hh"

using namespace std;

void parseGdml(const string &filename = "Setup.gdml") {
    G4GDMLParser parser;
    cout << "Reading from '" << filename << "'" << endl;
    parser.Read(filename.c_str());
}

int main() {
    cout << "Starting..." << endl;
    parseGdml();
}

Which should load the geometry from a gdml file. They error is G4GDML: VALIDATION ERROR!. I have included a few lines here:

...
G4GDML: VALIDATION ERROR! no declaration found for element 'physvol' at line: 4165
G4GDML: VALIDATION ERROR! attribute 'name' is not declared for element 'physvol' at line: 4165
G4GDML: VALIDATION ERROR! no declaration found for element 'volumeref' at line: 4166
G4GDML: VALIDATION ERROR! attribute 'ref' is not declared for element 'volumeref' at line: 4166
G4GDML: VALIDATION ERROR! no declaration found for element 'position' at line: 4167
G4GDML: VALIDATION ERROR! attribute 'x' is not declared for element 'position' at line: 4167
G4GDML: VALIDATION ERROR! attribute 'y' is not declared for element 'position' at line: 4167
...

Looks like they format is not compatible with the Geant4 parser? We have encountered this in the past, when I build geometries I usually test it using the root event visualizer but after doing some simulation we find additional errors. In the case of this geometry, it is correctly read from root via TGeoManager::Import("Setup.gdml");. I have tried modifying a few things in the .gdml that I could think of but no luck.

I attach a zip of the .gdml geometry (it is the one in the gdml-script test called babyiaxo) Setup.zip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.