CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   systemCall for defined time steps (http://www.cfd-online.com/Forums/openfoam/88031-systemcall-defined-time-steps.html)

canfield May 6, 2011 05:17

systemCall for defined time steps
 
I have been looking for a way to modify the alpha field during a simulation and stumbled across systemCall. It allows you to execute functions at the end of an iteration step, at a write interval and/or at the end of the simulation. I would like to be able to specify explicitly at which time steps the desired functions should be executed. Maybe someone has had some experience with systemCall and help me out?

Thanks in advance

n.b. I am using OF 1.7.x

wyldckat May 6, 2011 14:13

Greetings Peter,

Does this page help: Tip Function Object systemCall @ openfoamwiki.net ?

Best regards,
Bruno

canfield May 6, 2011 16:26

Thanks for the link, Bruno. Unfortunately, I had already scoured that page for additional information and haven't got any further. It basically says what i posted initially; a function can be called either before an iteration, before a write interval, or just before the end of the simulation. I would like to be able to use systemCall between write intervals, but not as often as every iteration.

Other hints would be very welcome!

Peter

wyldckat May 7, 2011 03:39

Hi Peter,

OK, now read again the chapter: Usage Description
Back when I first wrote that page, I didn't fully test all of it's options...

Either way, the new information sums up to using these options together:
Code:

outputControl timeStep;
outputInterval 2;

Quote:

Originally Posted by http://openfoamwiki.net/index.php/Tip_Function_Object_systemCall#Usage_Description
outputInterval is an integer/scalar value of the same type as controlDict's writeInterval.

edit: "outputInterval 2" means that it executes at every 2 iterations. So you'll have to do a bit of math if you want to output between two write times. Things will get a bit strange if the solver uses adaptive time steps...

I've also fixed the other sibling page "Tip Function Object writeRegisteredObject", in which I had wrongly categorized outputInterval but now it's fixed.

Best regards,
Bruno

canfield May 7, 2011 03:54

Bruno, you are amazing! This is indeed very good news. Thank you for opening my eyes to that option.

All the best

matthi June 9, 2011 09:47

Function "executeCalls"
 
Hello Bruno,

thanks for the helpful informations about the system calls. This tool is very helpful for me, because I want to call a fortran Program externally.
I tried the approach given in the wiki page to test the availability:

functions
{
sysCall
{
type systemCall;
functionObjectLibs ( "libsystemCall.so" );
executeCalls 1("echo Execute system call before time iteration is done");
endCalls 1("echo Finishing up with a system call, which is seems to be before the write call...");
writeCalls 2("echo Writing to file call" "ls -l");
outputControl outputTime;
}
}

It works ... but the command "executeCalls 1" shows NO echo line on the terminal ! The other comand lines work properly.
Maybe this has something to do with my version ? I use version 1.7.1.

Thanks in advance

Matthi

wyldckat June 9, 2011 16:15

Hi Matthias and welcome to the forum!

Well, you could have said which solver you were using ;)
But I suspect the issue you're seeing is quite simple and I'm going to quote with bold letters on the two critical words:
Quote:

echo Execute system call before time iteration is done
The solver you are using most probably doesn't use time iterations! I have a vague memory of seeing a solver that does explicit iterations instead of time iterations, but I can't remember which solver it was :(

Best regards,
Bruno

matthi June 10, 2011 03:15

Hello Bruno,

thanks a lot for the fast answer. You`re completely right. I should read all items more carefully. I tried it with the solver rhoSimpleFoam (steady state) and therefore it didn`t work.

I thought about my approach again and I will try to call the external Fortran program and several system calls (copy of files and so on) directly inside the solver. This gives me more freedom to position and handle the calls. But nevertheless the option of using your functions in controldict is a very valuable tool.

Thank you again ;););)

Greetings Matthias

wyldckat June 10, 2011 12:58

Hi Matthias,
Quote:

Originally Posted by matthi (Post 311397)
But nevertheless the option of using your functions in controldict is a very valuable tool.

To be clear, I didn't make those functions, I simply documented them since about these there was pretty much no information, except having to look into the code, tinkering with the options and figuring out how things work!

But indeed, an option for any iterations, not just time iterations, would be a nice feature. I wonder if I that option already exists and I didn't see it...

Best regards,
Bruno

wyldckat June 10, 2011 13:53

Hello again Matthias,

Hold on, something is amiss... I just ran the tutorial tutorials/compressible/rhoPorousSimpleFoam/angledDuctImplicit with the example from the wiki page and it showed me this:
Code:

Starting time loop

Time = 1

