Skip to content

Commit c97cd6e

Browse files
committed
more changes
1 parent 9c687fd commit c97cd6e

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tmp/
2121
# Build
2222
_build
2323
docs/.buildinfo
24+
matlab.txt
2425
docs/*.inv
2526
*.rat.cpp
2627
*.so

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include README.md
1+
include README.md matlab.txt
22
recursive-include cpp *
33
prune tests
44
prune */__pycache__

RATapi/examples/normal_reflectivity/DSPC_custom_layers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def DSPC_custom_layers():
5050
# Add the custom file to the project
5151
problem.custom_files.append(
5252
name="DSPC Model",
53-
filename="custom_bilayer_DSPC.py",
54-
language="python",
53+
filename="customBilayerDSPC.m",
54+
language="matlab",
5555
path=pathlib.Path(__file__).parent,
5656
)
5757

cpp/matlab/matlabCaller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
LIB_EXPORT void startMatlab()
44
{
5-
MatlabCaller::get_instance()->startMatlab();;
5+
MatlabCaller::get_instance()->setEngine();;
66
}
77

88
LIB_EXPORT void cd(std::string path)

cpp/matlab/matlabCallerImpl.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <vector>
66
#include <iostream>
77
#include <chrono>
8+
#include <cstring>
89

910
using namespace std::chrono;
1011

@@ -22,10 +23,6 @@ class MatlabCaller
2223
}
2324
engSetVisible(matlabPtr, 0);
2425
};
25-
26-
void startMatlab(){
27-
// this->matlabFuture = startMATLABAsync();
28-
};
2926

