Define variables inside a loop

I find it quite limiting that the Teddy language does not allow matrix definitions but is limited to lists.

I’ve been trying to do a simple workaround by defining several variables and formatting it as a matrix:

!           col0    col1    col2    col3
let#LC1     1.1,    1.2,    1.3,    1.4
let#LC17    2.1,    2.2,    2.3,    2.4
let#LC23    3.1,    3.2,    3.3,    3.4

let#LCs 1,17,23

etc…

When I call this inside eg. Sofiload I’d do like this:

loop#i LC1
    COMB 801+#i EXTR stan TYPE DESI
        LC 1 TYPE g #LC1(#i)
        LC 17 TYPE g #LC17(#i)
        LC 23 TYPE g #LC23(#i) 
    SUM 801+#i LC 801+#i TITL 'Comb #i'
endloop

Instead, I would like to call it through strings like this:

loop#i LC1
    loop#j LCs
        COMB 801+#i EXTR stan TYPE DESI
            LC #LCs(#j) TYPE g '#LC'#LCs(#j)(#i)    ! for #i and #j = 0: '#LC' --> #LC,  #LCs(0) --> 1 => #LC1(0) --> 1.1 
        SUM 801+#i LC 801+#i TITL 'Comb #i'
    endloop
endloop

Is this in anyway possible, or are anyone aware of a different solution?

Kind regards
Martin

Hi, Martin.

As for me, it is not always convenient to do complex code structures on Teddy. For example, I use Qt/C++ and VBA in Excel to generate code for Teddy. Someone is using Python.
It is even convenient to preview the model there, because there are libraries for 3D visualization.

I do use Excel to generate code for teddy as well, but it still requires that teddy is able to do the operations I’m preparing.

I don’t see the purpose of preparing a matrix code in eg. Python, as Teddy wouldn’t be able to read it anyway?

As I understand it, the nested loops in your example will turn into a set of strings. You would like to use matrices for convenience. Perhaps I do not understand something.
What I meant is that we end up with a set of text strings for Teddy. Why not create it using some programming language?

Correct, I’m using the set of springs as an alternative to matrices.

I could do it in another programming language, I’d just like to avoid having an extra interface and do everything directly in Teddy.
Unless there is some API in teddy which allows me to call that other programming language so I don’t have to copy the text back and forth every time I need to change something.

OK, understood. The last thing I can say is to use #include in places of potential frequent changes. The Update button in Excel will rewrite the information into an attached file (for example, nodes.dat, with the coordinates of the nodes). The file itself will be included in the SOFIMSHA module. Thus, changes need to be made in just one place.

Perhaps more experienced users will suggest something more suitable

Use your preferred language to generate a text file (myfile.dat) with e.g.
Let#row1 11,12,13
Let#row2 21,22,23
Let#row3 31,32,33

Then include the result in teddy/ssd:
#include myfile.dat

Att! The file must be calculated when you press calculate in sofistik.

If you want something more dynamic, you can do the same thing using +apply, but this requires a program, e.g.:
The File:
+Prog Template
Sto#row1 11,12,13
Sto#row2 21,22,23
Sto#row3 31,32,33
End

In SSD/teddy:
+Apply myfile.dat

The difference between the two approaches is:

  • Include: File is included once you press calculate (i.e. can not be generated during the calculation run by calling external scripts)
  • Apply: File is executed on the fly, but must contain a sofistik program (e.g. template) to be run
2 Likes

Okay that seems promising, I’ll have to look into that. Thanks

So… I’ve tried the file excerpts you’ve proposed, and it works fine with the #Include file, but I can’t get SSD to run the file when I use the +Apply file. What am I doing wrong?
Apply_test.dat file:

+Prog Template
    Sto#row1 11,12,13
    Sto#row2 21,22,23
    Sto#row3 31,32,33
End

Mainfile:

+Apply Apply_test.dat

+prog template urs:20.1
    head DAT
    prt#row1
END

Furthermore, am I understanding the “dynamic” part correctly?

What I would like to do is to use eg. Python to create a .dat file, and the +apply that .dat file in SSD, with a connection back to Python. But looking at your code excerpt, I don’t see a connection between the .dat file and the program that generates it?

I’ve created a small python program to tell time and print it to a .dat file.

Time.py:

import datetime


with open('Time.dat', mode='w') as f:
    print('+PROG TEMPLATE\n' 'let#time \'',datetime.datetime.now(),'\'' '\nEND', file=f, sep='')

This creates the following file.

Time.dat:

+PROG TEMPLATE
let#time '2021-04-13 10:59:26.103753'
END

However, reading the isolated .dat file, I don’t see how SSD would know the file is generated in Python and thereby execute the python script to update the file?

  1. Get apply working:
    Works for me when I copy/paste
    Are the files in the same folder?
    Are there typos?
    If you have a space you should use quotes “Apply test.dat”

  2. To execute something in sofistik you need to use +sys (command line access)
    So if your regular execution command is:
    python myfile.py
    It becomes:
    +sys python myfile.py
    So now your mainfile should be:
    +sys python myfile.py returning myresult.dat
    +apply myresult.dat
    Rest of code here

In case you want to do something more complicated that +sys can’t handle (several lines to the console), you write that in a .bat script and run the .bat script.

1 Like

But should the +apply be inside a program?

I’ve tried both with and without, but I can’t seem to get it to work.

My exact SSD code is this:

+Apply Apply_test.dat

+prog template urs:20.1
    head DAT
    prt#row1
end

No, it is outside of programs.

Everything with a + is a “program” in itself:
+Apply: include this when you get here
+sys: run this system command
+Prog “sofistikprogram”: run this sofistikprogram with the following input.

The only difference between +apply and #include is:

  • #include: “paste this text here” (even if text in a file)
  • +appyl: “run this .dat file” (i.e. the file should contain whole programs, e.g. +prog template)
1 Like

so, the only thing I now have in SSD is a single process with the following line of code:

+Apply Apply_test.dat   

The Apply_test.dat file consists of the following piece of code:

+Prog Template
Sto#row1 11,12,13
Sto#row2 21,22,23
Sto#row3 31,32,33
End

When I run the process in SSD I get the following error:

Parser error: Rekursives include.
Line (31): +Apply Apply_test.dat

oh well. the Apply test doesn’t work, but atleast I got it to communicate with python.

Thanks a lot!

Python file:

import datetime

with open('Time.dat', mode='w') as f:
    print(
'+PROG TEMPLATE\n'\
'Head Python process\n'\
'sto#time \'',datetime.datetime.now(),'\ \n'\
'END', file=f, sep='')

SSD process:

+sys python time.py
+apply time.dat

+prog template urs:20.1
Head Print from SSD
prt#time
end
1 Like