Quantcast
Channel: Latest Discussions - COMSOL Forums
Viewing all 2194 articles
Browse latest View live

Same model, different solutions?

$
0
0
Hello, Community,

I'm doing phase change simulation with phase change HT module. Currently I have a problem with COMSOL:

The same model gives different results in different runs!

I use Matlab Livelink to read the solution in each run and compare the difference between two runs.

What I did in Matlab is:

% load model
Comsol_model2 = mphload(Model.mph);
% Run the Temperature model
Comsol_model2.sol('sol2').runAll;
% get model values
Temp2=mpheval(Comsol_model2,{'T2'});
Comsol_T2=Temp2.d1; % Temperature Variables
Comsol_Px2=Temp2.p(1,:); % x coordinate
Comsol_Py2=Temp2.p(2,:); % y coordinate

Comparing the above 3 variables in two simulation runs, the x and y coordinate values are exactly the same but the temperature variable varies randomly in each simulation. I expect the same solution of temperature from each simulation runs if the model is the same.

So can anyone here help me to explain this weird problem?

Extraction at Arbitrary Points and NaN

$
0
0
I have gone through the matlab-comsol livelink manual, and I understand that the NaN output value occurs when the extraction point is outside of the computation domain; but in this case the point is within the domain and I am still getting the NaN value. Please, I will appreciate any assistance or suggestions.

Kindly find as attached the files in case you have sufficient time to run and check.

Regards

FEMI

Load vector is always zero vector in linear problems? Hmm...

$
0
0
Attached is a model with one 1D element and 2 Dirichlet-type BCs on both ends of it implemented similarly to Chien Liu's blog post here: www.comsol.com/blogs/implement...k-form-in-comsol-multiphysics/

Using Linear discretisation, stationary solver, Laplace equation in the domain, this is about the simplest FEM model possible, so it should be very easy to interpret.

As described in Chien Liu's blog post that follows the one linked above in that series
www.comsol.com/blogs/implement...he-weak-form-with-a-comsol-app
we can add an Assemble node under the solver node, and view the System Matrix under Derived Values.

In our case we have 4 DOFs: two corresponding to the dependent variable's value at each end of the element, and two for the lambdas used to implement the Dirichlet BCs. The stiffness matrix looks about right:
0 1 0 0
1 1 0 -1
0 0 0 1
0 -1 1 1
But the load vector is all zeros:
0
0
0
0
(Sometimes when testing with some trivial changes, the middle entries show up as small values around the machine epsilon ~1e-16.)

The BCs are 2.0 on the left end and 7.0 on the right, and the solution shows up correctly in the plot.

The eliminated stiffness matrix and load vector are the same (up to a scaling constant), because we are using weak constraints (nothing is eliminated, and the Lagrange multipliers are explicitly solved for). If we implement the Dirichlet BCs the usual way instead, and check the "Use weak constraints" checkbox, then exactly the same thing happens as above. Also, we can see under Equation View, that we have the same equations as when we use the "Weak Contribution" method to implement the Dirichlet BCs.

If we use LiveLink and get the Kc, Lc matrices, then, of course, the solution Kc\Lc is just the trivial all-zeros solution (as Lc is a zero vector, and so is L), which is incorrect.

There is a comment under Chien Liu's post (second link above) from Jing Wang July 6, 2015, reporting the same issue, which has been referred to Comsol support, but with no public follow-up. My guess is that, since the blog post refers to Comsol 5.0, that it used to work as expected under 5.0, but has changed since then.

The interesting thing is that I found a way to get the expected load vector, using this strange procedure: in "Stationary Solver 1" under "Linearity" choose "Linear perturbation", with "Values of linearization point" "Prescribed by" "Solution", "Solution": "Solution 1 (sol1)". If we solve and assemble now, we get a flat zero solution, but the load vector is now evaluated to the expected values.

However, under no combination of these Linearity settings could I get both the expected solution in the plots and the expected load vector from System Matrix evaluation.

My interpretation is that either this is a bug, or Comsol since 5.1 has been using another layer of linearisation point picking, that we can't access. Either way, we can't export the matrices into Matlab and solve the system there with higher precision using the procedure dictated by common sense.

