Matrix Variables

本文深入探讨了Spring MVC中矩阵变量的使用方法,包括如何在路径段中包含名称值对,如何通过URL模板匹配请求,以及如何在映射中定义矩阵变量的可选性和默认值。
The URI specification RFC 3986 definesthe possibility of including name-value pairs within path segments. There is no specificterm used in the spec. The general "URI path parameters" could be applied although themore unique "Matrix URIs", originatingfrom an old post by Tim Berners-Lee, is also frequently used and fairly well known.Within Spring MVC these are referred to as matrix variables.

Matrix variables can appear in any path segment, each matrix variable separated with a";" (semicolon). For example: "/cars;color=red;year=2012". Multiple values may beeither "," (comma) separated "color=red,green,blue" or the variable name may berepeated "color=red;color=green;color=blue".

If a URL is expected to contain matrix variables, the request mapping pattern mustrepresent them with a URI template. This ensures the request can be matched correctlyregardless of whether matrix variables are present or not and in what order they areprovided.

Below is an example of extracting the matrix variable "q":

// GET /pets/42;q=11;r=22

@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)
public void findPet(@PathVariable String petId, @MatrixVariable int q) {

    // petId == 42
    // q == 11

}

Since all path segments may contain matrix variables, in some cases you need to be morespecific to identify where the variable is expected to be:

// GET /owners/42;q=11/pets/21;q=22

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
public void findPet(
        @MatrixVariable(value="q", pathVar="ownerId") int q1,
        @MatrixVariable(value="q", pathVar="petId") int q2) {

    // q1 == 11
    // q2 == 22

}

A matrix variable may be defined as optional and a default value specified:

// GET /pets/42

@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)
public void findPet(@MatrixVariable(required=false, defaultValue="1") int q) {

    // q == 1

}

All matrix variables may be obtained in a Map:

