将字符串解释为引用程序内变量。 Geant4 单位

发布于 2024-10-09 16:28:12 字数 559 浏览 0 评论 0原文

所以,我知道这不是语言中内置的东西(即 c++ 中没有 eval() 方法)。我还了解到,将运行时输入的字符串内容与程序中的变量名称关联起来非常困难,因为大部分信息都会消失。尽管如此......

我正在尝试编写一个小类,以允许我的研究组中的用户将材料和材料属性添加到数据库文件中,并使用一个命令调用这些材料,以便在 Geant4 中轻松构建材料。不幸的是,Geant4 使用头文件来定义这些单位的实际变量,例如 cm3、mm、MeV 和 kg,以将单位转换为内部 Geant4 单位系统。我不相信有任何类型的地图可以将这些链接到相应的字符串。事实上,Geant4 用于物理建模,并且有大量的单位。我也没有真正的方法可以将它们全部映射,因为 Geant4 是可扩展的,并且可能会出现一些新的物理代码,需要一个我不知道的新单元。

我想做的是从文本文件中获取描述单位的字符串,并以某种方式将其与同名的实际变量相匹配,以便我可以执行转换。有什么办法可以解决这个问题,还是完全不可能(或几乎不可能)?

当然,我可以让人们将所有数据库条目转换为内部单位,但这很快就会变得丑陋,并且成为难以追踪错误的一大来源(小数点前 15 位看起来很像 14)。

谢谢

So, I understand that this is not something built into the language (i.e. there is no eval() method in c++). I also understand that it is very difficult to associate the contents of a string input at runtime with a variable name from the program, since most of that info goes away. Nevertheless...

I am trying to write a little class to allow users in my research group to add materials and material properties into a database file and call those materials up with one command so that material construction is easy in Geant4. Unfortunately, Geant4 uses a header file to define actual variables for these units such as cm3, mm, MeV, and kg to convert units into the internal Geant4 system of units. I don't believe there is any kind of map that links these to their corresponding strings. The fact of the matter is, Geant4 is for physics modelling, and there are a butt ton of units. There is no real way I can map them all, either, since Geant4 is expandable and some new physics code may come along that wants a new unit I don't know about.

What I would like to do is get the string that describes the unit from the text file and somehow match that to the actual variable of the same name so I can perform the conversion. Is there any way to hack and slash this out, or is it totally impossible (or totally nearly impossible)?

Of course I could make people convert to the internal units for all of their database entries, but that could quickly get ugly and be a large source of hard to track down error (15 places before the decimal looks a lot like 14).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-10-16 16:28:12

这个问题很难回答,部分原因是我从未使用过(或之前听说过)Geant4,部分原因是并不完全清楚您想要做什么:您是否正在制作一个 C++ 库供您的同事直接使用?或者您正在制作某种工具,让您的同事无需接触 C++ 即可使用?

尽管如此,我还是会尽力回答。

从根本上讲,如果您想在运行时获取文本输入并解释命名单元,那么您需要一个从单元名称到定义的映射表(无论这些定义采用什么内部形式)。确实没有任何办法解决这个问题。

但是,您不一定需要手动创建该映射表。该信息必须存在于定义单位的 Geant4 头文件中,因此您可以编写一个(如您所说的 hack 和斜线)工具来读取该头文件并生成映射表的代码。

至于将来支持新单位,这可能是可能的,但我对您的问题还没有真正了解,无法建议特定的方法。

不过,我在这里会有点冒险。在 google 上快速搜索 Geant4,并对我找到的第一个源代码进行极其粗略的搜索,找到两个相关文件: SystemOfUnits.hG4UnitsTable.hh

我注意到 2 提供了确切的名称 -您可能想要的到定义的映射,但我不清楚它与系统其余部分的关系,以及它是否相关。

如果 1 是相关标头,那么显然单位的定义是最简单的:即基本单位的比例因子。如果是这种情况,那么您的材料数据库文件没有特殊原因不应包含类似类型的单位定义(即,从加载材料定义的同一位置加载映射表),这将允许您的同事根据需要添加新单位。

我可能会提到最后一种可能性,我同意,虽然计算十进制数字非常容易出错,但如果允许用科学记数法表示值,那么大部分问题就会消失。因此,最简单的解决方案可能是告诉您的同事使用基本的 Geant4 单位,如果他们想要指定一个值,例如 5 公里,他们应该将其指定为 5e6(因为 Geant4 长度的基本单位是毫米,根据 1)。

This is difficult to answer, partly because I've never used (or heard of before now) Geant4, and partly because it's not entirely clear what you're trying to do: are you making a C++ library for your colleagues to use directly? Or are you making some kind of tool that your colleagues can use without touching C++ themselves?

Still, I'll try to answer as best I can.

Fundamentally, if you want to take textual input at runtime and interpret named units, then you need a mapping table from unit names to definitions (whatever internal form those definitions take). There isn't really any way around that.

However, you don't necessarily need to create that mapping table by hand. The information must be present in the Geant4 header file that defines the units, so you could write a (hack and slash, as you put it) tool to read that header file and generate code for a mapping table.

As for supporting new units in the future, it may be possible but I don't really understand enough about your problem to suggest a particular approach.

I'll go out on a limb a little here though. A quick google search for Geant4, and an extremely cursory search through the first source code I found leads me to two relevant files: SystemOfUnits.h, and G4UnitsTable.hh.

I note that 2 provides exactly the name-to-definition mapping that you might want, but I'm unclear as to how it relates to the rest of the system and therefore whether it's relevant.

If 1 is the relevant header, then clearly the definitions of the units is about the simplest possible: that is, scaling factors to base units. If this is the case, then there's no particular reason why your material database file shouldn't also contain unit definitions of a similar sort (that is, load your mapping table from the same place that you load the material definitions), which would allow your colleagues to add new units as necessary.

There is one final possibility I might mention, which is that while counting decimal digits is, I agree, extremely prone to error, if you allow values to be represented in scientific notation then much of that problem goes away. So, possibly the simplest solution is to tell your colleagues to use the basic Geant4 units, and if they want to specify a value of, say, 5 km, they should specify it as 5e6 (since the Geant4 basic unit for length is the millimetre, according to 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文