Remi Magnard
                                                                                                                                                    COMSOL Employee
                                                         
                            
                                                                                                                                                
                         
                                                
    
        Please login with a confirmed email address before reporting spam
     
    
 
                                                Posted:
                            
                                3 years ago                            
                            
                                30 sept. 2022, 03:30 UTC−4                            
                        
                        
                                                    Dear Michael,
See below a script that illustrates how to combine the solutions of two models using the COMSOL API in MATLAB:
% Load source model
model1 = mphopen('open_pipe_solve1','Model1');
model2 = mphopen('open_pipe_solve2','Model2');
% Load destination model
model =  mphopen('open_pipe_solve1','ModelFull');
% Get Parameter list
PVals1 = model1.sol('sol1').getPVals;
PVals2 = model2.sol('sol1').getPVals;
% Construct thenew full list of parameters and set it to the source model
PVals = [PVals1;PVals2(2:end)];
model.sol('sol1').setPVals(PVals);
% Extract solution vectors in source models and set them in the destination 
n = length(PVals1);    
for i = 1:n;
    U = model1.sol('sol1').getU(i);
    model.sol('sol1').setU(i,U);
end   
for i=2:length(PVals2);
    U = model2.sol('sol1').getU(i);
    model.sol('sol1').setU(n+i-1,U);
end
% Create the solution
model.sol('sol1').createSolution;
Note that you could also have both solutions available within the same model, in this case use the Combine Solutions study step to generate a new solution, see the attached model combine_solution.mph (the model solution is reset to reduce its size, so you need to run Study 1, Study 2, and then Study 3).
Best regards,
Rémi
                                                 
                                                
                            Dear Michael,
See below a script that illustrates how to combine the solutions of two models using the COMSOL API in MATLAB:
    % Load source model
    model1 = mphopen('open_pipe_solve1','Model1');
    model2 = mphopen('open_pipe_solve2','Model2');
    % Load destination model
    model =  mphopen('open_pipe_solve1','ModelFull');
    % Get Parameter list
    PVals1 = model1.sol('sol1').getPVals;
    PVals2 = model2.sol('sol1').getPVals;
    % Construct thenew full list of parameters and set it to the source model
    PVals = [PVals1;PVals2(2:end)];
    model.sol('sol1').setPVals(PVals);
    % Extract solution vectors in source models and set them in the destination 
    n = length(PVals1);    
    for i = 1:n;
        U = model1.sol('sol1').getU(i);
        model.sol('sol1').setU(i,U);
    end   
    for i=2:length(PVals2);
        U = model2.sol('sol1').getU(i);
        model.sol('sol1').setU(n+i-1,U);
    end
    % Create the solution
    model.sol('sol1').createSolution;
    
Note that you could also have both solutions available within the same model, in this case use the Combine Solutions study step to generate a new solution, see the attached model combine_solution.mph (the model solution is reset to reduce its size, so you need to run Study 1, Study 2, and then Study 3).
 
 Best regards,
 
 Rémi