Menu

Errors when run Matlab link for battery control sample case

Hongda Ren
2017-10-19
2017-12-06
  • Hongda Ren

    Hongda Ren - 2017-10-19

    Hi ,
    I am learning how to use Matlab link to work with GridLAB-D. I download a sample case through the link https://sourceforge.net/p/gridlab-d/code/HEAD/tree/course/Tutorial/Chapter%209%20-%20Interfacing%20with%20External%20Software/MATLAB%20Link/

    GridLAB D version
    GridLAB-D 3.2.0-5368 (Jojoba) 64-bit WINDOWS RELEASE

    I test the files
    GridLAB D files: MATLAB_battery_control.glm; MATLAB_battery_control.link;
    M file: battery_controller.m
    What I changed:
    1. Add “#setenv PATH=D:\Program Files\MATLAB\R2016a\bin\win64” in glm file
    2. Add : “workdir D:\GRIDlabD\Videos\TestTry\MatlabLink\” in link file

    GridLAB D throw out the errors:
    ERROR [2009-08-08 04:30:00 PDT] : 'Matlab: 'Undefined function 'battery_controller' for input arguments of type 'double'' in'
    ERROR [2009-08-08 04:30:00 PDT] : exec halted: internal property sync failure
    FATAL [2009-08-08 04:30:00 PDT] : shutdown after simulation stopped prematurely
    FATAL [2009-08-08 04:30:00 PDT] : environment startup failed: Invalid argument
    ERROR [2009-08-08 04:30:00 PDT] : 'Matlab: 'File not found' in 'csvread (line 35)''

    I search the first error and find that is an error that Matlab can not find the file battery_controller.m.
    But I place all file in the same folder which is the same as work directory added in link file. I do not know why Matlab can not find it. Do we need to add any path setting in glm or link file?
    Could you help me make the sample case run?

    Thanks,
    Hongda

     

    Last edit: Hongda Ren 2017-10-20
    • Frank Tuffner

      Frank Tuffner - 2017-10-23

      Hello Hongda,

      Sorry for the delay in getting back to you on this. You're on the right path, but the workdir property inside the link file is a bit odd. Due to how this is coded up, this only supports relative paths, to either MATLAB or where the GLM is located. To run the demo, have the .m files in the same folder as the GLM. Then, use:

      workdir .
      

      inside the .link file. That will direct MATLAB to look in the folder you are executing from (presumably the one the GLM is in), and it appears to work.

      -Frank

       
      • Hongda Ren

        Hongda Ren - 2017-10-23

        Hello Frank,

        Thank you for the reply! I tried the code "workdir ." and place .m files and glm files in the same folder. Then, the case run well! That will be really helpful!
        Thank you!

        Thanks,
        Hongda

         
  • Hongda Ren

    Hongda Ren - 2017-10-23

    Hi Technical support engineer,

    I tested the first case call Energy Meter in the following http://gridlab-d.sourceforge.net/wiki/index.php/Chapter_9_-_Interfacing_with_External_Software.
    It works well. The difference between two cases is that matlab commands are writen in the link file instead of a function call in the Battery Controller case.

    The error in previous question is Matlab can not find the called function trough GridLAB-D. I searched historcial questions, but there is no related solution. Could you help me to solve this problem and it will be really helpful for new users to apply Matlab link for their control in GridLAB-D.

    Thanks,
    Best,
    Hongda

     
  • Hongda Ren

    Hongda Ren - 2017-11-30

    Hi Technical support engineer,

    I tried to use MatlabLink in my own case. There is no error showing in GLD console, but Windows system pop up a window saying "gridlabd.exe has stopped working".
    Have you ever meet such problem? Look forward to hearing from you!

    Thanks
    Hongda

     

    Last edit: Hongda Ren 2017-11-30
    • Frank Tuffner

      Frank Tuffner - 2017-12-01

      Hello Hongda,

      Unfortunately, that error is very nebulous and can mean pretty much anything. The "good news" is that it does the same thing for me. However, it's not completely obvious why it is failing. I'll need to look into it a bit further and see if it is something with your GLM, or with GridLAB-D directly. Hopefully, I'll have some insights in the next couple days.

      -Frank

       
      • Hongda Ren

        Hongda Ren - 2017-12-01

        Hello Frank,

        Thank you for your swift reply. The good thing is that the sample case runs pretty well. I am trying to figure out the difference between my own case and the sample case.
        The phenomenon is Matlab is called and icon shows, but it does not reach "on_init" yet before GLD stopped.
        Look forward to hearing from you!

        Have a nice day!

        Thanks,
        Hongda

         
        • Frank Tuffner

          Frank Tuffner - 2017-12-01

          Hello Hongda,

          The segmentation fault is being caused by GridLAB-D trying to publish all of the objects into the MATLAB space. Unfortunately, MATLAB does not like variables to start with numbers. GridLAB-D has a crude check on this, but had a small logic hole that allowed it to get through and cause issues.

          Basically, there are two ways to fix this and get it to run. The first, and recommended suggestion, is to not use any GLM files that have numbers starting the name. i.e., name 623-634; is not valid, it would need to be something like name l623-634;. Basically, the GLM needs to be able to run without the #set relax_naming_rules=1 declaration at the top (this relaxes the "no numbers starting names" rule for legacy GridLAB-D files and is not recommended).

          The other option to work around is to prevent MATLAB from trying to import all of these objects. Inside the .link file, near the bottom, you can adjust it to look like the following:

          ...
          # Objects to publish (readonly)
          # Default is all objects
          # Listing "object" all by itself once will cause no objects to be published
          object
          
          # Variables to import from matlab to gridlabd and to export from gridlabd to Matlab workspace (by name)
          ...
          

          I'll also attach the modified .link file to this post. Note the comments around there. By putting "object" there, it does not publish any of the objects within MATLAB, and gets around this issue. The import and export commands still work, you just won't have unfettered access to the objects within MATLAB.

          I'm pushing a fix to our /develop branch that will go into the next release. It does a more explicit check for this condition and fails the simulation "gracefully", rather than MATLAB causing a segmentation fault.

          -Frank

           
          • Hongda Ren

            Hongda Ren - 2017-12-02

            Hello Frank,

            One thing I would like to add when I use your second solution. If we want to publish multiple objects from GLD to MATLAB, the format for object list I add is as follow.

            object object1

            object object2
            ...

            Instead of "object object1, object2" or "object object1 object2".

            Thanks,
            Hongda

             

            Last edit: Hongda Ren 2017-12-02
            • Frank Tuffner

              Frank Tuffner - 2017-12-05

              Thanks Hongda, that is good to point out.

              I had not tried that specific functionality, but I imagined it would work similar to export and import lines in that part of the link file -- each one needs a specific callout.

              This is good to indicate here though, in case future searches come looking for the same thing.

              -Frank

               
            • David P. Chassin

              I took a look at the link code, and I'm pretty sure you can specify multiple object lines if you want. Each line is processed individually but all the objects will be added to the data exchange list.

               
              • Hongda Ren

                Hongda Ren - 2017-12-06

                Yes David. You are right. I add mutiple object lines for different objects and export their properties to MATLAB without any problem. As Frank mentioned, each object needs specific callout.
                Thank you both!

                Hongda

                 
  • Hongda Ren

    Hongda Ren - 2017-12-01

    Hello Frank,

    Thank you very much for your so detailed explanation and swift reply!

    The second option is resource saving, because what we want to publish from GLD to MATLAB is much less than all objects. I will try both solutions you suggest.

    Really appreciate your help!
    Have a great day!

    Thanks,
    Best wishes,
    Hongda

     

Log in to post a comment.