CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM News & Announcements > OpenFOAM Announcements from Other Sources

Debugging OpenFOAMŪ with Visual Studio Code

Register Blogs Community New Posts Updated Threads Search

Like Tree28Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 6, 2020, 15:57
Default Debugging OpenFOAMŪ with Visual Studio Code
  #1
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Hello All

I have created a guide which will help in setting up Visual Studio Code for OpenFOAM debugging and development.

https://github.com/Rvadrabade/Debugg...al-studio-code

Suggestions are welcome.

Thank you.
Rahul Vadrabade
elvis, JonW, frantov and 9 others like this.
Rvadrabade is offline   Reply With Quote

Old   August 17, 2020, 00:50
Default
  #2
Member
 
Francisco T
Join Date: Nov 2011
Location: Melbourne, Australia
Posts: 64
Blog Entries: 1
Rep Power: 15
frantov is on a distinguished road
Thanks for this guide.
Are you aware of this?
https://marketplace.visualstudio.com...scode-openfoam
Rvadrabade and HPE like this.
frantov is offline   Reply With Quote

Old   August 17, 2020, 03:26
Default
  #3
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Yes, I am aware about it. Great work. However, I did not get time to use extension. I will try and surely give you feedback. Looking forward for next update and features.
frantov likes this.
Rvadrabade is offline   Reply With Quote

Old   April 30, 2021, 00:25
Default
  #4
New Member
 
FOAMraj
Join Date: Apr 2021
Posts: 19
Rep Power: 5
BIRAJ is on a distinguished road
Dear Rvadrabade,
I have encountered some errors regarding the fourth step. Here is the image:

https://imgur.com/EUc0s51

I have done the third step and I did pretty well.

Thank you
BIRAJ is offline   Reply With Quote

Old   April 30, 2021, 01:00
Default
  #5
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Hi Biraj,

It is not error as such. In general, to debug any executable you have to build the source code with debug flags. Openfoam default ships the optimised code for faster response ( WM_COMPILE_OPTION=Opt) not debug build. So, we have to build the source code with debug flags which is automatically set with WM_COMPILE_OPTION=Debug (step 3) and just execute ./Allwmake from main directory (Note that compilation might take several hours depending on your hardware). Now, all the built solvers have debug symbols which will help to debug executable.

I hope this helps you.
Thanks
Rvadrabade is offline   Reply With Quote

Old   April 30, 2021, 03:17
Default
  #6
New Member
 
FOAMraj
Join Date: Apr 2021
Posts: 19
Rep Power: 5
BIRAJ is on a distinguished road
Dear Rvadrabade,
I have already done step3 as you have mentioned. Here is the proof:

https://imgur.com/YwlmiFh

After this step, the procedure is hard for me to understand.
I tried to run Allwmake and got this error.
biraj@DESKTOP-3PJCARR:~/OpenFOAM/OpenFOAM-v2006$ ./Allwmake
gcc=/home/biraj/OpenFOAM/ThirdParty-v2006/platforms/linux64/gcc-6.3.0/bin/gcc
clang=
mpirun=/home/biraj/OpenFOAM/ThirdParty-v2006/platforms/linux64Gcc63/openmpi-4.0.3/bin/mpirun
make=
cmake=
wmake=/home/biraj/OpenFOAM/OpenFOAM-v2006/wmake/wmake
m4=
flex=

compiler=unknown

========================================
2021-04-30 11:57:50 +0545
Starting compile OpenFOAM-v2006 Allwmake
Gcc63 ThirdParty compiler
linux64Gcc63DPInt32Debug, with OPENMPI openmpi-4.0.3
========================================

/home/biraj/OpenFOAM/OpenFOAM-v2006/wmake/src/Allmake: 35: make: not found
PS: The fourth step is unclear for me. What does that mean? How can I append?

Thank you
BIRAJ is offline   Reply With Quote