Variable modifications between solution iterations

$
0
0
I'm developing a transient model using the Livelink interface. The model first calculates an initial solution for the first time window, then I would like to modify the state variable values due to some external disturbance using MATLAB, then proceed to rerun the model for the next time window using the newly adjusted values of some of the variables, but the other variables should be retained from the previous solution iteration.

When I prescribe the use of the previous solution for subsequent iterations as:

model.sol('sol1').feature('v1').set('initmethod', 'sol');
model.sol('sol1').feature('v1').set('initsol', 'sol1');

it seems that the specification of initial conditions through either

model.variable('var15').set('X0', num2str(X0));

or

model.init('init4').set('position',num2str(X0));

are ignored completely and the unchanged values from the previous solution are retained. Is there a plausible method to effectively update some of the variables this way? Perhaps by modifying the dataset of the previous solution?

Thanks.

Using deformation results as initial conditions for the next study

$
0
0
Hi all,

I have a linear elastic model with a stationary study whereby I apply a boundary load and deform the model domain - this is simple and works fine.

Next, I would like to use the deformed results from the first solution as the initial condition for a second solution where I re-apply the same boundary load. I hope to be able to see that the deformation builds up with each successive solution/load (I plan to repeat this 10 times eventually).

**However, I would like to know how to take the deformed results from the first solution and set them as the initial condition for the next solution?** and **Is it possible to alter the geometry as part of this, as the boundary with the boundary load is bound to deform too?**

Any help would be greatly appreciated!

Cheers, James.

"Using deformation results as initial conditions for the next study"

How to continue matlab code if Comsol solver did not converge?

$
0
0
Hi,

I am a student in engineer and I have to use comsol for my thesis.
To optimize the results of the model I build, I use a genetic algorithm I made in matlab.
I use Comsol 4.4 and matlab R2015a. LiveLink Comsol with Matlab too.

Here is a small description of my model :
- 1D model
- heat transfer in solids, heat flux and radiation
- Time dependent
- heat capacity and thermal conductivity are user defined and function of the temperature
- geometry : interval of 15mm

How work the algorithm for each iteration :
- It loads my model (mphload(...))
- It choose a value for the thermal conductivity (function of temerature) determined by the algorithm
- It puts the value in my model (model.param.set(...))
- It runs the solver (model.sol('sol1').runAll)
- It compares comsol results with experimental data (T = mpheval(...))
- It try again with another value for the thermal conductivity

After a lot of trial, my algorithm works good but sometimes I have an error saying "Nonlinear solver did not converge" and it returns an error in matlab and stop my algorithm.
To avoid this problem, I search a way to say T = 0 if the solver did not converge. So, my algorithm can continue and I can expect a result at the end.
I hope someone has a solution because when it runs, I can just expect that there will allways be a solution that converge...

You can find a part of the code here :
model = mphload('TestCom.mph'); % load model

% Determination of the thickness
model.geom('geom1').feature('i1').set('p2',th);
model.geom('geom1').run;

% Determination of the thermal conductivity (kf) function of temperature (TOP)
for kk = 1:1:np
name1 = sprintf('TOP%g',kk);
name2 = sprintf('kf%g',kk);
model.param.set(name1, sprintf('%g [K]',TOPTryd(kk)));
model.func('int6').setIndex('table',name1,kk-1,0);
model.param.set(name2, sprintf('%g [W/(m*K)]',kfTryd(kk)));
model.func('int6').setIndex('table',name2,kk-1, 1);
end

ModelUtil.showProgress(true); % display the progress bar
pause(eps)
model.sol('sol1').runAll; % solve
T = mpheval(model,'T','selection',2,'edim','boundary'); % extract and store data
ModelUtil.remove('model') % remove model because it will be reloaded on every loop

No Matlab Livelink response after loading model to Matlab

$
0
0
Hi there,

I have a large Comsol 4.3a model with size almost 50 GB. I would like to run it through MatLab to export all solution data from parametric sweep simulation with almost 1,300 parameter combinations. The problem is that MatLab cannot step to the next script even though its status is busy after finishing loading the model using the following script.

