Prepare and postwork for upgrading Ubuntu LTS from 18.04 to 20.04

This article contains some tips to ease the upgrade LTS in a productivity scenario/work environment.

I. preparation and backup

Because it is my working Ubuntu, backup is essential before even thinking about upgrade. not only documentation, configuration under `/etc` should also be backed up.

Follow the guide in https://www.cyberciti.biz/faq/upgrade-ubuntu-18-04-to-20-04-lts-using-command-line/ except the upgrade command:

RELEASE_UPGRADER_ALLOW_THIRD_PARTY=1 update-manager -d

RELEASE_UPGRADER_ALLOW_THIRD_PARTY=1will try to enable PPA if focal’s PPA is available. It is a new feature after Ubuntu 18.10, I think.

Another preparation is get the python3 packages list installed by pip3 , do not worry about python2, you should not use it any more.

ls /usr/lib/python3/dist-packages for system apt installed “python3-*”

ls /usr/local/lib/python3.6/dist-packages/ for sudo pip3 installed

ls ~/.local/lib/python3.6/dist-packages/ for pip3 installed

to save the list pip3 list --path "one_path_from_above" > pip_installed_list.txt for all 3 paths

Continue reading
Posted in Uncategorized | Comments Off on Prepare and postwork for upgrading Ubuntu LTS from 18.04 to 20.04

Choices and example of wrapping C++ code into Python

In 2019, I started a new project on my github. This project list most popular approaches to wrap C and C++ classes into Python modules.

pybind11
boost-python
swig
cython
cffi
cppyy

There are codes for C++ sources, setup.py and CMake integration example. I hope this project could be helpful somehow.

https://github.com/qingfengxia/python_wrap

Posted in Uncategorized | Comments Off on Choices and example of wrapping C++ code into Python

Effectively work with Nautilus File Manager

This belongs to Research Software Engineering on Linux series

 

Add new file types into “Create Document” shortcut menu

After adding empty or template files into “Template” folder in the user home, right click at the blank space of Nautilus (in small icon view, not in list view), there will be new file types in “Create Document” sub-menu.

 

Continue reading
Posted in Linux, Uncategorized | Comments Off on Effectively work with Nautilus File Manager

Effectively work with terminal

This belongs to Research Software Engineering on Linux series

 

get package name for a missing command

which can print the program full path for the command, useful if multiple version is installed.

command_name without any options argument, if not found bash will prompt you the name


gnome-terminalshortcut keys

Gnome desktop support the "Ctrl+Shift+T" shortcut key to launch a new terminal windows.

Inside "gnome-terminal" window , menu "Edit|Preferences" will bring up "Keyboard Shortcuts" where you can set your custom shortcuts.

  • Ctrl+Shift+T : create a new tab

    this can be changed to "Ctrl+T" to be consistent with nautilus, Firefox.

  • Alt+1 -:Switch to Tab1
  • Alt+2 : Switch to Tab2
  • Alt+3 : Switch to Tab3
  • Ctrl + + : increase font size
  • Ctrl + – : decrease font size

Open terminal in the current folder from shortcut menu

After install nautilus-open-terminal, right click any blank space, select "open a terminal" from drop down menu

sudo apt-get install dconf-tools nautilus-open-terminal

Without install any software, right click on the any folder, the drop down menu also have the menuitem, open a terminal and cd to that folder.


Change gnome desktop acceleration key

Using command gnome-control-center or go to system settings GUI, go to "device" pane and "Keyboard", system wide shortcut keys can be set. It is worth of browsing all the system short cut, such as screenshot.

 

In case shortcut keys are not allow to change.

Install dconf-editor by sudo apt-get install dconf-editor

Then run dconf-editor and set the org/gnome/desktop/interface/can-change-accels boolean on.

Posted in Uncategorized | Comments Off on Effectively work with terminal

Effectively setup multiple monitor layout at startup

This belongs to Research Software Engineering on Linux series

Super+P shortcut key can bring up a menu to switch between mirror, single, multiple modes.

keep Super key (windows key) pressed on, while P key can be released, use arrow key to select the display mode.

If primary display has turned off, Super+P shortcut key can save you from running the command gnome-control-center display to bring up setup GUI.

If you want to a script to auto detect external monitor and set up the screen extension at start up. here is an example

Continue reading
Posted in Linux | Comments Off on Effectively setup multiple monitor layout at startup

Cfd module for FreeCAD can work with external mesh file

