TRNSYSでPythonスクリプトを動かす

2022/06/20 追記

Python用に新しいコンポーネント、Type3157がリリースされています。Type169よりも安定して動作するので、こちらの利用がお薦めです。

2020/01/30 Numpyなど外部モジュールの組み合わせでエラーが確認されています。原因調査中です。

Type169 Python

TRNSYS18からPython用のコンポーネント、Type169が追加されています。TRNSYSのプロジェクトで、ちょっとした計算や判定処理をPythonで手軽(?)に記述できます。でも、いきなりType169を利用すると図のようなエラーが発生します。

Type169でエラーが発生する
Type169でエラーが発生する

詳細なメッセージは以下。

*** Fatal Error at time   :         0.000000
     Generated by Unit     : Not applicable or not available
     Generated by Type     :   169
     TRNSYS Message    105 : A TYPE was called in the TRNSYS input file but was either not linked into trndll.dll or was not found in an external dll. A dummy subroutine was called in its place. Please link the TYPE or remove it from the input file
     Reported information  :  Type169 could not be located in either the trndll.dll or in an external dll. Please relink theTRNDll.dll including this Type or make sure that an external DLL in the \UserLib\DebugDLLs and \UserLib\ReleaseDLLs folders contain the Type.

これ、普段はTypeのDLLが無い、もしくは関連するDLLが不足していると表示されるメッセージです。少々分かりにくいですが、Type169ではPyhtonの実行環境が必須です。Type169を使用する前に、予め環境を整えてないとこのエラーになります。

Pythonの環境を整える

ということで、事前にPythonの実行環境の設定が必要です。

注意!!
Type169はPython3.6用にビルドされています。それ以外のバージョンで使用する場合は、リビルドが必要です。

一般的にはPythonの公式サイト(https://www.python.org/)から入手しますが、ここではAnacondaのパッケージを使って環境を整えます。(サイズは少々大きくなりますが、こちらの方が取り扱いが簡単です)

AnacondaのダウンロードサイトからPython3.6,64bit版のインストーラーをダウンロードしてインストールします。

https://www.anaconda.com/download/

Anacondaのダウンロード

インストール中のオプションは基本的にすべてデフォルトでOKです。途中、環境変数Pathのオプションは後から手作業で設定するのが面倒な場合はチェックします。

環境変数のオプションはチェック

環境変数の設定

上記で環境変数のオプションをチェックしていた場合は、このセクションは必要ないので無視してください。

インストールが終了したら、次は環境変数、PathにAnacondaのパスを追加します。

デフォルトでインストールしていれば、環境変数Path C:Users<ユーザー名>Anaconda3 を追加すれば準備完了です。

2018/3/9 追記

Anaconda 5.1をインストールしたら、パスが変わっていました。(というか前回勘違いしていた?)環境変数Pathに追加するのは、こちらのパスになります。

C:\Users\<username>\AppData\Local\Continuum\anaconda3

Anacondaのバージョンやインストール方法で変わるのかも知れないので、環境に合わせて調整してください。

2018/5/11 追記

インストールの仕方(Anacondaのバージョン?)によってはインストール先のパスが変わる事があるようです。Type169の依存関係を調べるとPython36.DLLを参照しているのが分かります。Anacondaのインストール先を確認して、このファイルが存在するフォルダをPathに追加する必要があります。

Dependancy WalkerによるType169.dllの解析結果
Dependancy WalkerによるType169.dllの解析結果

例)環境変数の登録

環境変数の登録
環境変数の登録

Simulation Studioでなにかプロジェクトを用意して、うまく動けば設定環境です。

Simulation Studioで実行
Simulation Studioで実行

Pythonスクリプト

この例で使っているスクリプト(以下)を簡単に説明すると、Type169からはPythonFunction()を呼び出して、さらにmyFunc()を呼び出しています。

エラーが起きたときにログファル(_error.log)に書き出したかったので、2段階の呼び出しになっています。直接myFunc()を呼び出してもそのまま動くはずです。

type169_wtih_try.py

"""type169(Python) sample"""
import TRNSYSpy as trn
import traceback
import os

def myFunc():
    # retrieve values from the Inputs
    inp1 = trn.getInputValue(1)
    inp2 = trn.getInputValue(2)
    inp3 = trn.getInputValue(3)
    # processing
    out1 = inp1+inp2
    out2 = inp3**inp2
    # return the new values to the Outputs
    trn.setOutputValue(1, out1)
    trn.setOutputValue(2, out2)
    return

def PythonFunction():
    """This function is called from TRNSYS/Type169"""
    # delete the previous log file if it exists.
    logfile = "_error.log"
    if os.path.exists(logfile):
        os.remove(logfile)

    try:
        myFunc()

    except:
        # save messages to the log file when something goes wrong.
        print('error')
        with open(logfile, 'w') as f:
            f.write(traceback.format_exc())