DILUPBiCG:  Solving for h, Initial residual = 0.567508, Final residual = 0.021675, No Iterations 2
GAMG:  Solving for p, Initial residual = 1, Final residual = 0.04149, No Iterations 6
time step continuity errors : sum local = 5.65325, global = -0.302944, cumulative = -0.302944
rho max/min : 1.22254 1.1863
smoothSolver:  Solving for epsilon, Initial residual = 0.178745, Final residual = 0.00240625, No Iterations 2
smoothSolver:  Solving for k, Initial residual = 1, Final residual = 0.0389567, No Iterations 2
ExecutionTime = 0.27 s  ClockTime = 1 s

Execute system call before time iteration is done
Time = 2

DILUPBiCG:  Solving for h, Initial residual = 1, Final residual = 0.0359937, No Iterations 2
GAMG:  Solving for p, Initial residual = 0.409515, Final residual = 0.019402, No Iterations 4
time step continuity errors : sum local = 11.5033, global = -1.95711, cumulative = -2.26005
rho max/min : 1.58949 1.1863
smoothSolver:  Solving for epsilon, Initial residual = 0.0863096, Final residual = 0.00417895, No Iterations 4
smoothSolver:  Solving for k, Initial residual = 0.291302, Final residual = 0.0239134, No Iterations 4
ExecutionTime = 0.44 s  ClockTime = 1 s

Execute system call before time iteration is done
Time = 3

DILUPBiCG:  Solving for h, Initial residual = 0.927538, Final residual = 0.041592, No Iterations 2
(...)

The sentence "Execute system call before time iteration is done" does show up!

That solver should be very similar to rhoSimpleFoam, so it should work as intended!


And I also just tested tutorials/stressAnalysis/solidDisplacementFoam/plateHole, which seems to be a strict iteration only solver and it showed this:
Code:

Calculating displacement field

Iteration: 1

GAMG:  Solving for Dx, Initial residual = 1, Final residual = 0.47337, No Iterations 2
GAMG:  Solving for Dy, Initial residual = 0, Final residual = 0, No Iterations 0
ExecutionTime = 0.02 s  ClockTime = 0 s

Execute system call before time iteration is done
Iteration: 2

GAMG:  Solving for Dx, Initial residual = 0.0217996, Final residual = 0.00712496, No Iterations 1
GAMG:  Solving for Dy, Initial residual = 1, Final residual = 0.443501, No Iterations 1
ExecutionTime = 0.03 s  ClockTime = 0 s

Execute system call before time iteration is done
Iteration: 3

The sentence shows up just the same!

Sooo... something is not right! I think it should have worked as intended for you as well!

Best regards,
Bruno

matthi June 15, 2011 09:37

Hi Bruno,

sorry for my late answer. I was out of office. Thanks or your effort. I tried it again with rhoSimpleFoam and maybe I solved the secret. Here my entry in controldict:

functions
{
sysCall
{
type systemCall;
functionObjectLibs ( "libsystemCall.so" );
executeCalls 1("echo Execute system call before time iteration is done");
endCalls 1("echo Finishing up with a system call, which is seems to be before the write call...");
outputControl outputTime;
}
}

Here the output for only ONE iteration (this is what I did last week):

Starting time loop
Time = 3611
smoothSolver: Solving for Ux, Initial residual = 0.0171106, Final residual = 0.000317189, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.074749, Final residual = 0.00156304, No Iterations 2
smoothSolver: Solving for Uz, Initial residual = 0.02481, Final residual = 0.00060317, No Iterations 2
DILUPBiCG: Solving for h, Initial residual = 0.0148062, Final residual = 0.00114063, No Iterations 4
GAMG: Solving for p, Initial residual = 0.81745, Final residual = 0.0212307, No Iterations 2
time step continuity errors : sum local = 1.91384, global = -0.00727263, cumulative = -0.00727263
rho max/min : 1.15865 1.15098
ExecutionTime = 9.37 s ClockTime = 12 s

Finishing up with a system call, which is seems to be before the write call...
End

THE SENTENCE IS NOT HERE !

Here the output for TWO iterations:

Starting time loop
Time = 3612
smoothSolver: Solving for Ux, Initial residual = 0.0143379, Final residual = 0.000299003, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.0575645, Final residual = 0.00140563, No Iterations 2
smoothSolver: Solving for Uz, Initial residual = 0.0187994, Final residual = 0.000524105, No Iterations 2
DILUPBiCG: Solving for h, Initial residual = 0.0147228, Final residual = 0.000794698, No Iterations 4
GAMG: Solving for p, Initial residual = 0.85305, Final residual = 0.0210801, No Iterations 2
time step continuity errors : sum local = 2.05708, global = -0.00686541, cumulative = -0.00686541
rho max/min : 1.15865 1.15098
ExecutionTime = 10.07 s ClockTime = 12 s