http://www.iesensor.com/blog/wp-content/uploads/2018/12/FreeCAD_CFD_using_external_mesh.webm

Posted in Uncategorized | Comments Off on Cfd module for FreeCAD can work with external mesh file

export salome mesh to FeNICS mesh

Salome installation

  1. download

Salome platform 8.5 binary for Ubuntu 16.04 (64bit)

Salome-meca is the combination of salome and code_aster (FEA), it is also possible to manually add the Code_saturne plugin for CFD

  1. installation

extract this compressed file to a position like /opt, it has its own python2.7 and most not-so-common dependent packages Do read the readme to check if you have installed all the essential packages.

  1. run the program

run the program in a terminal (to catch output/error message) /path_to/salome.py which set env var.


Start SALOME, elapsed time :   2.1 seconds

Fatal Python error: Py_Initialize: Unable to get the locale encoding

  File "/opt/SALOME-8.5.0-UB16.04-SRC/BINARIES-UB16.04/Python/lib/python2.7/encodings/__init__.py", line 123

    raise CodecRegistryError,\

to fix this error on my machine, I need unset PYTHONPATH in my “~/.bashrc”

BTW, 3D The hardware acceleration is disable in my virtualbox,

“~/.bashrc”

#3D window is not working if 3D accel is turned on for vbox

export LIBGL_ALWAYS_SOFTWARE=1

  1. macro recording function

The operation within GUI can be recorded into python script by “File-> dump study”, this saved python script can be loaded later, or adapted for used in batch mode.

  1. NoteBook variables

variale can only be simple math expression, math.pi math.sin() are not supported.

  1. partition, group and save multi-zone mesh

submesh is used

=============================================================

mesh conversion from salome to Fenics

meshio-convert (from meshio package) can translate the salome Med format into dolfin format, however, it seems boundary and subdomains are not converted.

Here is another route: Salome>Gmsh>Fenics, inspired by [I was finally able to import an assembly mesh into OpenFOAM format with the following Salome>Gmsh>OpenFOAM process]https://www.cfd-online.com/Forums/openfoam-meshing/82210-importing-multiple-meshes.html


-Using Salome:

1. Within the Geometry module explode surfaces needed for Boundary Conditions and renamed them

2. Fused all parts together into a single assembly

3. Partitioned this assembly using each of the parts included

4. Created 'Volume Groups' for each partitioned part

5. Move to the Meshing module and mesh the whole partitioned assembly (optional: make sub-meshes of the parts to better control mesh sizing)

6. Make 'Volume Groups' of each part within the mesh

7. Make 'Surface Groups' for all the boundary conditions

8. Export mesh as MED file (need to check 'Automatically Create Groups' box)



-Using Gmsh

9. Open mesh file in Gmsh and 'Save As' Gmsh mesh file (.msh file extension, make sure file is saved as Version 2.0 ACSII and un-check all check boxes)

10. Move Gmsh mesh file into an empty case file (make sure to have constant->polyMesh (empty) and system folder with necessary files)



-OpenFOAM mesh conversion/manipulation tools

11. Run [>> gmshToFoam] in OpenFOAM terminal within the case directory

12. Run [>> splitMeshRegions -cellZonesOnly -overwrite] (this is only used if there are multiple parts (volumes) to be split up)



 

To export dolfin meshing, using dolfin-convert your_gmsh.msh your_dolfin.xml

Extra notes:

  1. In Salome-mesh export, Med version should select the lowest, however, gmsh may still complain the med lib version is too high. I use the gmsh 4 to read the med mesh sucessfully.

2. Make sure the gmsh “expert mode” is enabled in preference, so “gmsh v2 ASCII” option can be selected in gmsh export dialog.   It seems quad mesh cells are not supported in `dolfin-convert`

Posted in Programming | Comments Off on export salome mesh to FeNICS mesh

Coupling OpenFOAM with FeniCS for multiphysis simulation (OpenFOAM workshop 13 presentation)

This is my presentation for OpenFOAM workshop 13 presentation, unfortunately, I can not attend the workshop in person.

Link to my presentation PDF:
OFW13-19-119__Coupling_OpenFOAM_with_FeniCS_for_multiphysis_simulation__QingfengXIA

Outline of this presentation:
1. physics simulation automation
2. CFD module for FreeCAD (GUI for OpenFOAM)
3. multiphysics coupling by OpenFOAM and FenicsSolver
4. Future plan

