简体   繁体   English

F# - 变量范围

[英]F# - Scope of variables

I have an F# code computing liquid flow. 我有一个F#代码计算液体流量。 The code consist of 2 projects. 代码由2个项目组成。 The core library and project to run simulations. 运行模拟的核心库和项目。 When I run a parametric study like 当我运行参数研究时

open CoreLibrary
for lbmViscosity in [0.1] do
  for Re in [0.001; 0.5] do
    for lbmD in [6.;5.;4.;3.;2.5;2.;1.5;1.;0.8;0.6;0.4;0.2] do
      simulation setup
      let result = call CoreLibrary
      save result to file

then I obtain correct result. 然后我得到了正确的结果。 When I run the code as: 当我运行代码时:

open CoreLibrary
for lbmViscosity in [0.1] do
  for Re in [0.1; 0.001; 0.5] do
    for lbmD in [6.;5.;4.;3.;2.5;2.;1.5;1.;0.8;0.6;0.4;0.2] do
      simulation setup
      let result = call CoreLibrary
      save result to file

Then for Re = 0.001, I get a wrong result. 那么对于Re = 0.001,我得到了错误的结果。 I have also tried Array.iter instead of for loop with the same result. 我也尝试过Array.iter而不是for循环,结果相同。 When I compile my code and run it as exe with different input parameters then it works well. 当我编译我的代码并使用不同的输入参数将其作为exe运行时,它运行良好。

Is there anything which could cause an incorrect result aside from mutation? 除了变异之外还有什么可能导致错误的结果吗? May it be Garbage collector's fault? 可能是垃圾收集器的错吗? And is there any command which would completely clean up a memory at certain place? 是否有任何命令可以在某个地方彻底清理记忆? Something like: 就像是:

open CoreLibrary
for lbmViscosity in [0.1] do
  for Re in [0.1; 0.001; 0.5] do
    for lbmD in [6.;5.;4.;3.;2.5;2.;1.5;1.;0.8;0.6;0.4;0.2] do
      clean everything
      simulation setup
      let result = call CoreLibrary
      save result to file

I mutate values only in the core library not in the for loops. 我只在核心库中而不是在for循环中改变值。 Therefore I would expect that as soon as for loop goes to next cycle or as soon as the end of Array.iter occurs everything inside is erased. 因此,我希望只要for循环进入下一个循环,或者一旦Array.iter结束,内部的所有内容都将被删除。

Thanks for any help, hint etc :) 谢谢你的帮助,提示等:)


So I have found what was wrong - Massif was right! 所以我发现了什么错 - Massif是对的! :) In the coreLibrary, we use ConcurrentDictionary. :)在coreLibrary中,我们使用ConcurrentDictionary。 We wanted to use Records instead of Classes (to be functional :D) and records do not allow to create the dictionary inside of the class, so the dictionary has been created outside of the class and linked into the record by member functions. 我们想使用Records而不是Classes(功能:D)并且记录不允许在类内部创建字典,因此字典已经在类之外创建并通过成员函数链接到记录中。 So once the dll was loaded the dictionary has been created and stayed alive for ever... Now we changed the record into class and everything works fine. 因此,一旦dll被加载,字典就被创建并永远保持活着......现在我们将记录改为了类,一切正常。 Thank you very much for your hints 非常感谢您的提示

So I have found what was wrong - Massif was right! 所以我发现了什么错 - Massif是对的! :) In the coreLibrary, we use ConcurrentDictionary. :)在coreLibrary中,我们使用ConcurrentDictionary。 We wanted to use Records instead of Classes (to be functional :D) and records do not allow to create the dictionary inside of the class, so the dictionary has been created outside of the class and linked into the record by member functions. 我们想使用Records而不是Classes(功能:D)并且记录不允许在类内部创建字典,因此字典已经在类之外创建并通过成员函数链接到记录中。 So once the dll was loaded the dictionary has been created and stayed alive for ever... Now we changed the record into class and everything works fine. 因此,一旦dll被加载,字典就被创建并永远保持活着......现在我们将记录改为了类,一切正常。 Thank you very much for your hints 非常感谢您的提示

What data types are you using? 您使用哪些数据类型? Is it possible that the floating-point types that you are using are introducing representation errors? 您使用的浮点类型是否可能引入表示错误? What constitutes "correct" and "incorrect" output? 什么构成“正确”和“不正确”输出?

As a general rule, opening or not opening a library won't have any impact on the result of a calculation (assuming that the code still compiles!). 作为一般规则,打开或不打开库不会对计算结果产生任何影响(假设代码仍然编译!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM