Run OptiStruct Externally (Matlab)

Altair Forum User
Altair Forum User
Altair Employee
edited April 2022 in Community Q&A

Hi all,

I'd like to use Matlab to control OptiStruct -- change design variables and run the analysis. My intent is to use numerical methods scripts I've written in Matlab and loop back and forth between Matlab and OptiStruct. The process would run as follows:

 

1. Matlab -- numerical methods algorithm takes in FEA results, outputs design variables

2. Optistruct -- takes in design variables, runs FEA and outputs results (.out file)

3. Repeat

 

Can anyone help me run OptiStruct from Matlab? More generally, does anyone have experience running OptiStruct externally?

 

Thank you in advance for any help!

 

Best,

Alex

Answers

  • RodrigoFST_22268
    RodrigoFST_22268 New Altair Community Member
    edited January 2020

    Hi Alex,

     

    Were you able to solve this? I want to run a code that changes the .fem file, calls optistruct to run the linear analysis and checks the compliance. After that it will repeat the process till it reaches convergence. How do you get matlab to run optistruct?

     

    Thanks,

    Rodrigo

  • Crashphys
    Crashphys Altair Community Member
    edited January 2020

    Hi,

     

    This is very complicated but can be done:

     

    %uploading a .fem to optistruct and reading punch file using matlab

    %step 0 add punch card request(s) at the top of the file
    % DISPLACEMENT(SORT1,PUNCH,REAL,NORMAL) = ALL                      
    % ESE(PUNCH,PEAK) = ALL %element strain energies (for compliance optimization)
    % GPFORCE(PUNCH,REAL) = ALL
    % GPSTRAIN(PUNCH,GLOBAL,ALL) = ALL
    % STRAIN(SORT1,PUNCH,REAL,ALL) = ALL
    % STRESS(SORT1,PUNCH,REAL,ALL,CENTER) = ALL

    %step 1 get path to fem
    path_new_fem = 'INSERT FILEPATH TO FEM FILE HERE';
    %step 2 get path to optistruct
    loc_opti = 'C:\Program Files\Altair\2017\hwsolvers\scripts\optistruct.bat';
    %step 3 send to solver using system() or dos()
    command_string1 = char(strcat(''',loc_opti,'' '',path_new_fem,'' -monitor'));
    answer = system(command_string1);
    %step 4 get path of the punch file
    punch_path = 'INSERT FILEPATH TO PUNCH FILE';
    %step 5 read the punch file
    PCH = reading_punch(punch_path);

     

    Using this code, matlab will run optistruct. One thing I have not included is the reading_punch code, which reads a punch file output from NASTRAN/OS. If you need this let me know, however I am certain punch file readers exist for matlab online already.

     

    If you are going to do it this way, you will need many other scripts, including one which will take your input variables calculated by matlab and write them to an FEM file. The punch reader can be used to read outputs from OS to matlab such as stress etc. What you will need is heavily dependent on what you are trying to do.

  • RodrigoFST_22268
    RodrigoFST_22268 New Altair Community Member
    edited July 2020

    Hi,

     

    This is very complicated but can be done:

     

    %uploading a .fem to optistruct and reading punch file using matlab

    %step 0 add punch card request(s) at the top of the file
    % DISPLACEMENT(SORT1,PUNCH,REAL,NORMAL) = ALL                      
    % ESE(PUNCH,PEAK) = ALL %element strain energies (for compliance optimization)
    % GPFORCE(PUNCH,REAL) = ALL
    % GPSTRAIN(PUNCH,GLOBAL,ALL) = ALL
    % STRAIN(SORT1,PUNCH,REAL,ALL) = ALL
    % STRESS(SORT1,PUNCH,REAL,ALL,CENTER) = ALL

    %step 1 get path to fem
    path_new_fem = 'INSERT FILEPATH TO FEM FILE HERE';
    %step 2 get path to optistruct
    loc_opti = 'C:\Program Files\Altair\2017\hwsolvers\scripts\optistruct.bat';
    %step 3 send to solver using system() or dos()
    command_string1 = char(strcat(''',loc_opti,'' '',path_new_fem,'' -monitor'));
    answer = system(command_string1);
    %step 4 get path of the punch file
    punch_path = 'INSERT FILEPATH TO PUNCH FILE';
    %step 5 read the punch file
    PCH = reading_punch(punch_path);

     

    Using this code, matlab will run optistruct. One thing I have not included is the reading_punch code, which reads a punch file output from NASTRAN/OS. If you need this let me know, however I am certain punch file readers exist for matlab online already.

     

    If you are going to do it this way, you will need many other scripts, including one which will take your input variables calculated by matlab and write them to an FEM file. The punch reader can be used to read outputs from OS to matlab such as stress etc. What you will need is heavily dependent on what you are trying to do.

     

    Thanks a lot for your help. I managed to do it the way you suggested.

  • FaroukM
    FaroukM Altair Community Member
    edited July 2020

    An example :

                   cmd_str = [''C:\Program Files\Altair\2020\hwsolvers\scripts\optistruct.bat'',' ',''Job.fem''];
                   system(cmd_str);

     

    Good luck

  • Adeline War_20893
    Adeline War_20893 Altair Community Member
    edited April 2022

    Hi Alex,

     

    Were you able to solve this? I want to run a code that changes the .fem file, calls optistruct to run the linear analysis and checks the compliance. After that it will repeat the process till it reaches convergence. How do you get matlab to run optistruct?

     

    Thanks,

    Rodrigo

    Were you able to execute this?

  • Adeline War_20893
    Adeline War_20893 Altair Community Member
    edited April 2022
    Crashphys said:

    Hi,

     

    This is very complicated but can be done:

     

    %uploading a .fem to optistruct and reading punch file using matlab

    %step 0 add punch card request(s) at the top of the file
    % DISPLACEMENT(SORT1,PUNCH,REAL,NORMAL) = ALL                      
    % ESE(PUNCH,PEAK) = ALL %element strain energies (for compliance optimization)
    % GPFORCE(PUNCH,REAL) = ALL
    % GPSTRAIN(PUNCH,GLOBAL,ALL) = ALL
    % STRAIN(SORT1,PUNCH,REAL,ALL) = ALL
    % STRESS(SORT1,PUNCH,REAL,ALL,CENTER) = ALL

    %step 1 get path to fem
    path_new_fem = 'INSERT FILEPATH TO FEM FILE HERE';
    %step 2 get path to optistruct
    loc_opti = 'C:\Program Files\Altair\2017\hwsolvers\scripts\optistruct.bat';
    %step 3 send to solver using system() or dos()
    command_string1 = char(strcat(''',loc_opti,'' '',path_new_fem,'' -monitor'));
    answer = system(command_string1);
    %step 4 get path of the punch file
    punch_path = 'INSERT FILEPATH TO PUNCH FILE';
    %step 5 read the punch file
    PCH = reading_punch(punch_path);

     

    Using this code, matlab will run optistruct. One thing I have not included is the reading_punch code, which reads a punch file output from NASTRAN/OS. If you need this let me know, however I am certain punch file readers exist for matlab online already.

     

    If you are going to do it this way, you will need many other scripts, including one which will take your input variables calculated by matlab and write them to an FEM file. The punch reader can be used to read outputs from OS to matlab such as stress etc. What you will need is heavily dependent on what you are trying to do.

    I was able to connect matlab and optistruct but now I want to internally make changes in the parameters within optistruct. How do I do it? Can I code directly into .fem file?

  • PaulAltair
    PaulAltair
    Altair Employee
    edited April 2022

    I was able to connect matlab and optistruct but now I want to internally make changes in the parameters within optistruct. How do I do it? Can I code directly into .fem file?

    Hi, yes, you can, the .fem is an ascii file so you can edit the input in it in text form, all the input syntax is in the help documentation

    Depending on what you are doing/trying to do, an alternative is to couple Matlab and Optistruct using a 'DRESP3' in Optistruct (in this case matlab would be the 'external resource' to OS)

    Also, another possible approach, Hyperworks includes a math library tool of its own (Compose) and there is a tutorial on using it this way:

    Compose/OS Tutorial

    Compose has Altair reader support too, so you can use H3d output directly and don't need to use pch output

     

  • Adeline War_20893
    Adeline War_20893 Altair Community Member
    edited April 2022

    Hi, yes, you can, the .fem is an ascii file so you can edit the input in it in text form, all the input syntax is in the help documentation

    Depending on what you are doing/trying to do, an alternative is to couple Matlab and Optistruct using a 'DRESP3' in Optistruct (in this case matlab would be the 'external resource' to OS)

    Also, another possible approach, Hyperworks includes a math library tool of its own (Compose) and there is a tutorial on using it this way:

    Compose/OS Tutorial

    Compose has Altair reader support too, so you can use H3d output directly and don't need to use pch output

     

    I need to use the .fem file for its element connectivity but I want to change the material assignment automatically by codes. How can I do it using batch mode with Matlab? I could only call the .fem file through optistruct.bat but now I would like to put some codes in it to export certain fields and also update my material assignment received from optimization done in Matlab. 

     

    Kindly please help me with the steps to follow here?

  • PaulAltair
    PaulAltair
    Altair Employee
    edited April 2022

    I need to use the .fem file for its element connectivity but I want to change the material assignment automatically by codes. How can I do it using batch mode with Matlab? I could only call the .fem file through optistruct.bat but now I would like to put some codes in it to export certain fields and also update my material assignment received from optimization done in Matlab. 

     

    Kindly please help me with the steps to follow here?

    I do not know matlab, so I am not sure how you set it up on that side.

    The Material Assignment in Optistruct is done via the property referred to by the elements

    e.g. below, 2 PSHELL props, numbered 1 and 2 (blue field), both reference same material (ID 1, green field) The material card is shown below with it's ID (1) highlighted in green

    image

    Then, for the individual elements, the highlighted field here, is the Property the elements use, (1 or 2, highlighted in Yellow)

    image

    So, in this case to update the material on given elements, you could e.g. have PSHELL #3 referencing Material #2 and change the PID field on the elements from 1 or 2, to 3

    For 'field export' from OS that you refer to, if the value you need can be defined as a response, then you could create DRESP1 for them and add them to a DREPORT?