上記サンプルのダウンロードはこちらから。

Github/TRNSYS.JP

https://github.com/TRNSYSJP/TRNSYS.JP/tree/master/TRNSYS18/MyProjects/Type169_Calling_Python

Type169.dll for Python3.8

TRNSYS18添付のType169はPyhton3.6用です。Python3.8を疲れる場合はリンク先からDLLをダウンロードして、C:\TRNSYS18\UserLib\ReleaseDLLsへ配置してください。

動作環境

以下の環境で動作を確認しています。

Windows10 Pro(64bit)
Python:Anaconda 5.0.1(Python 3.6/64bit) https://www.anaconda.com/download/
TRNSYS18.00.0015(64bit)

Pocket

18件のコメント

  • Hello,

    Could you pls tell me why when I import python modules, type 169 works for
    some modules and does not work for some others python modules.

    For example:

    If in my python script:

    import TRNSYSpy as trn
    import os
    import traceback
    import math
    import sys

    It works.

    However when I tried to import numpy or some other modules (scipy, pandas),

    I got this error

    Floating point division by zero

    Thank you very much for your help.

    Best regards,

    Long

  • It seems to me that Type169 is not linked to the latest library or is linked to wrong library.

    You should contact your local TRNSYS distributor for help.

  • Dear Yuichi,

    Thank you very much for your reply. Indeed I tried to contact my local distributor. However, they cannot give me more help than your post and so I also tried to contact TESS. What I received as the answer is as following:
    "Unfortunately we cannot provide much insight into the issue you are having. I would guess that it was path issues where the Python function cannot find the included modules, but we don't know the answer for certain."

    Indeed, I tried to recompiled the Type169.cpp as another type Type269 and put the dll file in Userlib. But it doesn't work either.

    By the way, thank you very much for your reply.

    I really appreciate your posts.

    Best regards,

    Long

  • Dear Yuichi,
    Thanks you very much for your post. It is very useful !
    I have followed your instructions but I have an issue with the TRNDLL64.DLL when I check the dependency of the Type169.
    Do you have any ideas ?
    Thanks you very much for your help
    Best regards,
    AA

    • Hi AA,

      TRNDLL64.DLL is also referenced by TRNEXE64.exe.
      Even if Dependency Walker can’t find the TRNDLL64.DLL No problem. TRNSYS will find the DLL during the calculation, so you can calculate without any problems.
      By the way, I tried the latest version of Anaconda,2020.02 (Python 3.7.6 64-bit), but apparently Type169 doesn’t seem to work.

      I rebuilt Type 169 along with the latest version of the Python3.8.3 library. It worked properly.
      Best,
      Yuichi

  • Hello,
    When I test the code you provide, it shows: No module named ‘TRNSYSpy’, and I guess this is the reason why I can’t use direct calling in TRNSYS now.

    So, do I need to install any python package before importing TRNSYSpy?

    Thank you!

  • Sorry for disturbing you again @Yuichi. Just want to know if you find a solution for type169 with external python package like numpy pls?

    Thanks!

    Long

    • Long,

      I’ve looked into the cause of the problem several times, but I still haven’t figured out a solution.
      I’ll let you know if I find a solution.

      Best,
      Yuichi

  • Thanks Yuichi!

    Long

  • Dear Yuichi,
    I am looking for a solution to perform post-convergence operations : How Can I get the flag: INFO(13) within the python script ? The idea is to call the python function only after the convergence

    Thanks,
    Best,
    AA

    • Dear AA,

      Type169 only calls one Python function to process the calculation. It doesn’t do anything post-convergence.
      That means there’s no way to get the flag.
      If you want to do post-convergence processing, you’ll have to create a new component of your own version.

      Best,
      Yuichi

  • Dear Yuichi,

    Due to compatibility problems with other applications I need to use Python 3.7 .
    I was wondering if you could please share a Type169.dll suitable for that version of Python?

    I realized that my version of Trnsys is 32-bit. Do this have an impact on whether the Type169 works or not? I read something relate to this at the TRNSYS-USERS thread:
    http://lists.onebuilding.org/pipermail/trnsys-users-onebuilding.org/2018-July/030289.html

    Thank you very much!
    Manuel

    • Manuel,

      Unfortunately, As I mentioned in my earlier post, I’m not sure why, but Type 169 doesn’t work with Python 3.7.

      If you are using Python 3.7, try to build Type169 with Python 3.7 yourself. Of course, you must build it as a 32bit version.

      I recommend that you adapt your application to the 64-bit version of TRNSYS and python3.8.

      Best,
      Yuichi

      • Dear Yuichi,
        I managed to solve the compatibility with the other applications.
        Your version of Type169.dll for Python 3.8 works perfectly with Trnsys (64-bit).
        Thank you very much,
        Manuel

Long Le へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です