Explanation how to use External
comsol node with Dynamic Link Libraries (DLL's) written on C from scratch.
This guide written 27.05.2022 for 5.5 version of Comsol for Windows.
I found official version of doc's with External node very poor exlaining how to make it work.
- C program
- Compiler
Website mingw.org does not work anymore, but we can download installer here.
Also I keep a copy of installer at the repository mingw-get-setup.exe.
-
Right-click on the line which you need to install and choose "Mark for installation".
-
Make sure that you have green squares in front of each selected line.
Last thing it's toset environment variables. In Windows CMD prompt write next
set PATH=C:\mingw;C:\mingw\bin;%PATH%
check if environment variables setted
echo %PATH%
Slide 7 of Presentation from cern website for materials[2] link in the end. Code example [here](/~https://github.com/byquip/Comsol-External-Functions/blob/main/res/script.c).
This is basic example eqivalent to sin(a^2+b^2)/(a^2+b^2)
but in Comsol it will be used like test_func(x, y)
// Libs which necessary
#include <math.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER // DO NOT TOUCH
#define EXPORT __declspec(dllexport) // DO NOT TOUCH
#else // DO NOT TOUCH
#define EXPORT // DO NOT TOUCH
#endif // DO NOT TOUCH
static const char *error = NULL; // DO NOT TOUCH
EXPORT int init(const char *str) { // DO NOT TOUCH (initialisation)
return 1; // DO NOT TOUCH
} // DO NOT TOUCH
EXPORT const char *getLastError() { // DO NOT TOUCH (errors handler)
return error; // DO NOT TOUCH
} // DO NOT TOUCH
EXPORT int eval(const char *func, // <--- Name of function given
int nArgs, // <--- Number of arguments given
const double **inReal, // <--- Matrix of real part of arguments
const double **inImag, // <--- Matrix of imag part of arguments
int blockSize, // <--- Length of columns with arguments
double *outReal, // <--- Column of real part of output
double *outImag) { // <--- Column of imag part of output
int i;
if (strcmp("test_func", func) == 0) { // <--- Check if we call right function from dll
if (nArgs != 2) { // <--- Check if we use right number of arguments
error = "Two argument expected";
return 0;
}
for (i = 0; i < blockSize; i++) { // <--- looping over every value in columns
// ------------------------------------>MY CODE HERE<------------------------------------
double a = inReal[0][i]; // <--- take real part of first ([0]) argument.
double b = inReal[1][i]; // <--- take real part of second ([1]) argument.
outReal[i] = sin(a*a+b*b)/(a*a+b*b); // <--- solve and assign to real part of the output
// ------------------------------------>MY CODE HERE<------------------------------------
}
return 1;
}
else {
error = "Unknown function";
return 0;
}
}
For compiling we use installed utillity.
In Windows CMD prompt write next (open new):
gcc -shared -o c:\mypath\mylib.dll c:\mypath\script.c
at path c:\mypath
suppose to appeear mylib.dll
For Linux:
gcc -shared -o c:\mypath\mylib.so -fPIC c:\mypath\script.c
The file saved to repository Example.mph
-
Make sure you allowed to run external libraries at
Preferences
.
-
Select path to
mylib.dll
and indicate function name which we used inscript.c
on line 30test_func
and 2 arguments which this function take.
WARNING! Use File Schemes and File Handling such asuser:///mylib.dll
in order to avoid problems with File not found error -
As any other comsol function you can plot it.
For this you need to indicate range of values for each argument atPlot Preferences
.
Then pressPlot
.
WARNING! plot complicated function could compleately freeze your comsol project. Better to test it first on blanc file.
Author: Kostiantyn Vasko
Sources:
- Comsol unsolved forum problem
- Presentation from cern website for materials
On 7 slide of this Power Point linked some paper:
L. Bortot, USE OF EXTERNAL C FUNCTIONS IN COMSOL MULTIPHYSICS, p. 11
I did not found it. - Some paper from comsol conference