Posted in Uncategorized | Comments Off on Coupling OpenFOAM with FeniCS for multiphysis simulation (OpenFOAM workshop 13 presentation)

preCICE build and test on Ubuntu 16.04

 

please read PDF version for better code highlighting

 

preCICE build and test on Ubuntu 16.04

Posted in Uncategorized | Comments Off on preCICE build and test on Ubuntu 16.04

FreeCAD 0.17 + OpenFOAM CFD module now works on windows 10 WSL

after windows 10 2018 april update, it is possible to run my CFD module on FreeCAD 0.17

Limited by my time I can spend on this open source project, I will will make new feature in about half year, but maintain CFD module working with Stable FreeCAD release.

Posted in Uncategorized | Comments Off on FreeCAD 0.17 + OpenFOAM CFD module now works on windows 10 WSL

I have developed several multi-physics solver based on Fenics and GUI within FreeCAD CFD workbench

This is also announced in Fenics support forum

 

Screenshot of FenicsSolver in FreeCAD
Dear all,

I have developed several multi-physics solver based on Fenics, it is hosted on github:
https://github.com/qingfengxia/FenicsSolver
This solver is data driven (json), instead of programming for every case. It is still in early stage, I would pick up advice from community, especially standardization of json input data format, naming.

It is primarily used as a submodule for my CFD workbench for FreeCAD, giving GUI for boundary selection. I think a GUI for boundary selection is crucial for complicate geometry. I also use it for other projects with maximum code re-usage.
https://github.com/qingfengxia/Cfd

My future plan will be integrate Fenics with OpenFOAM which is quite advanced in multiphase flow and turbulent flow. Recently, my time is very limited without a permanent position in the academia, thus my update and response will be slow.

I should attend Fenics 2018 at Oxford to demonstrate some advanced usage.

Thanks

Qingfeng Xia @ Engineering Science Dept of Oxford University

Posted in Programming, Research | Comments Off on I have developed several multi-physics solver based on Fenics and GUI within FreeCAD CFD workbench

Add visual studio command prompt to windows explorer context menu

Recently relised visual studio build tool (with cmake support), enables building windows c++ project without GUI. It is essential to accelerate the testing of cross-platform program.

It is convenient and decent to add Bash (WSL) and visual studio command line tool

1. git clone
2. cd source & mkdir build & cd build & cmake .. # cmake to generate msbuild/makefile
3. nmake # to compile

Continue reading

Posted in Uncategorized | Comments Off on Add visual studio command prompt to windows explorer context menu

using cloud space as a private git repo

[answer from stackoverflow]

It is absolutely possible – what you probably want to do is create a “bare” git repository in folder Y (git init --bare)

and then add that file location as a remote:

git remote add Y file:///path/to/Y
#example path: file:///media/sf_OneDrive/gitrepo/FenicsSolver/
======= push and pull has different repo ==========
git remote set-url origin --push --add <a remote>
git remote set-url origin --push --add <another remote>

========push to multiple repo======

The latest version of git (as of October 2012) allows you to do this from the command line:

git remote set-url origin --push --add user1@repo1
git remote set-url origin --push --add user2@repo2
git remote -v

It is also possible to push encrypted code to public repo, not tested.

Posted in Uncategorized | Comments Off on using cloud space as a private git repo

BoundaryLayer setup for Gmsh 3D meshing

Precise control of the thickness of the first cell normal to wall, is crucial for  turbulence models like k-epsilon, k-omega. Before moving to turbulence flow simulation for CFD workbench of FreeCAD

2D meshing with boundary layer, tutorial

3D boundary meshing for simple cube is also done, based on FreeCAD FEM workbench example of 3D cantilever.
CAD and Gmsh setup files are embeded.
photo and progress will be updated on the FreeCAD Fem post

Continue reading

Posted in Uncategorized | Comments Off on BoundaryLayer setup for Gmsh 3D meshing

from gmsh geo file to fenics mesh with subdomain and boundary facet exported

from dolfin import *
import os.path
from subprocess import Popen, PIPE, check_output

# https://fenicsproject.org/qa/9983/setting-boundary-conditions-for-meshes-generated-by-gmsh
# generete mesh by: gmsh test.geo -3
# dolfin-convert -i gmsh test.msh test.xml