addpath '/cluster/cloud/comsol-43a/mli'
import com.comsol.model.*
import com.comsol.model.util.*

mphstart
ModelUtil.showProgress(true);
model=mphload();

I'm guessing that the model size is too large and there is some limitation on Java about size in the settings. Does anyone know how to solve this problem?

Thanks,
Mengxue

LiveLink geometry layout design

$
0
0
Hi,

I am using Matlab LiveLink and am trying to create a layout for optical reflectors. My Problem is the following: I am looking at about 2000 reflectors and want them to form a union so I don't have to click on every single one and set their properties seperatly every time I generate a new layout.
I have prepared a Codefragment that demonstrates my Problem:

import com.comsol.model.*;
import com.comsol.model.util.*;

model = ModelUtil.create('Model');
geom1 = model.geom.create('geom1',3);

wp1 = geom1.feature.create('wp1','WorkPlane');
wp1.set('planetype','quick');
wp1.set('quickplane','xy');

refl_union = wp1.geom.feature.create('refl_union', 'Union');

for i=1:10

refl_geom_name{i} = wp1.geom.feature.create(['r' num2str(i)], 'BezierPolygon');
refl_geom_name{i}.set('type', 'closed');
refl_geom_name{i}.set('p', [i i+0.5 i+0.5 i ; 0 0 2 2]);
wp1.geom.feature.create(['refl_csol' num2str(i)],'ConvertToSolid');
wp1.geom.feature(['refl_csol' num2str(i)]).selection('input').set(['r' num2str(i)]);

refl_union.selection('input').object('geom1');
refl_union.selection('input').set(['refl_csol' num2str(i)]);

end

ext1 = geom1.feature.create('ext1','Extrude');
ext1.selection('input').set({'wp1'});
ext1.set('distance',5);

geom1.run;
mphgeom(model,'geom1');

My Intention was to create one geometrical Object (refl_union) and one by one add the objects to it during each itteration of the for loop. However this does not seem to work. The Code works fine till I try to form a Union, what am I missing?

Thanks
Mark

beg for a quick way to add material to each domain

$
0
0
Hello everyone,

I am using AC/DC module. The geometry has a lot of domains, which is shown in the attached file. Now, I want to add different materials with different relative permittivity to each domain. But it really takes a long time (my code in comsol with matlab is shown below). Is there a quick way to achieve this?

Thanks

parfor n2=1:4096 %no of grids
model.material.duplicate(strcat('mat', num2str(n2+3)), 'mat1');
model.material(strcat('mat', num2str(n2+3))).selection.set(n2+2);
model.material(strcat('mat', num2str(n2+3))).propertyGroup('def').set('relpermittivity', grid_permittivity(n2));
end

best suited PC for comsol 5.2

$
0
0
As a scholar, I rigorously work in comsol. I'm currently using core i7 based hp pavilion (5 yrs old), with 16GB RAM where I regularly faces some issues like hanging, unable to open files larger in size (even 10GB), problem in display driver and so on. So, any suggestion about, how to recover from these issues? Some friends even suggest me to buy a workstation.

Is there any possibility for me to work with same PC, or should I've to change it?

Running Comsol Desktop Model with Matlab Script: Updating Parameters

$
0
0
I am using a matlab script to update fluid parameters (viscosity and density) initially defined in comsol desktop. Based on some coupled physics, the density and viscosity of the fluid will change, once the necessary criterion is met. The flow velocity and pressure are extracted and used to determine if the criterion will be met or not, in the first place.

Now, do I need to place the "model.study('std1').run" command line after each matlab update, as schematically illustrated below:

begin code

import com.comsol.model.*
import com.comsol.model.util.*
model=mphload('XYZ');
model.study('std1').run;

% Extract Initial Pressure and Velocity based on initial prescribed parameters in comsol desktop
vf1=(mphinterp(model,'u','coord',coord1a,);
pf1=(mphinterp(model,'p','coord',coord1a);

% Given Criterion
if criterion is met
rho=rho2 (rho2 has been determined in matlab)
mu=mu2;
else if criterion is not met
rho=rho1 (rho1 is the intial fluid density)
mu=mu1;

%Updating the Parameters in Comsol
model.param.set('mu',mu)
model.param.set('rho',rho)

((model.study('std1').run; ))...xxxxxx

% Extracting the Pressure and Velocity Values
vf2=(mphinterp(model,'u','coord',coord1a,);
pf2=(mphinterp(model,'p','coord',coord1a);

% Other Calculations to be done with the second extracted pressure and velocity

end of code

Do I need to put the "xxxxx" marked line above, since I have updated the parameters, and I need to get the new pressure and velocity values? In case I erase the xxxxx line, will the extracted pressure and velocity values, (1) an (2) be different or same, considering the update on fluid properties?
In a nutshell, will the current value of the parameter in the matlab script (since the value of the parameter at latter line of the code is the current) be the current parameter in comsol desktop or do I need to rerun the comsol model with the xxxxx line command, so that I can get the corresponding flow properties based on the updated fluid properties?

Regards

Automating Comsol with Matlab: While Loop and Time Dependent Solver

$
0
0
I am running a comsol model with matlab; and I have a while loop in which the pressure values extracted from comsol at a time tmax corresponding to the iteration parameter, k of the while loop, is used to compute a variable. Since MATLAB and COMSOL do not run at the same sequence, how can t and k be synchronized so that my computation will not run out of results or solutions.

In other words, suppose I have the code below, and pwmid is to extracted from Comsol destop. The final value of k is not known until the end of the iteration. For a time dependent problem in Comsol, I need to specify the computing time, t (i.e 0:1:tmax), where tmax is the max time. How can I synchronize tmax and k, so that I do not run out of values in MATLAB; if a specify a value tmax=1000, and at the end of while loop k=1001, I will run out of values at the last iteration step. But if I update the value of tmax with k and recompute, I am guessing this should synchronize the two values; will it despite the difference in the computing speed of matlab and comsol?

clear all
clc
Lh=15*ones(1,5);
for j=1:5
k=1;
%Lh=15;
Lo(k,j)=0.2;
k2(k,j)=Lo(k,j)./sqrt(Lh(j));
ud(k,j)=(k2(k,j)).^2.*Lh(j);
p(k,j)=Lh(j).^3*Lo(k,j);
pq(k,j)=k2(k,j).*Lh(j).^3;
if pq(k,j)<p(k,j)
while Lo(k,j)<Lh
Lo(k+1,j)=Lo(k,j)+ud(k,j);
k2(k+1,j)=Lo(k+1,j)./Lh(j);
ud(k+1,j)=(k2(k+1,j)).^2./Lh(j);
pq(k+1,j)=k2(k+1,j).*Lh(j).^3;
p(k+1,j)=Lh(j).^3*Lo(k+1,j);
Pm=[p k2 ud pq];
pwmid(k)=(mphinterp(model,'p','coord',coord10a,'t',k)
Pm2(k+1,j)=Pm(k+1,j)-pwmid(k);
k=k+1;
if k>1000
break;
end
model.param.set('tmax',k)
model.study('std1').run;
end
end
end

Change the username and password in the client server

$
0
0
Hello,

I am working with the Livelink Comsol with Matlab. For some reason, I would like to change the username and password for the client server. Do anyone know how to do it?

Thanks!

Shujun

How to assign a parameter to each grid cell by COMSOL with MATLAB?

$
0
0
For example, in Darcy model, in order to realize heterogeneity for model, different permeability parameters which may satisfy a kind of distribution function should be assigned to each grid cell. How to call the grid information to assign value by COMSOL with MATLAB before calculation. In attach file, 'heterogeneity1.m' is a m. file for a simple PDE model and parameter k is permeability metioned above. The grid information is give in 'mesh.m'. Thanks for your attention!

accessing MATLAB global variables when running models in MATLAB

$
0
0
I have defined an external function in matlab that I use in my Comsol/Matlab livelink model to set a volume heat source, let's call this function my_heat(x,y,z), where x,y,z are comsol spatial variables.
Everything works fine with this (comsol passes x,y,z vectors and matlab returns heat source values based on this, and the model is set up and solved)

Now, I tried to use some global MATLAB variables within the 'my_heat' function. When I run function in MATLAB the variables and their values are accessible in 'my_heat'. However, when the same function is called by COMSOL as part of setting the physics, it seems 'my_heat' does not recognize these variables.

I think I know what the problem is, but don't know the solution to it:

When the model is run from Matlab, Comsol seem to open another instance of MATLAB (as manifested by a second command window). This seems like the place where all the Comsol/Matlab interfacing happens. No wonder that in this new instance of Matlab, the information about any global variables in the main Matlab window is missing.
Any ideas on how to 'pass' all the info from the main Matlab window to this auxiliary one?

Thanks,

Robert

P.S.: On an unrelated note, I discovered that by typing 'clear functions' in this new matlab window, I can have comsol get the updated version of any edits to my matlab fuinctions. Without this, comsol only has the first version of external functions. Typing 'clear functrions' in the main matlab window doesn't maker any difference.


How to determine face numbers (using matlab livelink) associated with an object

$
0
0
Hello again,

I am trying to 'automatically' set my boundary conditions for a model generated in matlab with livelink. I know which objects need to have which boundary conditions based on a draw order that I specify but I don't know how to get COMSOL to tell me which boundaries (i.e. face numbers) are associated with which object. I can get a list of face numbers associated with objects by the following (which another user has kindly posted elsewhere on this forum):

upDown = model.geom('geom1').getUpDown;
domain = cell(1,max(upDown(:))+1);
for i = 1:length(upDown)
domain{upDown(1,i)+1}=[domain{upDown(1,i)+1},i];
domain{upDown(2,i)+1}=[domain{upDown(2,i)+1},i];
end

but I don't understand how to link the resulting lists of elements with their associated object since they aren't listed according to draw order.

Export Mesh of selected domain via MATLAB

$
0
0
Dear all,

I'm currently working on a simulation approach which requires to export the COMSOL-mesh to a Abaqus Inputfile (.inp). Fortunately I already have some code lines that allows me to do so, since I have zero programming experience.

The problem with the exisiting code is, that it only writes out ~3000 nodes of the COMSOL-mesh regardless of the mesh settings (e.g. if I set the mesh to "very fine" it exports the same number of nodes as with "fine").

I think that this is caused by the function mpheval which is used. I tried to use mphmeshstats instead but couldn't get it to work.

What I need is quite simple. After some information lines the code should write out the node numbers of a selected domain (via domain numbers) and the corresponding coordinates x,y,z separated by a decimal comma. This is followed by another text line: *Elements, type=C3D10, elset=set1
After that the element numbers and information gets written. Finally some material definitions are set (not important).

I attached the existing code as MATLAB and Document to make sure that you can open it. Additionally I upload a example of how the Inputfile should look like at the end.

Anyone knows how to export the mesh properly? The export function which is implemented in COMSOL doesn't help, because you can't export a Abaqus Inputfile.

Thank you very much for your help!!

Best regards,
Maximilian


Feasibility of iteration with parametric sweep

$
0
0
We need to run the simulation iteratively in Comsol, which means we need the result of the first simulation as an input of the second simulation. Since the number of iteration is quite high, we have used Comsol with Matlab to run the iteration automatically and it works quite well. The only problem here is the simulation is too slow. So we try to use parametric sweep now, because the geometry and mesh has to be only generated once for the entire calculation, unlike previously in Comsol with Matlab, the geometry and mesh is generated in every iteration and it is very time-consuming. But in parametric sweep it seems that the values of all the parameters have to be given before we run the simulation. What we wish is that we can set the value of the second parameter after the simulation with the first parameter. Is it possible in Comsol?

Convergence plots

$
0
0
Does anyone know if it is possible to show a convergence plot during a livelink with Matlab? I'm using version 4.4 and matlab R2013a.

Thanks

Number of degrees of freedom per domain

$
0
0
Hello,

I'm using the Matlab LiveLink. The code below is used to obtain the total degrees of freedom of the model:

info = mphxmeshinfo(model);
info.ndofs

Is it possible to list the number of DOF present in a specific geometric domain, instead of the total number of DOF in the whole model?

Any help would be appreciated!

Martijn
Viewing all 2194 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>