// GET /owners/42;q=11;r=12/pets/21;q=22;s=23

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
public void findPet(
        @MatrixVariable Map<String, String> matrixVars,
        @MatrixVariable(pathVar="petId"") Map<String, String> petMatrixVars) {

    // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
    // petMatrixVars: ["q" : 11, "s" : 23]

}

Note that to enable the use of matrix variables, you must set theremoveSemicolonContent property of RequestMappingHandlerMapping to false. Bydefault it is set to true.

[Tip]

The MVC Java config and the MVC namespace both provide options for enabling the use ofmatrix variables.

If you are using Java config, The Advanced Customizationswith MVC Java Config section describes how the RequestMappingHandlerMapping canbe customized.

In the MVC namespace, the <mvc:annotation-driven> element has anenable-matrix-variables attribute that should be set to true. By default it is setto false.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven enable-matrix-variables="true"/>

</beans>
Model Manager mmgetalltags - Get all assigned tags for a branch mmgetbranch - Get branch variable in Model Manager mmgetcommit - Get commit message from an item mmmodelinfo - Model info for models stored in Model Manager or as files mmsave - Save model in Model Manager mmsearch - Full-text and keyword search in Model Manager User interface functions mphcd - Change directory to the directory of the model mphdoc - Open documentation window mphload - Load a COMSOL model MPH-file mphopen - GUI for opening recent model files mphrun - Run various actions on nodes mphsave - Save a COMSOL model as a file mphversion - Return the version number of COMSOL Multiphysics mphlaunch - Launch COMSOL Multiphysics Client and import a model mphstart - Connect MATLAB to a COMSOL server mphstartcomsolmphserver - Start Comsol Multiphysics server Geometry functions mphgeom - Plot a geometry in a MATLAB figure mphgeominfo - Get geometry information mphimage2geom - Convert image to geometry mphviewselection - Display a geometric entity selection in a MATLAB figure Mesh functions mphmesh - Plot mesh in a MATLAB figure mphmeshstats - Retrieve mesh statistics and and mesh data information mphwritemesh - Write mesh data as an mphtxt, mphbin or STL file Utility functions mphbatchinfo - Get information about the batch solution structures mphevaluate - Evaluate parameter expressions in model mphgetadj - Return adjacency info for geometric entities mphgetcoords - Return point coordinates of geometry entities mphgetu - Return solution vector mphinputmatrix - Add a linear matrix system to a model mphinterpolationfile - Save data in file readable by the Interpolation feature mphmatrix - Get model matrices mphmeasure - Measure entites in a geometry mphquad2tri - Convert plotdata quad mesh into simplex mesh mphreadstl - Read an STL file and returns the data as a struct mphreduction - Return reduced order state space matrices for a model mphselectball - Select geometric entity using a ball selection mphselectbox - Select geometric entity using a rubberband/box mphselectcoords - Select geometric entity using point coordinates mphsolinfo - Get information about a solution object mphsolutioninfo - Get information about solution objects and data sets containing given parameters mphstate - Get state-space matrices for dynamic system mphwritestl - Export plot data as an STL file mphxmeshinfo - Extract information about the extended mesh Postprocessing functions mphaddplotdata - Add plot data to plotgroup mphcolortable - Return colortable data mphdataset - Plots a dataset mphdatasetinfo - Gets information about datasets mpheval - Evaluate expressions on node points mphevalglobalmatrix - Evaluate matrix variables mphevalpoint - Evaluate expressions at geometry vertices mphevalpointmatrix - Evaluate matrix quantities at points in the geometry mphglobal - Evaluate global quantities mphint2 - Perform integration of expressions mphinterp - Evaluate expressions in arbitrary points or data sets mphmax - Perform maximum of expressions mphmean - Perform mean of expressions mphmin - Perform minimum of expressions mphparticle - Evaluate expressions on particle trajectories mphplot - Render a plot group in a figure window mphray - Evaluate expressions on ray trajectories mphreport - Add report to model or run report generator mphsurf - Create plot data from surf data mphtable - Get table data Model information and navigation mphapplicationlibraries - GUI for viewing the Model Library mphgetexpressions - Get the model variables and parameters mphgetproperties - Get properties from a model node mphgetselection - Get information about a selection node mphgetview - Information about a view mphcomponentinfo - Gets information about components mphmodel - Return tags for the nodes and subnodes in the COMSOL model object mphnavigator - GUI for viewing the COMSOL model object mphsearch - GUI for searching expressions in the COMSOL model object mphsetparam - Set parameter values and descriptions mphshowerrors - Show messages in error and warning nodes in the COMSOL model object mphtags - Return tags and names for model nodes mphthumbnail - Get and set model thumbnail images COMSOL Multiphysics - LiveLink for Matlab Version 6.3 1-Oct-2024 Copyright 2010-2024 COMSOL Model Manager mmgetalltags - Get all assigned tags for a branch mmgetbranch - Get branch variable in Model Manager mmgetcommit - Get commit message from an item mmmodelinfo - Model info for models stored in Model Manager or as files mmsave - Save model in Model Manager mmsearch - Full-text and keyword search in Model Manager User interface functions mphcd - Change directory to the directory of the model mphdoc - Open documentation window mphload - Load a COMSOL model MPH-file mphopen - GUI for opening recent model files mphrun - Run various actions on nodes mphsave - Save a COMSOL model as a file mphversion - Return the version number of COMSOL Multiphysics mphlaunch - Launch COMSOL Multiphysics Client and import a model mphstart - Connect MATLAB to a COMSOL server mphstartcomsolmphserver - Start Comsol Multiphysics server Geometry functions mphgeom - Plot a geometry in a MATLAB figure mphgeominfo - Get geometry information mphimage2geom - Convert image to geometry mphviewselection - Display a geometric entity selection in a MATLAB figure Mesh functions mphmesh - Plot mesh in a MATLAB figure mphmeshstats - Retrieve mesh statistics and and mesh data information mphwritemesh - Write mesh data as an mphtxt, mphbin or STL file Utility functions mphbatchinfo - Get information about the batch solution structures mphevaluate - Evaluate parameter expressions in model mphgetadj - Return adjacency info for geometric entities mphgetcoords - Return point coordinates of geometry entities mphgetu - Return solution vector mphinputmatrix - Add a linear matrix system to a model mphinterpolationfile - Save data in file readable by the Interpolation feature mphmatrix - Get model matrices mphmeasure - Measure entites in a geometry mphquad2tri - Convert plotdata quad mesh into simplex mesh mphreadstl - Read an STL file and returns the data as a struct mphreduction - Return reduced order state space matrices for a model mphselectball - Select geometric entity using a ball selection mphselectbox - Select geometric entity using a rubberband/box mphselectcoords - Select geometric entity using point coordinates mphsolinfo - Get information about a solution object mphsolutioninfo - Get information about solution objects and data sets containing given parameters mphstate - Get state-space matrices for dynamic system mphwritestl - Export plot data as an STL file mphxmeshinfo - Extract information about the extended mesh Postprocessing functions mphaddplotdata - Add plot data to plotgroup mphcolortable - Return colortable data mphdataset - Plots a dataset mphdatasetinfo - Gets information about datasets mpheval - Evaluate expressions on node points mphevalglobalmatrix - Evaluate matrix variables mphevalpoint - Evaluate expressions at geometry vertices mphevalpointmatrix - Evaluate matrix quantities at points in the geometry mphglobal - Evaluate global quantities mphint2 - Perform integration of expressions mphinterp - Evaluate expressions in arbitrary points or data sets mphmax - Perform maximum of expressions mphmean - Perform mean of expressions mphmin - Perform minimum of expressions mphparticle - Evaluate expressions on particle trajectories mphplot - Render a plot group in a figure window mphray - Evaluate expressions on ray trajectories mphreport - Add report to model or run report generator mphsurf - Create plot data from surf data mphtable - Get table data Model information and navigation mphapplicationlibraries - GUI for viewing the Model Library mphgetexpressions - Get the model variables and parameters mphgetproperties - Get properties from a model node mphgetselection - Get information about a selection node mphgetview - Information about a view mphcomponentinfo - Gets information about components mphmodel - Return tags for the nodes and subnodes in the COMSOL model object mphnavigator - GUI for viewing the COMSOL model object mphsearch - GUI for searching expressions in the COMSOL model object mphsetparam - Set parameter values and descriptions mphshowerrors - Show messages in error and warning nodes in the COMSOL model object mphtags - Return tags and names for model nodes mphthumbnail - Get and set model thumbnail images。以上是COMSOL with matlab接口中的部分代码含义,我需要你学习并记住这段代码
最新发布
11-18
<think> 根据用户需求,用户希望学习COMSOL与MATLAB接口中的部分代码含义,包括Model Manager、用户界面函数、几何函数、网格函数、实用函数、后处理函数、模型信息和导航等。下面我将逐一解释这些类别下的常用函数及其含义。 ### 1. Model Manager (模型管理器) Model Manager用于创建、打开、保存和管理COMSOL模型文件(.mph文件)。常用函数如下: - `comsolmphserver`: 启动COMSOL服务器进程(后台运行)。 - `mphopen(modelname)`: 打开一个现有的COMSOL模型文件,返回模型对象。 - `mphsave(model, filename)`: 将模型对象保存为.mph文件。 - `mphnavigator`: 返回模型导航树结构,便于遍历模型组件。 ### 2. 用户界面函数 这些函数用于控制COMSOL图形用户界面(GUI)的显示和交互。 - `mphlaunch`: 启动COMSOL图形用户界面。 - `mphstart(model)`: 在COMSOL GUI中打开指定模型。 - `mphplot(model, plotgroup)`: 在MATLAB中绘制COMSOL模型的指定绘图组。 ### 3. 几何函数 用于创建和修改几何模型。 - `model.geom.create(tag, type)`: 创建一个几何节点。例如:`geom = model.geom.create('geom1', 3)` 创建三维几何。 - `model.geom(tag).feature().create(tag, type)`: 在几何节点下创建几何特征(如立方体、球体等)。例如:`block = geom.feature().create('block1', 'Block')` 创建立方体。 - `model.geom(tag).run()`: 执行几何构建操作。 ### 4. 网格函数 用于生成和修改网格。 - `model.mesh.create(tag, 'geom')`: 创建一个网格节点,并关联到指定几何。例如:`mesh = model.mesh.create('mesh1', 'geom1')`。 - `model.mesh(tag).create(tag, 'FreeTet')`: 在网格节点下创建网格划分特征(如自由四面体网格)。例如:`mesh.feature().create('ftet1', 'FreeTet')`。 - `model.mesh(tag).run()`: 运行网格划分。 ### 5. 物理场函数 用于设置物理场、边界条件和材料属性。 - `model.physics.create(tag, 'HeatTransfer', 'geom1')`: 在指定几何上创建物理场(如传热)。例如:`physics = model.physics.create('ht', 'HeatTransfer', 'geom1')`。 - `model.physics(tag).feature().create(tag, 'ConvectiveHeatFlux', boundary)`: 在指定边界上创建边界条件(如对流热通量)。 - `model.material.create(tag, 'Material')`: 创建材料节点。例如:`mat = model.material.create('mat1')`。 - `model.material(tag).propertyGroup.create(tag, 'Basic')`: 为材料创建属性组(如基本热属性)。 ### 6. 实用函数 提供辅助功能,如设置参数、变量和函数。 - `model.param.set(name, expression)`: 设置全局参数。例如:`model.param.set('length', '10[cm]')`。 - `model.variable.create(tag)`: 创建变量节点。例如:`var = model.variable.create('var1')`。 - `model.variable(tag).set(name, expression)`: 设置变量表达式。 ### 7. 后处理函数 用于结果提取和可视化。 - `model.result.create(tag, 'PlotGroup')`: 创建绘图组。例如:`plotGroup = model.result.create('pg1', 'PlotGroup')`。 - `model.result(tag).create(tag, 'Surface')`: 在绘图组下创建表面绘图。 - `model.result(tag).run()`: 运行绘图组以生成结果。 - `mphmax(model, expression)`: 计算模型中表达式的最大值。 - `mphinterp(model, expression, coordinates)`: 在指定坐标点插值结果表达式。 ### 8. 模型信息和导航 用于获取模型结构信息。 - `mphmodel(model)`: 返回模型的完整结构信息(树形结构)。 - `mphgetexpressions(node)`: 获取节点(如参数、变量)的表达式。 - `mphgetselection(node)`: 获取节点(如边界条件)的几何选择。 ### 示例代码 以下是一个简单的示例,展示如何使用API创建一个简单的热传导模型: ```matlab import com.comsol.model.* import com.comsol.model.util.* % 创建新模型 model = ModelUtil.create('Model'); % 创建几何 geom = model.geom.create('geom1', 3); block = geom.feature().create('block1', 'Block'); block.set('size', {'1' '1' '1'}); geom.run; % 创建网格 mesh = model.mesh.create('mesh1', 'geom1'); mesh.feature().create('ftet1', 'FreeTet'); mesh.run; % 创建传热物理场 physics = model.physics.create('ht', 'HeatTransfer', 'geom1'); physics.feature('hteq1').set('k', '0.5'); % 设置热导率 % 设置边界条件 source = physics.feature().create('hs1', 'HeatSource', 2); % 2表示体选择 source.set('Q0', '1000'); % 设置热源功率 source.selection.set(1); % 选择整个几何体(体1) sink = physics.feature().create('temp1', 'Temperature', 1); % 1表示边界选择 sink.set('T0', '300'); % 设置温度 sink.selection.set([1,2,3,4,5,6]); % 选择所有边界 % 创建研究 study = model.study.create('std1'); study.feature().create('stat', 'Stationary'); model.sol.create('sol1'); study.feature('stat').set('plist', '1'); % 设置参数列表(此处无参数) model.sol('sol1').feature().create('s1', 'StudyStep'); model.sol('sol1').feature().create('v1', 'Variables'); model.sol('sol1').feature().create('s2', 'Stationary'); model.sol('sol1').feature('s2').feature().create('fc1', 'FullyCoupled'); model.sol('sol1').run; % 后处理 plotGroup = model.result.create('pg1', 'PlotGroup'); surface = plotGroup.feature().create('surf1', 'Surface'); surface.set('expr', 'T'); plotGroup.run; mphplot(model, 'pg1'); ``` ### 注意事项 - 在MATLAB中使用COMSOL API时,需确保COMSOL与MATLAB已正确连接(通过COMSOL安装目录下的`mphstart.m`初始化)。 - 函数参数中的`tag`为字符串,用于唯一标识节点或特征。 - 几何选择(selection)通常通过索引(整数数组)指定。 §§相关问题§§ 1. 如何通过API在COMSOL中创建参数化扫描研究? 2. 如何使用COMSOL API导出结果数据(如温度场)到MATLAB数组? 3. COMSOL API中如何处理复杂几何的选择(如通过坐标选择边界)? </think> ### COMSOL with MATLAB 接口核心代码含义详解 以下是COMSOL与MATLAB接口中关键函数分类及含义解析,涵盖您提到的所有模块: #### 一、Model Manager(模型管理器) 1. **`mphopen('model.mph')`** - 含义:打开COMSOL模型文件 - 示例:`model = mphopen('thermal_model.mph');` - 作用:加载模型到MATLAB工作空间 2. **`mphsave(model, 'new_model.mph')`** - 含义:保存当前模型状态 - 参数:`model`对象 + 保存路径 #### 二、用户界面函数 3. **`mphlaunch`** - 含义:启动COMSOL图形界面 - 注意:需配合`mphstart(model)`使用 4. **`mphnavigator`** - 含义:获取模型树形结构 - 输出:返回模型组件层级关系 ```matlab nav = mphnavigator(model); disp(nav.tags); % 显示所有组件标签 ``` #### 三、几何函数 5. **`model.geom.create('geom1', 3)`** - 含义:创建3D几何体 - 参数:`3`表示三维,`2`表示二维 6. **`model.geom('geom1').feature.create('cyl', 'Cylinder')`** - 含义:创建圆柱体 - 参数设置: ```matlab cyl = model.geom('geom1').feature('cyl'); cyl.set('r', 5); % 半径 cyl.set('h', 10); % 高度 ``` #### 四、网格函数 7. **`model.mesh.create('mesh1', 'geom1')`** - 含义:为几何体创建网格 - 关联:绑定到几何标签'geom1' 8. **`model.mesh('mesh1').feature.create('ftet', 'FreeTet')`** - 含义:创建四面体网格 - 设置参数: ```matlab ftet = model.mesh('mesh1').feature('ftet'); ftet.set('maxh', 0.5); % 最大网格尺寸 ``` #### 五、实用函数 9. **`mphglobal(model)`** - 含义:获取全局求解参数 - 输出:返回结构体包含所有变量值 10. **`mphinterp(model, 'T', 'coord', [x;y;z])`** - 含义:在指定坐标点插值场变量 - 示例:在(1,2,3)处获取温度值 ```matlab T_val = mphinterp(model, 'T', 'coord', [1;2;3]); ``` #### 六、后处理函数 11. **`model.result.create('pg1', 'PlotGroup')`** - 含义:创建绘图组 - 用途:结果可视化容器 12. **`model.result('pg1').create('surf', 'Surface')`** - 含义:创建表面图 - 参数设置: ```matlab surf = model.result('pg1').feature('surf'); surf.set('expr', 'T'); % 绘制温度场 ``` #### 七、模型信息和导航 13. **`mphgetproperties(model)`** - 含义:获取模型所有属性 - 输出:返回结构化的元数据 14. **`mphgetselection(model.geom('geom1').feature('cyl'))`** - 含义:获取几何特征的选择域 - 用途:用于边界条件设置 #### 八、参数化研究函数 15. **`model.study.create('paramStudy', 'Parametric')`** - 含义:创建参数化研究 - 参数设置: ```matlab param = model.study('paramStudy').feature('param'); param.set('plist', 'range(0,1,10)'); % 参数范围 ``` ### 应用示例:圆柱体热分析 ```matlab model = ModelUtil.create('Model'); % 创建新模型 geom = model.geom.create('geom1', 3); % 3D几何 cyl = geom.feature.create('cyl', 'Cylinder'); % 圆柱体 cyl.set('r', 5); cyl.set('h', 10); geom.run; % 执行几何构建 % 物理场设置 physics = model.physics.create('ht', 'HeatTransfer', 'geom1'); physics.feature('hs1').set('Q0', 1000); % 热源功率 % 求解并获取结果 model.study.create('std1', 'Stationary'); model.sol.create('sol1'); model.sol('sol1').run; T_max = mphglobal(model, 'max(T)'); % 获取最高温度 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值