1

我试着让它变短。我遇到了一个问题,涉及对带有气体管道的流动进行数值模拟。我使用不同的模型来模拟从一个边界到另一个边界(即从右到左)的气流(传输方程)。问题是,因为我使用不同的模型来表示所述气体流量和边界条件,所以我得到了很多函数,例如:

boundary_left_model_1
boundary_right_model_1
boundary_left_model_2
boundary_right_model_2
boundary_left_model_2b
boundary_right_model_2b
....
flux_model_1
flux_model_2
....
source_model_1
source_model_2
.....

我通过(例如)在数值方案之前分配所需的功能:

boundary_left = @[needed function];
boundary_right=@ [needed function];
flux = @[needed function];
source=@ [needed function];

接下来是这样的:

Solution = numerical_scheme_#1(boundary_left,boundary_right,flux,source,parameters);

你明白了。

现在,构建程序的最有效方式(在结构化时)是什么?据我所知,我有三个选择:

1)我在一个matlab函数文件中定义了每个函数(会产生很多文件)

2)我在脚本中为数值方案定义了每个函数(将导致长文件,调整/读取不太清楚)

3)我为边界条件创建了一个类,其中包含所有模型的方法,一个包含相应函数的通量和源类。在我的数值方案的函数分配中,我然后调用我需要的方法。(似乎复杂且低效,不确定在matlab中调用方法所需的时间,但对我来说是最结构化的方式)

我可能应该注意到,我对 matlab 和数值模拟(学生)相对较新,而且我也在问这种问题通常是如何解决的。提前致谢!另外作为奖励:如果有人喜欢使用 matlab 对 PDE 系统进行实际模拟——我会遇到诸如“我如何决定数值方案——我应该考虑哪些标准?”之类的问题。

再次感谢,祝大家有个愉快的一天!

4

1 回答 1

0

假设与模型 1 关联的方法总是一起使用,而不是与模型 2 或 3 的方法混合使用,那么您可以在一个文件中设置具有一个模型的所有函数的代码:

% MODEL1   Methods that implement model 1
function [boundary_left,boundary_right,flux,source] = model1
boundary_left = @boundary_left_method;
boundary_right = @boundary_right_method;
flux = @flux_method;
source = @source_method;

function [out, args] = boundary_left_method(input, args)
  % [implementation]
end

function [out, args] = boundary_right_method(input, args)
  % [implementation]
end

function [out, args] = flux_method(input, args)
  % [implementation]
end

function [out, args] = source_method(input, args)
  % [implementation]
end

end

基本上,这里有一个函数,它将句柄返回给实现一个方法的一组函数。boundary_left_method是一个私有函数,因此不能直接访问,但model1可以返回这些函数的句柄,这使得它们可以访问。

您现在可以执行以下操作:

[boundary_left,boundary_right,flux,source] = model1;
Solution = numerical_scheme_1(boundary_left,boundary_right,flux,source,parameters);

此解决方案与 OP 建议的自定义类解决方案非常相似,但更简单一些。我认为性能不会有很大差异,但唯一确定的方法是实施和计时各种选项。随着 MATLAB 的 JIT 改进,随着时间的推移,最有效的变化是什么。

请注意,此处提出的方案还允许将数据 ( parameters) 包含在这些函数中:

% MODEL1   Methods that implement model 1
function [boundary_left,boundary_right,flux,source] = model1(parameters)
boundary_left = @boundary_left_method;
boundary_right = @boundary_right_method;
flux = @flux_method;
source = @source_method;

function [out, args] = boundary_left_method(input, args)
  out = input * parameters; % You can use PARAMETERS here, note that it is not
                            % an input argument to this nested function, it is
                            % found in the parent scope.
end

% ...

end % This end here is important now, this way `boundary_left_method` is an
    % nested function, not simply a separate function within the same file.

现在返回的函数句柄中嵌入boundary_left了 的数据。parameters请参阅相关文档


如果您还控制采用这些函数句柄的数值方案函数的代码,您可以编写method1et al. 改为返回句柄元胞数组,以及numerical_scheme_1等。函数来获取句柄的元胞数组。然后你可以简单地做:

numerical_scheme_1(method1,parameters);
numerical_scheme_1(method2,parameters);
numerical_scheme_1(method3,parameters);
numerical_scheme_2(method1,parameters);
numerical_scheme_2(method2,parameters);
numerical_scheme_2(method3,parameters);
% etc.

然后,您可以使用循环遍历所有组合:

schemes = {@numerical_scheme_1, @numerical_scheme_2, ... };
methods = {method1, method2, method3, ... };
for ii=1:numel(schemes)
   for jj=1:numel(methods)
      schemes{ii}(methods{jj},parameters);
   end
end

[免责声明:此代码未经测试,但我不明白为什么它不起作用......]

于 2018-09-28T16:08:50.837 回答