Page 1 of 1

MATC: if-elseif or select?

Posted: 07 May 2012, 15:29
by mzenker
Hi,

just one short question: Is there any such thing as a select, if-elseif or nested if-else control structure in MATC?
The manual says that there is only if-else, without mentioning a nesting possibility.

Thanks,

Matthias

Re: MATC: if-elseif or select?

Posted: 07 May 2012, 22:19
by NJank
I believe there's only if-else, but you can nest them. I've used the following as an enthalpy definition, it seems to work as expected. (note most of those except tx are MATC variables defined eariler in the sif file, tx is the temperature variable).

Code: Select all

  Enthalpy = Variable Temperature
    MATC "if (tx<=T1) (cpvs*tx); else if (tx>=T2) ((tx-Tm)*cpvl+Tm*cpvs+Lh); else (P1*tx+P0);"
Also, in case anyone wonders, as an enthalpy formula this uses a simple linear equation for the final else state, which would be much more efficiently set just using a lookup table.

I'm not sure how line splitting works for MATC within a sif file, that would make it more legible for complex structures. someone else may want to comment on that.

Re: MATC: if-elseif or select?

Posted: 08 May 2012, 09:04
by mzenker
Thank you!

Re: MATC: if-elseif or select?

Posted: 06 Apr 2019, 00:03
by kate.hruby
Hi,
I realize I'm resurfacing a pretty old thread, but this has been a huge help for me, so maybe there is still someone around who can answer this!

Within MATC if statements, is there a way to set greater than and less than bounds in the same expression?

Say my tx is a Variable Coordinate from 0 to 10, and I want a value applied from 2 to 8 (and a different value from 0-2 and 8-10).
I've tried: Real MATC "if (2 < tx < 8) 0.01; else 1.0"
And: Real MATC "if ((tx+2) < 8) 0.01; else 1.0"

But nothing seems to work. Is there something I can do with &, or some other creative way to go about it?

Thank you!
Kate

Re: MATC: if-elseif or select?

Posted: 06 Apr 2019, 02:42
by kevinarden
check out MATC general flow control structures

in the MATC manual

http://www.nic.funet.fi/pub/sci/physics/elmer/doc/

Re: MATC: if-elseif or select?

Posted: 06 Apr 2019, 02:48
by kevinarden
"Say my tx is a Variable Coordinate from 0 to 10, and I want a value applied from 2 to 8 (and a different value from 0-2 and 8-10).
I've tried: Real MATC "if (2 < tx < 8) 0.01; else 1.0"
And: Real MATC "if ((tx+2) < 8) 0.01; else 1.0"

In general you would have to nest several if statements that overwrite the values

If tx < 2 then tx = .01
if tx >= 2 then tx = 1.0
if tx < 8 then tx = .01
if tx >= 8 then tx = 1.0

basically you over write the variable with each if statement

Kevin

Re: MATC: if-elseif or select?

Posted: 06 Apr 2019, 12:51
by kevinarden

Re: MATC: if-elseif or select?

Posted: 07 Apr 2019, 13:31
by NJank
What's the LUA equivalent construct?

Re: MATC: if-elseif or select?

Posted: 12 Apr 2019, 19:00
by kate.hruby
Rad, thanks!

The code that works is:

Real MATC "if (tx < 2) 1.0; if (tx > 8) 1.0; else 0.01"