Clear Filters
Clear Filters

Not enough input arguments error when optimizing a COMSOL model output with fmincon

6 views (last 30 days)
I am trying to optimize the output of a polymer degradation via hydrolysis model in COMSOL with fmincon. Model is a porous geometry so there are 2 domains which are the pores including water for hydrolysis and the polymer part. My only input variable is the diffusivity of water but it has different values for 2 domains becuase diffusivity of water to polymer is 10 times less from the difusivity of water to water. Hence I am supplying the diffusivity values to the model with a interpolation function in a csv file. So basically I have a single value to optimize but it's supplied to comsol with a csv file as interpolation function not with set.params.
I wrote the optimization function and my main file and when I run the code I get the error 'not enough input arguments'. I am sharing the code below and supplying the necassary files as attachments.
First the main file:
clear all;
clc;
global model
import com.comsol.model.*
import com.comsol.model.util.*
model = ModelUtil.create('Model'); % Load the model
model.modelPath('C:\Users\ilkersevgen\Desktop\Sezen_modeller\Optimizasyon\fmincon\deneme'); % lokasyonu ekle
model=mphload('degradationSun_1.5mm_3x3_600poresize_connectedspherepores_3PDElideneme_yenimesh.mph'); %% load an existing model
M=csvread('Book1.csv');
M(:,end+1)=1;
x0 = 4.6; % Diffusivity of water (m^2/s)
A = []; %Linear and nonlinear inequaility constraints
B = [];
Aeq = [];
Beq = [];
lb = [0.46];
ub = [1000]; % upper limit
opts = optimoptions('fmincon','Display','iter','Algorithm','sqp', 'PlotFcns',{@optimplotx,@optimplotfval,@optimplotfirstorderopt},'ScaleProblem', true,'OptimalityTolerance',1e-2, 'DiffMinChange', 1e-2, 'DiffMaxChange', 1e-1);
opts.Display = ' iter-detailed';
sprintf('ScaleProblem : true\n')
[xopt, fopt,exitflag,output,lambda,grad,hessian] = fmincon(@(x) objectiveFunc_sezen_2, x0, A, B, Aeq, Beq, lb, ub, [], opts);
Then the optimization function:
function [f] = objectiveFunc_sezen_2(x)
% % Read data from the input text file
% data = readmatrix("input_file.txt"); % Assuming the data is in a tabular format
%MainSezen(D1);
%Extract data from the file
%time = data(:, 1);
%x = data(:, 2); % x is polymer concentration data obtained from comsol run
folder = 'C:\Users\ilkersevgen\Desktop\Sezen_modeller\interpolationfiles\yeni interpolation files for new mesh';
alpha = x(1)
M(1:191657,end) = alpha*10E-14;
M(191658:end,end) = alpha*10E-13;
csvwrite('DW_600um_yenimesh_Lagrange_Fine.csv', M);
model.component('comp1').func('int2').discardData;
%% the names of csv files imported to comsol
baseFileName = sprintf('DW_600um_yenimesh_Lagrange_Fine.csv'); %DW_600um_yenimesh_Lagrange_Fine----Xscaff int1, DW int2 in comsol
fullFileName = fullfile(folder,baseFileName);
model.component('comp1').func('int2').set('filename',fullFileName);
%do I need to export them to refresh?
% Interpolation functions are refreshed
model.component('comp1').func('int2').refresh;
model.component('comp1').func('int2').importData;
model.study('std1').run; %run the study
model.sol('sol1').run; %run the solution
PC = model.result.numerical('pev2').getReal();%read the value of Tm in the point of evaluation
% Perform necessary computations on the data to calculate the objective value(your computation code here)
objective_value = sum((PC - 0.9).^2);
% Define the objective function to be minimized
f = objective_value
end
In the main part I am reading the coordinates of each node of my geometry to the matrix M and then in my optimization function I am adding a one more column to matrix M to fill it with the diffusivity values. As I said in the polymer parts diffusivity values are divided by 10. Then matrix M is written back to the csv file that the COMSOL model reads.
alpha = x(1) part of the code gives the error 'Not enough input arguments'. Can you guys please help sorting out this problem.
I am not able to add the files because they are bigger even when zipped than 5MB but basically book1 is the coordinates of the 3d geometry so 3 columns and around 500000 rows and the csv I trying to write to has extra one column to book1 which has the diffusivity values for that coordinate. I can send them by mail if it is required.
Thank you so much

Accepted Answer

SANKALP DEV
SANKALP DEV on 4 Oct 2023
Hi Sezen,
I understand that you are seeking assistance in resolving the error Not enough input arguments.”
The error you are experiencing is because the variable 'alpha' is not defined within the scope of the 'objectiveFunc_sezen_2' function. To address this issue, you should pass the 'alpha' value as an input argument to the function. Here's how you can make the necessary modifications to your code:
function [f] = objectiveFunc_sezen_2(x, alpha)
% Rest of your code remains the same
end
In your main file, you should update the objective function call to include the 'alpha' value:
[xopt, fopt, exitflag, output, lambda, grad, hessian] = fmincon(@(x)...
objectiveFunc_sezen_2(x, a), x0, A, B, Aeq, Beq, lb, ub, [], opts); % update the call to include alpha as an argument
Hope this helps to resolve your issue.

More Answers (0)

Categories

Find more on Chemistry in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!