geo_file_content = """
size = 1; //m
meshThickness = size / 3; 
gridsize = size / 5;

// All numbering counterclockwise from bottom-left corner
Point(1) = {0, 0, 0, gridsize};
Point(2) = {0, size, 0, gridsize};
Point(3) = {size, size, 0, gridsize};
Point(4) = {size, 0, 0, gridsize};
Line(1) = {1, 2};               
Line(2) = {2, 3};           
Line(3) = {3, 4};       
Line(4) = {4, 1};       
Line Loop(4) = {1, 2, 3, 4};
Plane Surface(8) = {4};
Physical Line(12) = {1};
Physical Line(23) = {2};
Physical Line(34) = {3};
Physical Line(41) = {4};

Physical Surface(1) = {8};  // do not comment out this line

//Transfinite Surface{8};  // fenics does not support recomine
//Recombine Surface{8};


// boundary layer
Field[4] = BoundaryLayer;
Field[4].EdgesList = {1,3};  // FacesList  for 3D
Field[4].hfar = 10;
Field[4].hwall_n = 0.015;
Field[4].hwall_t = 0.01;
Field[4].thickness = 0.02;
Field[4].ratio = 1.0;

BoundaryLayer Field = 4;

Mesh.CharacteristicLengthFromCurvature = 1;
Mesh.Algorithm = 8; // delannay=1
"""

fname="test"
with open(fname+".geo", 'w') as f:
    f.write(geo_file_content)

cmd = 'gmsh {}.geo -3'.format(fname)
print check_output([cmd], shell=True)  # run in shell mode in case you are not run in terminal

cmd = 'dolfin-convert -i gmsh {}.msh {}.xml'.format(fname, fname)
Popen([cmd], stdout=PIPE, shell=True).communicate()

mesh = Mesh(fname+".xml")
if os.path.exists( fname+"_physical_region.xml"):
    subdomains = MeshFunction("size_t", mesh, fname+"_physical_region.xml")
    plot(subdomains)
if os.path.exists( fname+"_facet_region.xml"):
    boundaries = MeshFunction("size_t", mesh, fname+"_facet_region.xml")
    plot(boundaries)

plot(mesh)
interactive()

 

Posted in Programming, Research | Comments Off on from gmsh geo file to fenics mesh with subdomain and boundary facet exported

FreeCAD compatible Fenics mshr geometry building

Fenics geometry building API is based on CGAL, it can import CAD, but can not export CAD file like step. Here is a solution, not fully tested. This file may be further developed in github.

Continue reading

Posted in Uncategorized | Comments Off on FreeCAD compatible Fenics mshr geometry building

solved Fenics (2016.2) cpp code compiling error on ubuntu 16.04

for any expression in Expression for Fenics 2016.2( installed from PPA )
“`
expr = “”” x[2]> {} ? {}:{}”””.format(disc_zmax, lambda_p, lambda_d)
print(expr)
K = Expression(expr, element=V.ufl_element()) # error in compiling
“`
instant error log:


— The imported target “vtkRenderingPythonTkWidgets” references the file
“/usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so”
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
“/usr/lib/cmake/vtk-6.2/VTKTargets.cmake”
but not all the files it references.

check if this file is missing or not
`ls -l  /usr/lib/python2.7/dist-packages/vtk/libvtkRendering*`
python-vtk6  (VTK6.2) is installed, and installed file list:

/usr/lib/python2.7/dist-packages/vtk/libvtkRenderingPythonTkWidgets.x86_64-linux-gnu.so

“`
sudo ln -s /usr/lib/python2.7/dist-packages/vtk/libvtkRenderingPythonTkWidgets.x86_64-linux-gnu-6.2.so /usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so
“`

#this is the only file needs symbolic link to /usr/lib/x86_64-linux-gnu/

then, error is solved

Posted in Programming | Comments Off on solved Fenics (2016.2) cpp code compiling error on ubuntu 16.04

Cmake cheatsheet

#Cmake cheat sheet in markdown

Continue reading

Posted in Uncategorized | Comments Off on Cmake cheatsheet

c and cpp pitfalls, mixing signed and unsigned integer, implicit conversion

1. mixing unsigned and sign int is a bad idea

  • first of all, remember: signed int types will be implicitly converted to unsigned if sizeof(signed int types) == sizeof(unsigned int types)
  • Google C++ coding standard, say “do not use unsigned integer”
  • C++ STL using size_t which is unsigned int to represent size of containters like std::vector, std::string
  • do not assume sizeof(int types), always #include <stdint.h> and use int64_t
  • compiler can warn warning: comparison between signed and unsigned integer expressions [-Wsign-compare] . However, mixed unsigned and signed int algorithm is not warned
  • JAVA before Java 8 SE does not support unsigned int, even in Java 8 SE you can not declare unsigned int types, but only using static methods, unsigned comparison.
    There are good reason not to supported it.
  • python built-in integer/long is unsigned, but ctypes module defined c++ unsigned types; python 3 integer will not overflow
  • see also

