Matlab Cantera error Invalid MEX-file

295 views
Skip to first unread message

AJ Fillo

unread,
Jan 17, 2024, 9:23:30 PM1/17/24
to Cantera Users' Group
Hello,

I'm trying to install cantera for matlab and having some trouble, I continue to get the same error in Matlab.

Error is:
Invalid MEX-file 'C:\Users\aj\miniconda3\envs\ct-env-matlab\share\cantera\matlab\toolbox\ctmethods.mexw64':
The specified module could not be found.
Error in thermo_get (line 7)
    i = ctmethods(20, n, job, a, b);

Error in ThermoPhase (line 21)
t.tp_id = thermo_get(0, 0, src, id);

Error in Solution (line 50)
t = ThermoPhase(src, id);

Error in GRI30 (line 34)
    s = Solution('gri30.yaml', 'gri30');


Tried with Matlab version 2023a and 2023b same error, can try reverting to an earlier version if needed.

Cantera installed with conda
>conda create --name ct-env-matlab --channel cantera cantera cantera-matlab

I do have a second environment that was previously just a python install.  There doesn't seem to be any wires crossed there but I haven't ruled it out. 

I tried to check which dependencies where missing in `ctmethods.mexw64` following this recommendation here.   looks like `libmx.dll` and `libmex.dll`  Screen shot below.

confirmed both files are in `C:\Program Files\MATLAB\R2023b\bin\win64` and tried adding that path to MATLAB default paths.  No luck.

I have tried fully reinstalling both matlab and cantera, run `conda clean --all` and `conda update --all` still no luck.

Any support you can provide would be greatly appreciated. 

Cheers,
AJ


Screenshot 2024-01-17 181518.png



Ray Speth

unread,
Jan 20, 2024, 12:30:58 PM1/20/24
to Cantera Users' Group
Hi AJ,

It looks like you've already done everything I would have tried to debug this.

Having libmx.dll and libmex.dll show up as missing in the Dependencies program isn't an indicator of a problem -- those will both have been automatically loaded by Matlab, which does its own path manipulations, before trying to load ctmethods, so there shouldn't be a problem finding those. I'm assuming there aren't any more "missing" dependencies hiding under the first library dependency listed (the one that's in the Conda environment but whose name isn't fully shown in the screenshot)?