3027
void cd(std::string path){
3128
this->currentDirectory = path;
@@ -34,8 +31,8 @@ class MatlabCaller
3431

3532
void call(std::string functionName, std::vector<double>& xdata, std::vector<double>& params, std::vector<double>& output)
3633
{
37-
if (!this->matlabPtr)
38-
this->setEngine();
34+
if (!matlabPtr)
35+
setEngine();
3936

4037
if (dirChanged){
4138
std::string cdCmd = "cd('" + (this->currentDirectory + "')");
@@ -70,8 +67,8 @@ class MatlabCaller
7067
void call(std::string functionName, std::vector<double>& params, std::vector<double>& bulkIn,
7168
std::vector<double>& bulkOut, int contrast, int domain, std::vector<double>& output, double* outputSize, double* rough)
7269
{
73-
if (!this->matlabPtr)
74-
this->setEngine();
70+
if (!matlabPtr)
71+
setEngine();
7572

7673
if (dirChanged){
7774
std::string cdCmd = "cd('" + (this->currentDirectory + "')");

setup.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import platform
3+
import shutil
34
import sys
5+
import warnings
46
from glob import glob
57
from pathlib import Path
68

@@ -108,14 +110,23 @@ def run(self):
108110
f"{build_py.get_package_dir(PACKAGE_NAME)}/{obj_name}")
109111

110112
obj_name = get_shared_object_name(libmatlab[0])
111-
build_py.copy_file(f"{build_py.build_lib}/{PACKAGE_NAME}/{obj_name}",
112-
f"{build_py.get_package_dir(PACKAGE_NAME)}/{obj_name}")
113-
113+
matlab_dll_path = Path(build_py.build_lib) / PACKAGE_NAME / obj_name
114+
if matlab_dll_path.exists():
115+
build_py.copy_file(matlab_dll_path,
116+
f"{build_py.get_package_dir(PACKAGE_NAME)}/{obj_name}")
117+
118+
open(f"{build_py.get_package_dir(PACKAGE_NAME)}/matlab.txt", 'w').close()
119+
114120

115121
class BuildClib(build_clib):
116122
def initialize_options(self):
117123
super().initialize_options()
118-
self.matlab_install_dir = os.environ.get("MATLAB_INSTALL_DIR", "")
124+
matlab_install_dir = os.environ.get("MATLAB_INSTALL_DIR")
125+
if matlab_install_dir is None:
126+
matlab_install_dir = shutil.which("matlab")
127+
if matlab_install_dir is not None:
128+
matlab_install_dir = Path(matlab_install_dir).parent.parent.as_posix()
129+
self.matlab_install_dir = matlab_install_dir
119130
build_py = self.get_finalized_command("build_py")
120131
self.build_clib = f"{build_py.build_lib}/{PACKAGE_NAME}"
121132

@@ -137,12 +148,12 @@ def build_libraries(self, libraries):
137148
link_libraries = []
138149
link_library_dirs = []
139150

140-
if lib_name == libmatlab[0] and not self.matlab_install_dir:
141-
print("No MATLAB install dir was not given so the MATLAB integration cannot be built.")
151+
if lib_name == libmatlab[0] and self.matlab_install_dir is None:
152+
warnings.warn("No MATLAB install dir was not given so the MATLAB integration cannot be built.")
142153
del libraries[index]
143154
continue
144155

145-
if self.matlab_install_dir:
156+
if lib_name == libmatlab[0] and self.matlab_install_dir is not None:
146157
extra_include.append(f"{self.matlab_install_dir}/extern/include/")
147158

148159
build_info["cflags"] = compile_args
@@ -159,12 +170,13 @@ def build_libraries(self, libraries):
159170
debug=self.debug,
160171
)
161172
language = self.compiler.detect_language(sources)
162-
if self.matlab_install_dir:
163-
link_libraries.extend(["libeng", "libmx"])
173+
if lib_name == libmatlab[0] and self.matlab_install_dir is not None:
164174
if platform.system() == "Windows":
165-
link_library_dirs.append(f"{self.matlab_install_dir}/bin/win64")
175+
link_libraries.extend(["libeng", "libmx"])
176+
link_library_dirs.append(f"{self.matlab_install_dir}/extern/lib/win64/microsoft")
166177
elif platform.system() == "Linux":
167-
link_library_dirs.append(f"{self.matlab_install_dir}/bin//microsoft")
178+
link_libraries.extend(["eng", "mx"])
179+
link_library_dirs.append(f"{self.matlab_install_dir}/bin/glnxa64"))
168180

169181

170182
self.compiler.link_shared_object(
@@ -189,7 +201,7 @@ def build_libraries(self, libraries):
189201
long_description_content_type="text/markdown",
190202
packages=find_packages(),
191203
include_package_data=True,
192-
package_data={"": [get_shared_object_name(libevent[0]), get_shared_object_name(libmatlab[0])], "RATapi.examples": ["data/*.dat"]},
204+
package_data={"": ["matlab.txt", get_shared_object_name(libevent[0]), get_shared_object_name(libmatlab[0])], "RATapi.examples": ["data/*.dat"]},
193205
cmdclass={"build_clib": BuildClib, "build_ext": BuildExt},
194206
libraries=[libevent, libmatlab],
195207
ext_modules=ext_modules,
@@ -206,12 +218,6 @@ def build_libraries(self, libraries):
206218
':python_version < "3.11"': ["StrEnum >= 0.4.15"],
207219
"Dev": ["pytest>=7.4.0", "pytest-cov>=4.1.0", "ruff>=0.4.10"],
208220
"Orso": ["orsopy>=1.2.1", "pint>=0.24.4"],
209-
"Matlab_latest": ["matlabengine"],
210-
"Matlab_2025a": ["matlabengine == 25.1.*"],
211-
"Matlab_2024b": ["matlabengine == 24.2.2"],
212-
"Matlab_2024a": ["matlabengine == 24.1.4"],
213-
"Matlab_2023b": ["matlabengine == 23.2.3"],
214-
"Matlab_2023a": ["matlabengine == 9.14.3"],
215221
},
216222
zip_safe=False,
217223
)

0 commit comments

Comments
 (0)