Old   April 30, 2021, 10:27
Default
  #7
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Try following commands
sudo apt-get update
sudo apt-get install build-essential flex bison cmake zlib1g-dev libboost-system-dev libboost-thread-dev libopenmpi-dev openmpi-bin gnuplot libreadline-dev libncurses-dev libxt-dev
sudo apt-get install qt4-dev-tools libqt4-dev libqt4-opengl-dev freeglut3-dev libqtwebkit-dev
sudo apt install make
Rvadrabade is offline   Reply With Quote

Old   May 15, 2021, 21:18
Default
  #8
Member
 
Join Date: Feb 2020
Posts: 90
Rep Power: 6
Shibi is on a distinguished road
Hi,


Is it possible to debug code that is running in parallel?
Shibi is offline   Reply With Quote

Old   May 16, 2021, 00:58
Default Debugging OpenFoam I parallel..
  #9
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Hi,

Well, quick answer is NO for a built-in solver with vscode. However, workaround is to modify solver to add sufficient pause so that all the thread can be attached to vscode seperately(like .https://openfoamwiki.net/index.php/H...dditional_Info). But if the number of processors increases it make difficult to do this way.

Further, non vscode workaround is to use the mpirunDebug script (https://openfoamwiki.net/index.php/H...ng#mpirunDebug).

Also, let the community know if you found better way to debug Openfoam in parallel.
Rvadrabade is offline   Reply With Quote

Old   May 16, 2021, 15:46
Default
  #10
Member
 
Join Date: Feb 2020
Posts: 90
Rep Power: 6
Shibi is on a distinguished road
Hi again,

So, this is based on several contributions and some peer review of the procedure would be highly appreciated.

If you have suggestions to make the procedure better, feel free to do so.

Procedure:

1š -> Check this video: https://www.youtube.com/watch?v=lTt02xuD51w

2š -> Here are my .vscode files with some modifications

c_cpp_properties.json
Code:
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${FOAM_SRC}"
            ],
            "defines": [],
            "compilerPath": "mpic++",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
launch.json
Code:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "OF-Debug",
            "type": "cppdbg",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "program": "${env:FOAM_USER_APPBIN}/${fileBasenameNoExtension}",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "wmake-build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
tasks.json
Code:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "wmake-build",
            "command": "wmake",
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: cpp build active file",
            "command": "/usr/bin/cpp",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /usr/bin/cpp"
        }
    ]
}
3š -> Somewhere in your code (wherever it makes sense) set an infinite loop. E.g.,
Code:
int myi = 0; 

while (0 == myi) 

    Foam::sleep(5);
4š -> Open a terminal window and compile the code with:
Code:
wmake mySolverFoam
5š -> Execute your solver in parallel. It will get stuck in your infinite loop

Code:
decomposePar && mpirun -np 2 mySolverFoam -parallel
6š -> In vsCode, go to view -> Command Palette... and type Workspaces: Duplicate As Workspace in New Window. A new vsCode window will appear. We will be using both for setting a debugger into processor0 and processor1.

7š -> On your first vsCode window, open your mySolverFoam.C file and go to the Run and Debug tab (control + shift + D). On the top left corner click on Start Debugging (green play button).

8š -> After clicking on Start Debugging, look for your solver name, in this case
mySolverFoam. Two processes will appear. Choose one for your first vsCode window. It will ask you if you want to attach the debugger to the process. Type y.

9š -> Perform steps 7 and 8 and choose the second process for the second window.

10 -> Now you are inside the solver. In your first window, press the Pause (F6) button in the debugger. An error message will appear, just press Step Out (Shift + F11) 2 or 3 times and you will be located in your infinite loop.