Execute system call before time iteration is done

Time = 3613
smoothSolver: Solving for Ux, Initial residual = 0.0115322, Final residual = 0.000258155, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.0431911, Final residual = 0.0011706, No Iterations 2
smoothSolver: Solving for Uz, Initial residual = 0.0140338, Final residual = 0.000416262, No Iterations 2
DILUPBiCG: Solving for h, Initial residual = 0.0110091, Final residual = 0.000334477, No Iterations 4
GAMG: Solving for p, Initial residual = 0.840982, Final residual = 0.0209793, No Iterations 2
time step continuity errors : sum local = 2.13375, global = -0.00501545, cumulative = -0.0118809
rho max/min : 1.15865 1.15098
ExecutionTime = 15.11 s ClockTime = 19 s

Finishing up with a system call, which is seems to be before the write call...
End


Here you can find the message just right befor the second time information is written. A little bit strange, but it seems, that it works not right for only on iteration.
I will not investigate it furthermore, because I search for another way. If it works, I will post it here.

Many Greetings

Matthias

wyldckat June 15, 2011 15:57

Hi Matthias,

Ah, now I understand the logic behind this function object:
  • Before the first iteration is when the solver starts, so it's not very useful to call the shell right after the solver starts.
  • executeCalls is executed before the next (time) iteration.
  • endCalls is executed instead of executeCalls, after the last iteration. This is because after the final iteration, it's very likely that one might not want to do exactly the same task as in executeCalls; and if one does want it so, simply repeat the same list from executeCalls!
I'm going to update the wiki page to reflect this reasoning :)

Edit: OK, I'm a bit slow today... You needed it to run after writing the fields. Mmm, then you need writeCalls, not either one of the other two!

Best regards,
Bruno

HakikiCanakkaleli November 23, 2012 13:53

Hi,

I also try to use systemCall to couple an external executable file with OpenFOAM ( a small C++ code which also reads and writes some text files inside the case file).

However, I receive this error:

Code:

sh: 1: ./cwind: Permission denied
Nevertheless, in the same run, I also obtain:

Code:

allowSystemOperations : Allowing user-supplied system call operations
which shows me that OpenFOAM allows me to use sort of C++ code.

My systemCall function in the controlDict is as follows:

Code:

functions
{
    sysCall
    {
        type systemCall;
        functionObjectLibs ( "libsystemCall.so" );
        executeCalls 1( "./cwind" );
        endCalls 0();
        writeCalls 0();
        outputControl timeStep;
        outputInterval 1;
    }
}

I kindly ask any help which may show where I possibly do a mistake.

During that time, if I could find an answer I will post its explanation.

Many thanks in advance and millions espresso.

gschaider November 23, 2012 14:07

Quote:

Originally Posted by HakikiCanakkaleli (Post 393875)
Hi,

I also try to use systemCall to couple an external executable file with OpenFOAM ( a small C++ code which also reads and writes some text files inside the case file).

However, I receive this error:

Code:

sh: 1: ./cwind: Permission denied
Nevertheless, in the same run, I also obtain:

Code:

allowSystemOperations : Allowing user-supplied system call operations
which shows me that OpenFOAM allows me to use sort of C++ code.

My systemCall function in the controlDict is as follows:

Code:

functions
{
    sysCall
    {
        type systemCall;
        functionObjectLibs ( "libsystemCall.so" );
        executeCalls 1( "./cwind" );
        endCalls 0();
        writeCalls 0();
        outputControl timeStep;
        outputInterval 1;
    }
}

I kindly ask any help which may show where I possibly do a mistake.

During that time, if I could find an answer I will post its explanation.

Many thanks in advance and millions espresso.

The problem is on the OS level not OF: seems like the shell tried to execute your script but couldn't. My two guesses:
- cwind is not executable
- the current working directory is different than what you're expecting and the shell doesn't even find the util

HakikiCanakkaleli November 23, 2012 14:39

Hi,

Thanks for your answer.

I think you are right. I also consider what you said beforehand and tried to solve it.

Even though I am not an expert on Linux, it is obvious that the problem stems from the OS itself.

I have made the changes in the following:

1. I edited "sudoers" file in order to eliminate the OS functionality which asks password for any execution of any C++ executable script.

a. First, select editor by inputting the following command in the terminal:

Code:

sudo select-editor
b. Second, select "nano" editor because its usage is the simpliest.

