英文:
How can I assign elements of an array in Modelica's equation and algorithm sections?
问题
我正在尝试通过离散化来解决两个耦合的偏微分方程的边界值问题。为此,我需要按顺序解决这两个微分方程,并在单个时间步长内迭代循环,直到它们的值收敛。
我考虑在方程部分分配边界值,因为它们将始终有效。由于我需要迭代地解决这些偏微分方程,我将它们实现在算法部分,以便它们可以并入一个while循环中。
然而,由于Modelica规范11.1.2,在算法部分初始化整个数组,这导致模型变为奇异,无法解决,因为方程太多(在这种情况下,有4个方程太多)。是否有任何方法可以为这个问题指定边界条件,因为当前的方法似乎不可行?
以下是我的代码片段。我省略了算法部分的RHS变量声明,但这些也将作为语句列出。n和C是与我试图解决的偏微分方程相关的变量。它们是长度为N的数组,其中N是离散化元素的数量。
equation
// PDE 1 Bounds
n[1] = n[2]; // dn/dx = 0 at X = 0
n[N] = n[N - 1]; // dn/dx = 0 at X = N
// PDE 2 Bounds
// 边界条件
C[1] = C[2]; // dC/dx = at X = 0
C[N] = RH*Psat/(R*T); // Boundary Condition at X = N.
algorithm
// 这一部分应该在while循环中运行
// PDE 1 Interior Node Calculation
for i in 2:N-1 loop
der(n[i]) :=(((D1[i + 1]*n[i + 1] + D1[i - 1]*n[i - 1] - 2*D1[i]*n[i])/dx^2)
+ PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(1 - V_g0);
end for;
// PDE 2 Exterior Node Calculation
for i in 2:N-1 loop
der(C[i]) :=(((Deff[i + 1]*C[i + 1] + Deff[i - 1]*C[i - 1] - 2*Deff[i]*C[i])
/dx^2) + PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(V_g[i]);
end for;
// 更新收敛标准
希望这对你有所帮助。
英文:
I am trying to solve a boundary value problem for two coupled PDEs by discritizing. For this I need to solve the two differential equations sequentially and iteratively loop until their values converge for a single timestep.
I thought to assign the boundary values in the equation section as they will always be valid. Because I need to iteratively solve the PDEs until the values converge, I implemented them in an algorithm section so they could be incorporated in a while loop.
However, due to Modelica specification 11.1.2 the entire array is initialized in the algorithm section and this causes the model to become singular and can't be solved, as there are too many equations (in this case there are 4 too many equations). Is there any way specify the boundary conditions for this problem, as this current method does not seem possible?
Here is a snippet of my code. I omitted the decleration of RHS variables for the statements in the algorithm section, but those would also be listed as satements. n and C are the variables relating to the PDEs I am trying to solve. They are arrays of length N, where N is the number of discritized elements.
equation
// PDE 1 Bounds
n[1] = n[2]; // dn/dx = 0 at X = 0
n[N] = n[N - 1];// dn/dx = 0 at X = N
// PDE 2 Bounds
// Boundary Conditions
C[1] = C[2]; // dC/dx = at X = 0
C[N] = RH*Psat/(R*T); // Boundary Condition at X = N.
algorithm
//This section should run in a while loop
// PDE 1 Interior Node Calculation
for i in 2:N-1 loop
der(n[i]) :=(((D1[i + 1]*n[i + 1] + D1[i - 1]*n[i - 1] - 2*D1[i]*n[i])/dx^2)
+ PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(1 - V_g0);
end for;
// PDE 2 Exterior Node Calculation
for i in 2:N-1 loop
der(C[i]) :=(((Deff[i + 1]*C[i + 1] + Deff[i - 1]*C[i - 1] - 2*Deff[i]*C[i])
/dx^2) + PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(V_g[i]);
end for;
//Update convergence criteria
答案1
得分: 1
有一种可能性是创建辅助数组:
Real smallN[N-2]=n[2:N-1];
Real smallC[N-2]=C[2:N-1];
方程
// PDE 1 边界条件
n[1] = n[2]; // 在 X = 0 处 dn/dx = 0
n[N] = n[N - 1];// 在 X = N 处 dn/dx = 0
// PDE 2 边界条件
// 边界条件
C[1] = C[2]; // 在 X = 0 处 dC/dx =
C[N] = RH*Psat/(R*T); // 在 X = N 处的边界条件。
算法
//此部分应在 while 循环中运行
// PDE 1 内部节点计算
for i in 2:N-1 loop
der(smallN[i]) :=(((D1[i + 1]*n[i + 1] + D1[i - 1]*n[i - 1] - 2*D1[i]*n[i])/dx^2)
+ PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(1 - V_g0);
end for;
// PDE 2 外部节点计算
for i in 2:N-1 loop
der(smallC[i]) :=(((Deff[i + 1]*C[i + 1] + Deff[i - 1]*C[i - 1] - 2*Deff[i]*C[i])
/dx^2) + PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(V_g[i]);
end for;
// 更新收敛条件
但是没有整个模型,我没有详细检查它。
英文:
One possibility is to create helper arrays:
Real smallN[N-2]=n[2:N-1];
Real smallC[N-2]=C[2:N-1];
equation
// PDE 1 Bounds
n[1] = n[2]; // dn/dx = 0 at X = 0
n[N] = n[N - 1];// dn/dx = 0 at X = N
// PDE 2 Bounds
// Boundary Conditions
C[1] = C[2]; // dC/dx = at X = 0
C[N] = RH*Psat/(R*T); // Boundary Condition at X = N.
algorithm
//This section should run in a while loop
// PDE 1 Interior Node Calculation
for i in 2:N-1 loop
der(smallN[i]) :=(((D1[i + 1]*n[i + 1] + D1[i - 1]*n[i - 1] - 2*D1[i]*n[i])/dx^2)
+ PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(1 - V_g0);
end for;
// PDE 2 Exterior Node Calculation
for i in 2:N-1 loop
der(smallC[i]) :=(((Deff[i + 1]*C[i + 1] + Deff[i - 1]*C[i - 1] - 2*Deff[i]*C[i])
/dx^2) + PN*S[i]*Dv[i]*(aws[i]*Psat/(R*T) - C[i]))/(V_g[i]);
end for;
//Update convergence criteria
But without the entire model I haven't checked it in detail.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论