The other option you can try is the MSI installer available from the GitHub release page (https://github.com/Cantera/cantera/releases/tag/v3.0.0).

Regards,
Ray

AJ Fillo

unread,
Jan 23, 2024, 3:19:29 PM1/23/24
to canter...@googlegroups.com
Hi Ray,

I took another look into the Dependencies and did see the following is also missing, I must not have missed it before. 
ext-ms-win-oobe-query-l1-1-0.dll

I haven't been able to find this dependency anywhere so it does seem to be missing.

I haven't been able to find anything on Stack Overflow or the Mathworks support page that even mentions this dependency.  The microsoft support forums suggest it shouldn't be an issue but I'm also trouble shooting outside my understanding of windows at this point.  Any ideas on this dependency?

I can try the MSI installer, will it cause any issue with my existing conda python install?  

Also do you have a recommendation on which matlab version might be most successful to try?  I can revert back to an earlier version if it will solve issues.

Cheers,
AJ

A.J. Fillo, Ph.D.

Sr. Combustion Engineer

arbor.co




--
You received this message because you are subscribed to a topic in the Google Groups "Cantera Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cantera-users/KVK3EBwOVfw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cantera-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cantera-users/c9fb4436-cd40-4a3a-a6c3-12fe364383cfn%40googlegroups.com.

AJ Fillo

unread,
May 24, 2024, 9:59:49 PM5/24/24
to Cantera Users' Group
Hi Ray,

I found a better way to run the Python instance of Cantera natively in MATLAB which is kind of the best of both worlds for my team.

I wanted to follow up on this and share my solution in case others find it useful, especially, if like me they have collogues who are only using MATLAB and want to increase collaboration.

Before the details, the MATLAB MSI installer did work, but I found the MATLAB version of Cantera doesn't seem to have all the bells and whistles.  So in my continuing effort to create interoperability across my team here is the below install process I found works best.  It should allow you to call Cantera through python natively in MATLAB which lets you use all the great stuff Cantera has to offer.

I did all of this on windows, haven't tried it on my ubuntu machine but I expect that may be a smoother process.
  1. Download and install python through

    1. You must install a python version compatible with your flavor of MATLAB, check compatibility here

  1. Open windows Command Prompt, and follow the Cantera Pip install

  2. C:\Users\aj> py -m venv ct-env-matpy
    C:\Users\aj> ct-env-matpy\Scripts\activate
    (ct-env-matpy) C:\Users\aj> py -m pip install cantera

  3. Get your python PATH location - You will need the path with your ct-env-matpy in a later step

  4. (ct-env-matpy) C:\Users\aj>where python C:\Users\aj\ct-env-matpy\Scripts\python.exe C:\Users\aj\AppData\Local\Microsoft\WindowsApps\python.exe

  5.  Use this Preamble in MATLAB to import canters, change the python Path to your path from the previous step

    1. %% Load correct python environment pyExec = 'C:\Users\aj\ct-env-matpy\Scripts\python.exe'; % to find path activate correct envitionment in CMD and use `where python` comand pyRoot = fileparts(pyExec); p = getenv('PATH'); p = strsplit(p, ';'); addToPath = { pyRoot fullfile(pyRoot, 'Library', 'mingw-w64', 'bin') fullfile(pyRoot, 'Library', 'usr', 'bin') fullfile(pyRoot, 'Library', 'bin') fullfile(pyRoot, 'Scripts') fullfile(pyRoot, 'bin') fullfile(pyRoot, 'Share') }; p = [addToPath(:); p(:)]; p = unique(p, 'stable'); p = strjoin(p, ';'); setenv('PATH', p); %% load cantera module ct = py.importlib.import_module('cantera');
  6. From here you can call Cantera and other python modules natively in the Python syntax just adding py.cantera before functions, for example.

    1. temp_init = 439; pres_init = 101325; phi = .8; gas = py.cantera.Solution('gri30.yaml'); gas.set_equivalence_ratio(phi, struct('CO', 0.438, 'H2', 0.193, 'CO2', 0.446, 'N2', 0.002), struct('O2', 1.0), basis='mole'); gas.TP = [temp_init, pres_init]; % Domain width in metres width = .02; % Create the flame object flame = py.cantera.FreeFlame(gas, width=width); % Define tolerances for the solver flame.set_refine_criteria(ratio=3,slope=0.05,curve=0.05); % Define logging level loglevel = 1; flame.solve(loglevel=loglevel,auto='True')
  7. That should return a 1D flame front in a numpy array, you can easily convert back to a MATLAB vector using the double() function

    1. % %% Change variable type form python array to MatLab array with double() funciton. X = double(flame.X)'; v = double(flame.velocity); Su0 = v(1); Tad = max(double(flame.T)); delta = (max(double(flame.T)) - min(double(flame.T))) / (max(gradient(double(flame.T),double(flame.grid)))); figure plot(double(flame.grid),double(flame.T)) xlabel('Distance [m]'); ylabel('Temperature [K]'); title('Temperature Profile of the Laminar Flame'); figure plot(double(flame.grid),X(:,4)), hold on plot(double(flame.grid),X(:,1)) plot(double(flame.grid),X(:,15)) plot(double(flame.grid),X(:,14)) plot(double(flame.grid),X(:,6)) plot(double(flame.grid),X(:,16)) legend('O2','H2','CO','CH4','H2O','CO2','location','northwest') xlabel('Distance [m]'); ylabel('Mole Fraction'); title('Flame Profile of the Laminar Flame');


I hope this helps anyone who has also needs to collaborate between the MATLAB and Python ecosystems often.

Cheers,
AJ

Reply all
Reply to author
Forward
0 new messages