http://blog.robertelder.org/signed-or-unsigned-part-2/

example and explanation from stackoverflow

2. Relevant quote from the Standard: Expressions

10 Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are de?ned as follows:

2 clauses about equal types or types of equal sign omitted

  • Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.
  • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.
  • Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

3. pitfall of overflow and underflow

  • overflow detection can not been done by compiler but user
  • float overflow could be captured and hook to abort routine
  • avoiding infinite looping for underflow by compiler warning

    comparison of unsigned expression >= 0 is always true [-Wtype-limits]

  • assigned minus int to unsigned int should be prohibited, try hex instead or UINT_MAX – 1, since compiler will not warn you for implicit conversion into unsigned int types

4. pitfall of implicit conversion

see more c and c++ pitfalls

Guru of the Week (GotW)

Guru of the Week (GotW) is a series of C++ programming problems created and written by Herb Sutter.

#include <iostream>
#include <limits>
#include <vector>


void std_container_test(){
    std::vector<int> v;
    int i = 10;
    if (i < v.size()-1) // there is a compiler warning here
        std::cout <<"underflow occurs for empty container size()-1\n";
}

void underflow_test(int input_s, unsigned int input_u){
	unsigned int size_u = 10u;
	unsigned int i_u;
	int size_s = 10;
	int i_s;
    // infinite looping for underflow
	for(i_u = size_u -1; i_u >=0; i_u--){
		std::cout <<"unsigned: " << ((input_u + i_u) / 2u) << "\n";
	}
}

void float_overflow_test() {

    float d = std::numeric_limits<float>::max();
    std::cout << "std::numeric_limits<float>::max() = " << d << std::endl;
    float q = d + 100;  //behavior is undefined,  could be inf, or remained as std::numeric_limits<float>::max() 
    std::cout << "q: " << q << std::endl;

}

void bool_implicit_coversion_pitfall_test() {
    int x = 0;
    if (!(-0.5 <= x <= 0.5))
        std::cout << "x=0, -0.5 <= x <= 0.5 is false"<<"\n";
}

void signed_int_test() {

    signed int s1 = -4;
    unsigned int u1 = 2;

    signed long long s2 = -4;
    unsigned int u2 = 2;

    signed long long int s3 = -4;
    unsigned long int u3 = 2;

    unsigned long int u4 = -2; // no worning at all

    if (s1>u1)
        std::cout << s1 << " > " << u1 << " is true? \n";
    std::cout << (s1 + u1) << "\n"; // 4294967294
    std::cout << (s2 + u2) << "\n"; // -2 
    std::cout << (s3 + u3) << "\n"; // 18446744073709551614 
    
    std::cout << " What are you expecting from `unsigned long int u4 = -2;`, actually stored integer is " << u4<< "\n"; 
}

int main()
{
    signed_int_test();
    bool_implicit_coversion_pitfall_test() ;
    std_container_test();
    float_overflow_test();
}

 

Posted in Programming | Comments Off on c and cpp pitfalls, mixing signed and unsigned integer, implicit conversion

compile c++ codes using Visual C++ Build Tools from the command prompt

Introducing the Visual Studio Build Tools

“If you have installed Microsoft Visual C++ Build Tools 2015 on Windows 10, open the Start menu and choose All apps. Scroll down and open the Visual C++ Build Tools folder. Choose Visual C++ 2015 x86 Native Tools Command Prompt to open the command prompt window.”

1) for single file source file

cl -> gcc , but the command options is different,

cl /EHsc hello.cpp

https://msdn.microsoft.com/en-us/library/f35ctcxw.aspx

2) for a small project, CMake is recommended,

it is handy to write one from example CMakeLists.txt online.

In your source code folder: md build && cd build && cmake ..

then using nmake instead of make

3) for exsitent vs solution using msbuild:

compile visual studio solution/project designed by VS IDE, no corresponding command on Linux.

search “MSBuild Command-Line Reference”

In the near future, other light weight IDE might support detecting “Visual Studio Build Tools”, e.g QtCreator

Posted in Programming | Comments Off on compile c++ codes using Visual C++ Build Tools from the command prompt