c. Third, launch editor by inputting the following command in the terminal
Code:

sudo visudo
d. If you want to have access to all programs without password add this line in the relevant line: (as far as I concern, the sequence of the commands in the "sudoers" file is important; therefore, please be careful and do double check.
Code:

%admin    ALL = NOPASSWD: ALL
e. To save the file: use CONTROL+X. (It took this for me at least half an hour.)

2. In the case folder, please also pay attention to the "folder access". Some does not let the user to create or delete files inside a folder. So, make sure that everything is appropriate in the folder "Permissions" properties.

I expected everything would be fine; but, I observed that I have had issues and systemCall didn't work properly.

3. Then I realized that there is no gap between the execution of the external program and OpenFOAM solver - the relevant time iteration. Hence, I have modified the systemCall accordingly by adding 1 second between the start of execution of the external program and OpenFOAM time step:

Code:

functions
{
    sysCall
    {
        type systemCall;
        functionObjectLibs ( "libsystemCall.so" );
        executeCalls 1( "./cwind" );
        endCalls 0();
        writeCalls 1("sleep 1");
        outputControl timeStep;
        outputInterval 1;
    }
}

This does not give the exact picture, though. There would be some hidden things that I couldn't appreciate yet worked for me. Hopefully it gives some idea for whom deal with similar issues.

And thanks a lot to Bruno, without the Wiki page which is his product and Bernhard for the "sleep 1" tip in the forum somewhere, I would not end up with the coupling.

Millions Turkish coffee.

gschaider November 23, 2012 16:43

Quote:

Originally Posted by HakikiCanakkaleli (Post 393883)
Hi,

Thanks for your answer.

I think you are right. I also consider what you said beforehand and tried to solve it.

Even though I am not an expert on Linux, it is obvious that the problem stems from the OS itself.

I have made the changes in the following:

1. I edited "sudoers" file in order to eliminate the OS functionality which asks password for any execution of any C++ executable script.

a. First, select editor by inputting the following command in the terminal:

Code:

sudo select-editor
b. Second, select "nano" editor because its usage is the simpliest.

c. Third, launch editor by inputting the following command in the terminal
Code:

sudo visudo
d. If you want to have access to all programs without password add this line in the relevant line: (as far as I concern, the sequence of the commands in the "sudoers" file is important; therefore, please be careful and do double check.
Code:

%admin    ALL = NOPASSWD: ALL
e. To save the file: use CONTROL+X. (It took this for me at least half an hour.)

2. In the case folder, please also pay attention to the "folder access". Some does not let the user to create or delete files inside a folder. So, make sure that everything is appropriate in the folder "Permissions" properties.

I expected everything would be fine; but, I observed that I have had issues and systemCall didn't work properly.

3. Then I realized that there is no gap between the execution of the external program and OpenFOAM solver - the relevant time iteration. Hence, I have modified the systemCall accordingly by adding 1 second between the start of execution of the external program and OpenFOAM time step:

Code:

functions
{
    sysCall
    {
        type systemCall;
        functionObjectLibs ( "libsystemCall.so" );
        executeCalls 1( "./cwind" );
        endCalls 0();
        writeCalls 1("sleep 1");
        outputControl timeStep;
        outputInterval 1;
    }
}

This does not give the exact picture, though. There would be some hidden things that I couldn't appreciate yet worked for me. Hopefully it gives some idea for whom deal with similar issues.

And thanks a lot to Bruno, without the Wiki page which is his product and Bernhard for the "sleep 1" tip in the forum somewhere, I would not end up with the coupling.

Millions Turkish coffee.

What I don't fully understand: is the problem solved now? Anyway. I'm qutie sure that step 1 (editing the sudoers file) has nothing to do with it. What I meant with "permission problem" is that the execution bit of the script file is not set (that would generate the error message you reported). Best way to check that is to try to execute it "by hand" (==from the shell). If that doesn't work check the man-page for chmod

HakikiCanakkaleli November 23, 2012 18:40

The problem seems to be solved now. I also checked from the shell whether the external program works or not and it worked well. Now, considering your message, I will reconsider the reason behind why it did not work properly.

matthi November 27, 2012 10:43

Hi,

I also don`t understand the problem fully. But anyway, there is another possibility to launch an external program or to use system calls.
This might be easier than defining an own function.

If you want to call a programm named "test" in a routine you can use: system(./test);

If the program is NOT in the case folder, then you have to change the directory. E.G. the program is in the subfolder "system". The you could use the command: system("cd system; ./test; cd ..");

Hope this helps.

Greetings Matthi


All times are GMT -4. The time now is 15:00.