11š -> Now, you will need to leave the infinite loop. For that, change the value of the variable myi to 1 on the variables -> locals bar (located on the left side of the Run and Debug tab. After changing the value, press on Step Over (F10). You should be outside of you infinite loop.

12š -> Perform steps 10 and 11 on your second vsCode window. You can proceed with your normal debugging activities, but now in parallel.

Best Regards,
Michael@UW likes this.
Shibi is offline   Reply With Quote

Old   May 17, 2021, 11:12
Default Debugging OpenFoam In parallel with vscode
  #11
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Hi,

Fantastic work. Thanks for putting the efforts to list down all the steps needed to setup vscode environment which will help to debug Openfoam in parallel. These steps surely help community members a lot.

However, please note that, invest your time and efforts carefully while debugging OpenFoam in parallel unless you have good reason to do it or doing any research related to HPC. Even though Openfoam is very simple looking code for users,but the underlying implementation is considerably complex specially for parallel architecture.
Rvadrabade is offline   Reply With Quote

Old   July 15, 2021, 05:31
Default Error while setting up
  #12
New Member
 
Gaurav Gokhale
Join Date: Dec 2016
Posts: 1
Rep Power: 0
gauravgokhale is on a distinguished road
Hello,
I am getting an error "The term 'wmake' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
I followed the steps as mentioned in the guide. Can anyone suggest how to solve it?

Edit (SOLVED): I was just trying to step through the simpleFoam solver in vscode. The issue disappeared when I activated the openfoam-debug alias which I had set in bashrc and then opened vscode using "code ."

Thanks,
Gaurav Gokhale
Rvadrabade likes this.

Last edited by gauravgokhale; July 15, 2021 at 14:00.
gauravgokhale is offline   Reply With Quote

Old   January 14, 2022, 11:56
Default
  #13
New Member
 
Join Date: Dec 2021
Posts: 23
Rep Power: 4
MamboJambo is on a distinguished road
Hello to all,


Shouldn't this be posted into the OpenFOAM wiki? The configuration files are quite useful!
Rvadrabade likes this.
MamboJambo is offline   Reply With Quote

Old   January 14, 2022, 23:26
Default Openfoam wiki
  #14
Member
 
Rahul Vadrabade
Join Date: Apr 2018
Posts: 46
Rep Power: 8
Rvadrabade is on a distinguished road
Quote:
Originally Posted by MamboJambo View Post
Hello to all,


Shouldn't this be posted into the OpenFOAM wiki? The configuration files are quite useful!
Actually, it is a great suggestion. However, I am not aware of how to contribute to OpenFOAM wiki? And also if this kind of work really can be part of OpenFOAM wiki?
In case you know how can I submit this work then guide me. I will surely take it forward.

Thank you,
Rahulv
Rvadrabade is offline   Reply With Quote

Old   January 15, 2022, 12:12
Default
  #15
New Member
 
Join Date: Dec 2021
Posts: 23
Rep Power: 4
MamboJambo is on a distinguished road
Quote:
Originally Posted by Rvadrabade View Post
Actually, it is a great suggestion. However, I am not aware of how to contribute to OpenFOAM wiki? And also if this kind of work really can be part of OpenFOAM wiki?
In case you know how can I submit this work then guide me. I will surely take it forward.

Thank you,
Rahulv

Hi,

I guess through here https://openfoamwiki.net/index.php/User:Bgschaid. The pages that opens when you click on contacts. To me, your github page was more than enough to get me started: https://github.com/Rvadrabade/Debugg...al-Studio-Code.

I would just change line 12 of c_cpp_properties to be c++11 (OF standard) and possibly line 7 to include ""${FOAM_SRC}/**"


Additionally, since this will use vscode as a GUI we can use gbd command (data watchpoints) which is also quite useful!
MamboJambo is offline   Reply With Quote

Old   February 3, 2022, 05:51
Default debugging
  #16
Member
 
Join Date: Nov 2012
Posts: 83
Rep Power: 14
Henning86 is on a distinguished road
A great option to switch between opt and debug branch is to define aliases

Code:
alias of2106='source $HOME/OpenFOAM/OpenFOAM-v2106/etc/bashrc'
alias of2106_debug='source $HOME/OpenFOAM/OpenFOAM-v2106/etc/bashrc_debug'
copy the etc/bashrc to etc/bashrc_debug and change
export WM_COMPILE_OPTION=Opt
to
export WM_COMPILE_OPTION=Debug
Rvadrabade and Richal Sun like this.
Henning86 is offline   Reply With Quote

Old   February 4, 2022, 09:57
Default
  #17
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Henning86 View Post
A great option to switch between opt and debug branch is to define aliases

Code:
alias of2106='source $HOME/OpenFOAM/OpenFOAM-v2106/etc/bashrc'
alias of2106_debug='source $HOME/OpenFOAM/OpenFOAM-v2106/etc/bashrc_debug'
copy the etc/bashrc to etc/bashrc_debug and change
export WM_COMPILE_OPTION=Opt
to
export WM_COMPILE_OPTION=Debug

Often times for debugging you want to enable FULLDEBUG and perhaps some symbols etc, but don't really want to recompile everything. This is where the '-debug' option comes in handy. For example,
Code:
wmake -debug some/source.C
(see wmake -help-full for more detailed information)



This is not only very convenient when debugging a single library or bunch of files, but can also have the advantage keeping the current compiler optimization level (eg, -02, -03). In some low-level code some problems/inconsistencies are not visible with the -O0 level.
olesen is offline   Reply With Quote

Old   September 27, 2022, 01:19
Default How to debug library in OpenFOAM
  #18
New Member
 
Lei Zhou
Join Date: Nov 2021
Posts: 6
Rep Power: 5
zhoulei_3c is on a distinguished road
Hello, foamers:

I created a new library. And I already used this method to debug the new solver, which linked both the default openfoam library and my new library. When debugging, it could step into the default openfoam by the methods, however, it cannot step into my own new library. What should I do?

Thanks in advance.
zhoulei_3c is offline   Reply With Quote

Old   October 13, 2022, 02:11
Default
  #19
New Member
 
venu
Join Date: May 2019
Posts: 8
Rep Power: 7
Venu Angirekula is on a distinguished road
Hi @zhoulei_3c

I don't have a direct method to step into the custom library. However, you can open your code file in vs code and place a breakpoint somewhere. By doing this vs code will reach a breakpoint and you can debug your code from there.
Venu Angirekula is offline   Reply With Quote

Old   January 7, 2023, 23:22
Default
  #20
Member
 
WY
Join Date: Mar 2020
Posts: 36
Rep Power: 6
WUYing is on a distinguished road
Quote:
Originally Posted by zhoulei_3c View Post
Hello, foamers:

I created a new library. And I already used this method to debug the new solver, which linked both the default openfoam library and my new library. When debugging, it could step into the default openfoam by the methods, however, it cannot step into my own new library. What should I do?

Thanks in advance.
Hi Zhoulei_3c,

I met the same problem with you. I develop a new library based on OpenFOAM 5.X, and if I understand correctly, if I want to use VS code to debug it (proposed by Rvadrabade), I first need to compile OF5 in debugging mode? Or have you found any other more convenient way to debug the new library? Thanks!
WUYing is offline   Reply With Quote

Reply

Tags
debugging, vscode

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The udf.h headers are unable to open- in VISUAL STUDIO 13 sanjeetlimbu Fluent UDF and Scheme Programming 4 May 2, 2016 06:38
freelancer/pay:Coding solidworks flow simulation API using microsoft visual studio Edreez CFD Freelancers 1 December 8, 2015 12:18
Compiling Fluent UDF with Visual Studio kamnaz Fluent UDF and Scheme Programming 0 August 28, 2014 13:58
Error with visual studio installation akshaymanikjade FLUENT 1 January 31, 2013 00:30
error while compiling a udf rahul FLUENT 10 August 30, 2006 09:33


All times are GMT -4. The time now is 04:40.