Skip to content

Explanation how to use "External" comsol node with Dynamic Link Libraries (DLL's) written on C from scratch.

Notifications You must be signed in to change notification settings

byquip/Comsol-External-Functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

Comsol External Functions

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.

What do you need to create you own DLL written on C?

  1. C program
  2. Compiler

Install compiler (Click to expand)

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.

  1. Launch installer.
    Launch installer

  2. Set path or keep as it is.
    Set path or keep as it is.

  3. Wait for manager installed.
    Wait for manager installed.

  4. Right-click on the line which you need to install and choose "Mark for installation".
    Right-click on the line which you need to install and choose "Mark for installation".

  5. Select marked lines.
    Select marked lines.

  6. Click "Installation" and then "Apply changes".
    Click "Installation" and then "Apply changes".

  7. Click "Apply" again.
    Click "Apply" again.

  8. Make sure that you have green squares in front of each selected line.
    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%


Prepere C code (Click to expand)

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;
	}
}


Compile C code (Click to expand)

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


Usage of created DLL (Click to expand)

The file saved to repository Example.mph

  1. Make sure you allowed to run external libraries at Preferences.
    Make sure you allowed to run external libraries.

  2. Create External node.
    Create External node

  3. Select path to mylib.dll and indicate function name which we used in script.c on line 30 test_func and 2 arguments which this function take.
    WARNING! Use File Schemes and File Handling such as user:///mylib.dll in order to avoid problems with File not found error Select path to DLL

  4. Usage is the same as with any other comsol function.
    Usage is the same as with any other comsol function.

  5. As any other comsol function you can plot it.
    For this you need to indicate range of values for each argument at Plot Preferences.
    Then press Plot.
    WARNING! plot complicated function could compleately freeze your comsol project. Better to test it first on blanc file. As any other comsol function you can plot it.

Author: Kostiantyn Vasko
Sources:

  1. Comsol unsolved forum problem
  2. 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.
  3. Some paper from comsol conference

About

Explanation how to use "External" comsol node with Dynamic Link Libraries (DLL's) written on C from scratch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages