Extracting System Matrices
Extract the matrices of the COMSOL Multiphysics linearized system with the function mphmatrix. To call the function mphmatrix, specify a solver node and the list of the system matrices to extract:
str = mphmatrix(model, <soltag>, 'out', out)
where <soltag> is the solver node tag used to assemble the system matrices and out is a cell array containing the list of the matrices to evaluate. The output data str returned by mphmatrix is a MATLAB® structure, and the fields correspond to the assembled system matrices.
See the Advanced section and the Assemble section in the COMSOL Multiphysics Reference Manual, for more information about matrix evaluation.
The system matrices that can be extracted with mphmatrix are listed in the table:
Selecting Linearization Points
The default selection of linearization points for the system matrix assembly is the current solution of the solver node associated to the assembly.
If the linearization point is not specified when calling mphmatrix, the COMSOL Multiphysics software automatically runs the entire solver configuration before assembling and extracting the matrices.
You can save time during the evaluation by manually setting the linearization point. Use the initmethod property as in this command:
str = mphmatrix(model,<soltag>,'out',out,'initmethod',method)
where method corresponds to the type of linearization point — the initial value expression ('init') or a solution ('sol').
To set the solution to use for the linearization point, use the property initsol:
str = mphmatrix(model,<soltag>,'out',out,'initsol',<initsoltag>)
where <initsoltag> is the solver tag to use for linearization points. You can also set the initsol property to 'zero', which corresponds to using a null solution vector as a linearization point. The default is the current solver node where the assemble node is associated.
For continuation, time-dependent, or eigenvalue analyses you can set the solution number to use as a linearization point. Use the solnum property:
str = mphmatrix(model,<soltag>,'out',out,'solnum',<solnum>)
where <solnum> is an integer value corresponding to the solution number. The default value is the last solution number available with the current solver configuration.
See Retrieving Xmesh Information to learn how to get relation between the degrees of freedom information in the matrix system and coordinates or element information.
Specifying when to assemble the matrices in the solution sequence
You can specify when in the solution sequence to assemble the system matrices, for instance after computing the solution or if you have a solution sequence combining different solver. By default, the system matrices are assembled before running the first solver, just after the first dependent variable node in the solution sequence. To specify the node that precede the matrix extraction, use the extractafter property:
str = mphmatrix(model,<soltag>,'out',out,'extractafter',<nodetag>)
where <nodetag> is the tag of a solution sequence node such as dependent variable or solver nodes.
Eigenvalue problems
For eigenvalue problems, it is necessary to specify the eigenvalue name and the eigenvalue linearization point. Use the property eigname to specify the name of the eigenvalue and eigref to specify the value of eigenvalue linearization point:
str = mphmatrix(model,<soltag>,'out',out,'eigname',<eigname>)
str = mphmatrix(model,<soltag>,'out',out,'eigname',<eigname>,... 'eigref', <eigref>)
where <eigname> is a string and <eigref> a possibly complex number.
row equilibration, matrix symmetry, and null-space function
The default assembly of the system matrices assumes row equilibration of the system matrices. It is, however, possible to extract the unscaled matrices; to proceed, set the rowscale property to off:
str = mphmatrix(model,<soltag>,'out',out,'rowscale','off')
Set the symmetry property to specify manually the symmetry type for the matrix evaluation. The symmetry property supports the following values:
str = mphmatrix(model,<soltag>,'out',out,'symmetry',sym)
where sym can be either of one of the following value:
'on', to assemble and extract the system matrices as symmetric.
'off', to assemble and extract the system matrices as nonsymmetric.
'hermitian', to assemble and extract the system matrices as hermitian.
'auto', to let the solver assembly determine the type of the system matrices.
Use the nullfun property to specify the method for computation of matrices needed for constraint handling:
'flnullorth' — a method based on singular value decomposition.
'flspnull' — to handle constraint matrices with nonlocal couplings using singular sparse algorithm.
'explicitsp' — to handle constraints by explicitly eliminating the DOFs on the destination side of the explicit constraints. The remaining constraints are handled using the Sparse method.
'explicitorth' — to handle constraints by explicitly eliminating the DOFs on the destination side of the explicit constraints. The remaining constraints are handled using the Orthonormal method.
'auto' — to let the software automatically determine the most appropriate method, which uses an explicit handling of nodal constraints and one of the Orthonormal or Sparse methods for the remaining constraints.
Complex Function
If the system contains a complex function, use the property complexfun to specify how to handle such a function. Set this property to on to use a complex-valued function with real input:
str = mphmatrix(model,<soltag>,'out',out,'complexfun','on');
Handling undefined operations
It is possible to disable the error for undefined operations during the assembly and matrix evaluation, to proceed set the property matherr to off as in the command below:
str = mphmatrix(model,<soltag>,'out',out,'matherr','off')
Extracting the System Matrices
The following illustrates how to use the mphmatrix command to extract eliminated system matrices of a stationary analysis and linear matrix system at the MATLAB prompt.
The model consists of a linear heat transfer problem solved on a unit square with a 1e5 W/m^2 surface heat source and temperature constraint. Only one quarter of the geometry is represented in the model. For simplification reasons, the mesh is made of four quad elements and the discretization is set with linear element.
These commands set the COMSOL model object:
model = ModelUtil.create('Model2');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
geom1.feature.create('sq1', 'Square');
geom1.run;
 
mat1 = comp1.material.create('mat1');
def = mat1.materialModel('def');
def.set('thermalconductivity',4e2);
 
ht = comp1.physics.create('ht', 'HeatTransfer', 'geom1');
ht.prop('ShapeProperty').set('boundaryFlux_temperature',false);
ht.prop('ShapeProperty').set('order_temperature',1);
hs1 = ht.feature.create('hs1','HeatSource',2);
hs1.selection.set(1);
hs1.set('Q',1,1e5);
 
temp1 = ht.feature.create('temp1','TemperatureBoundary',1);
temp1.selection.set([1 2]);
 
mesh1 = comp1.mesh.create('mesh1');
dis1 = mesh1.feature.create('dis1','Distribution');
dis1.selection.set([1 2]);
dis1.set('numelem',2);
mesh1.feature.create('map1','Map');
 
std1 = model.study.create('std1');
std1.feature.create('stat','Stationary');
std1.run;
To extract the solution vector of the computed solution, run the function mphgetu as in this command:
U = mphgetu(model);
To assemble and extract the eliminated stiffness matrix and the eliminated load vector, set the linearization point to the initial value expression by entering:
MA = mphmatrix(model ,'sol1', ...
'Out', {'Kc','Lc','Null','ud','uscale'},...
'initmethod','sol','initsol','zero');
Solve for the eliminated solution vector using the extracted eliminated system:
Uc = MA.Null*(MA.Kc\MA.Lc);
Combine the eliminated solution vector and the particular vector:
U0 = Uc+MA.ud;
Scale back the solution vector:
U1 = U0.*MA.uscale;
Now compare the solution vectors U and U1 computed by COMSOL Multiphysics and by the matrix operation, respectively.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
geom1.feature.create('sq1', 'Square');
geom1.run;
mat1 = comp1.material.create('mat1');
def = mat1.materialModel('def');
def.set('thermalconductivity',4e2);
ht = comp1.physics.create('ht', 'HeatTransfer', 'geom1');
ht.prop('ShapeProperty').set('boundaryFlux_temperature',false);
ht.prop('ShapeProperty').set('order_temperature',1);
hs1 = ht.feature.create('hs1','HeatSource',2);
hs1.selection.set(1);
hs1.set('Q',1,1e5);
temp1 = ht.feature.create('temp1','TemperatureBoundary',1);
temp1.selection.set([1 2]);
mesh1 = comp1.mesh.create('mesh1');
dis1 = mesh1.feature.create('dis1','Distribution');
dis1.selection.set([1 2]);
dis1.set('numelem',2);
mesh1.feature.create('map1','Map');
std1 = model.study.create('std1');
std1.feature.create('stat','Stationary');
std1.run;
U = mphgetu(model);
MA = mphmatrix(model ,'sol1', ...
'Out', {'Kc','Lc','Null','ud','uscale'},...
'initmethod','sol','initsol','zero');
Uc = MA.Null*(MA.Kc\MA.Lc);
U0 = Uc+MA.ud;
U1 = U0.*MA.uscale;