C#,数值计算——偏微分方程,Mglin的计算方法与源程序

1 文本格式

using System;
using System.Collections.Generic;

namespace Legalsoft.Truffer
{
    public class Mglin
    {
        private int n { get; set; }
        private int ng { get; set; }
        private double[,] uj1 { get; set; }
        private List<double[,]> rho { get; set; } = new List<double[,]>();

        public Mglin(double[,] u, int ncycle)
        {
            this.n = u.GetLength(0);
            this.ng = 0;
            int nn = n;
            while ((nn >>= 1) != 0)
            {
                ng++;
            }
            if ((n - 1) != (1 << ng))
            {
                throw new Exception("n-1 must be a power of 2 in mglin.");
            }
            nn = n;
            int ngrid = ng - 1;
            //rho.resize(ng);
            rho = new List<double[,]>(ng);
            rho[ngrid] = new double[nn, nn];
            rho[ngrid] = u;
            while (nn > 3)
            {
                nn = nn / 2 + 1;
                rho[--ngrid] = new double[nn, nn];

                //rstrct(ref rho[ngrid], rho[ngrid + 1]);
                double[,] tmp = rho[ngrid];
                rstrct(tmp, rho[ngrid + 1]);
                //rho[ngrid] = new double[,](tmp);
                rho[ngrid] = Globals.CopyFrom(tmp);
            }
            nn = 3;
            double[,] uj = new double[nn, nn];
            slvsml(uj, rho[0]);
            for (int j = 1; j < ng; j++)
            {
                nn = 2 * nn - 1;
                uj1 = uj;
                uj = new double[nn, nn];
                interp(uj, uj1);
                uj1 = null;
                for (int jcycle = 0; jcycle < ncycle; jcycle++)
                {
                    mg(j, uj, rho[j]);
                }
            }
            u = uj;
        }

        public void interp(double[,] uf, double[,] uc)
        {
            int nf = uf.GetLength(0);
            int nc = nf / 2 + 1;
            for (int jc = 0; jc < nc; jc++)
            {
                for (int ic = 0; ic < nc; ic++)
                {
                    uf[2 * ic, 2 * jc] = uc[ic, jc];
                }
            }
            for (int jf = 0; jf < nf; jf += 2)
            {
                for (int iif = 1; iif < nf - 1; iif += 2)
                {
                    uf[iif, jf] = 0.5 * (uf[iif + 1, jf] + uf[iif - 1, jf]);
                }
            }
            for (int jf = 1; jf < nf - 1; jf += 2)
            {
                for (int iif = 0; iif < nf; iif++)
                {
                    uf[iif, jf] = 0.5 * (uf[iif, jf + 1] + uf[iif, jf - 1]);
                }
            }
        }

        public void addint(double[,] uf, double[,] uc, double[,] res)
        {
            int nf = uf.GetLength(0);
            interp(res, uc);
            for (int j = 0; j < nf; j++)
            {
                for (int i = 0; i < nf; i++)
                {
                    uf[i, j] += res[i, j];
                }
            }
        }

        public void slvsml(double[,] u, double[,] rhs)
        {
            double h = 0.5;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    u[i, j] = 0.0;
                }
            }
            u[1, 1] = -h * h * rhs[1, 1] / 4.0;
        }

        public void relax(double[,] u, double[,] rhs)
        {
            int n = u.GetLength(0);
            double h = 1.0 / (n - 1);
            double h2 = h * h;
            for (int ipass = 0, jsw = 1; ipass < 2; ipass++, jsw = 3 - jsw)
            {
                for (int j = 1, isw = jsw; j < n - 1; j++, isw = 3 - isw)
                {
                    for (int i = isw; i < n - 1; i += 2)
                    {
                        u[i, j] = 0.25 * (u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - h2 * rhs[i, j]);
                    }
                }
            }
        }

        public void resid(double[,] res, double[,] u, double[,] rhs)
        {
            int n = u.GetLength(0);
            double h = 1.0 / (n - 1);
            double h2i = 1.0 / (h * h);
            for (int j = 1; j < n - 1; j++)
            {
                for (int i = 1; i < n - 1; i++)
                {
                    res[i, j] = -h2i * (u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - 4.0 * u[i, j]) + rhs[i, j];
                }
            }
            for (int i = 0; i < n; i++)
            {
                res[i, 0] = res[i, n - 1] = res[0, i] = res[n - 1, i] = 0.0;
            }
        }

        public void rstrct(double[,] uc, double[,] uf)
        {
            int nc = uc.GetLength(0);
            int ncc = 2 * nc - 2;
            for (int jf = 2, jc = 1; jc < nc - 1; jc++, jf += 2)
            {
                for (int iif = 2, ic = 1; ic < nc - 1; ic++, iif += 2)
                {
                    uc[ic, jc] = 0.5 * uf[iif, jf] + 0.125 * (uf[iif + 1, jf] + uf[iif - 1, jf] + uf[iif, jf + 1] + uf[iif, jf - 1]);
                }
            }
            for (int jc = 0, ic = 0; ic < nc; ic++, jc += 2)
            {
                uc[ic, 0] = uf[jc, 0];
                uc[ic, nc - 1] = uf[jc, ncc];
            }
            for (int jc = 0, ic = 0; ic < nc; ic++, jc += 2)
            {
                uc[0, ic] = uf[0, jc];
                uc[nc - 1, ic] = uf[ncc, jc];
            }
        }

        public void mg(int j, double[,] u, double[,] rhs)
        {
            const int NPRE = 1;
            const int NPOST = 1;
            int nf = u.GetLength(0);
            int nc = (nf + 1) / 2;
            if (j == 0)
            {
                slvsml(u, rhs);
            }
            else
            {
                double[,] res = new double[nc, nc];
                double[,] v = new double[nc, nc];
                double[,] temp = new double[nf, nf];
                for (int jpre = 0; jpre < NPRE; jpre++)
                {
                    relax(u, rhs);
                }
                resid(temp, u, rhs);
                rstrct(res, temp);
                mg(j - 1, v, res);
                addint(u, v, temp);
                for (int jpost = 0; jpost < NPOST; jpost++)
                {
                    relax(u, rhs);
                }
            }
        }
    }
}
 

2 代码格式

using System;
using System.Collections.Generic;

namespace Legalsoft.Truffer
{
    public class Mglin
    {
        private int n { get; set; }
        private int ng { get; set; }
        private double[,] uj1 { get; set; }
        private List<double[,]> rho { get; set; } = new List<double[,]>();

        public Mglin(double[,] u, int ncycle)
        {
            this.n = u.GetLength(0);
            this.ng = 0;
            int nn = n;
            while ((nn >>= 1) != 0)
            {
                ng++;
            }
            if ((n - 1) != (1 << ng))
            {
                throw new Exception("n-1 must be a power of 2 in mglin.");
            }
            nn = n;
            int ngrid = ng - 1;
            //rho.resize(ng);
            rho = new List<double[,]>(ng);
            rho[ngrid] = new double[nn, nn];
            rho[ngrid] = u;
            while (nn > 3)
            {
                nn = nn / 2 + 1;
                rho[--ngrid] = new double[nn, nn];

                //rstrct(ref rho[ngrid], rho[ngrid + 1]);
                double[,] tmp = rho[ngrid];
                rstrct(tmp, rho[ngrid + 1]);
                //rho[ngrid] = new double[,](tmp);
                rho[ngrid] = Globals.CopyFrom(tmp);
            }
            nn = 3;
            double[,] uj = new double[nn, nn];
            slvsml(uj, rho[0]);
            for (int j = 1; j < ng; j++)
            {
                nn = 2 * nn - 1;
                uj1 = uj;
                uj = new double[nn, nn];
                interp(uj, uj1);
                uj1 = null;
                for (int jcycle = 0; jcycle < ncycle; jcycle++)
                {
                    mg(j, uj, rho[j]);
                }
            }
            u = uj;
        }

        public void interp(double[,] uf, double[,] uc)
        {
            int nf = uf.GetLength(0);
            int nc = nf / 2 + 1;
            for (int jc = 0; jc < nc; jc++)
            {
                for (int ic = 0; ic < nc; ic++)
                {
                    uf[2 * ic, 2 * jc] = uc[ic, jc];
                }
            }
            for (int jf = 0; jf < nf; jf += 2)
            {
                for (int iif = 1; iif < nf - 1; iif += 2)
                {
                    uf[iif, jf] = 0.5 * (uf[iif + 1, jf] + uf[iif - 1, jf]);
                }
            }
            for (int jf = 1; jf < nf - 1; jf += 2)
            {
                for (int iif = 0; iif < nf; iif++)
                {
                    uf[iif, jf] = 0.5 * (uf[iif, jf + 1] + uf[iif, jf - 1]);
                }
            }
        }

        public void addint(double[,] uf, double[,] uc, double[,] res)
        {
            int nf = uf.GetLength(0);
            interp(res, uc);
            for (int j = 0; j < nf; j++)
            {
                for (int i = 0; i < nf; i++)
                {
                    uf[i, j] += res[i, j];
                }
            }
        }

        public void slvsml(double[,] u, double[,] rhs)
        {
            double h = 0.5;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    u[i, j] = 0.0;
                }
            }
            u[1, 1] = -h * h * rhs[1, 1] / 4.0;
        }

        public void relax(double[,] u, double[,] rhs)
        {
            int n = u.GetLength(0);
            double h = 1.0 / (n - 1);
            double h2 = h * h;
            for (int ipass = 0, jsw = 1; ipass < 2; ipass++, jsw = 3 - jsw)
            {
                for (int j = 1, isw = jsw; j < n - 1; j++, isw = 3 - isw)
                {
                    for (int i = isw; i < n - 1; i += 2)
                    {
                        u[i, j] = 0.25 * (u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - h2 * rhs[i, j]);
                    }
                }
            }
        }

        public void resid(double[,] res, double[,] u, double[,] rhs)
        {
            int n = u.GetLength(0);
            double h = 1.0 / (n - 1);
            double h2i = 1.0 / (h * h);
            for (int j = 1; j < n - 1; j++)
            {
                for (int i = 1; i < n - 1; i++)
                {
                    res[i, j] = -h2i * (u[i + 1, j] + u[i - 1, j] + u[i, j + 1] + u[i, j - 1] - 4.0 * u[i, j]) + rhs[i, j];
                }
            }
            for (int i = 0; i < n; i++)
            {
                res[i, 0] = res[i, n - 1] = res[0, i] = res[n - 1, i] = 0.0;
            }
        }

        public void rstrct(double[,] uc, double[,] uf)
        {
            int nc = uc.GetLength(0);
            int ncc = 2 * nc - 2;
            for (int jf = 2, jc = 1; jc < nc - 1; jc++, jf += 2)
            {
                for (int iif = 2, ic = 1; ic < nc - 1; ic++, iif += 2)
                {
                    uc[ic, jc] = 0.5 * uf[iif, jf] + 0.125 * (uf[iif + 1, jf] + uf[iif - 1, jf] + uf[iif, jf + 1] + uf[iif, jf - 1]);
                }
            }
            for (int jc = 0, ic = 0; ic < nc; ic++, jc += 2)
            {
                uc[ic, 0] = uf[jc, 0];
                uc[ic, nc - 1] = uf[jc, ncc];
            }
            for (int jc = 0, ic = 0; ic < nc; ic++, jc += 2)
            {
                uc[0, ic] = uf[0, jc];
                uc[nc - 1, ic] = uf[ncc, jc];
            }
        }

        public void mg(int j, double[,] u, double[,] rhs)
        {
            const int NPRE = 1;
            const int NPOST = 1;
            int nf = u.GetLength(0);
            int nc = (nf + 1) / 2;
            if (j == 0)
            {
                slvsml(u, rhs);
            }
            else
            {
                double[,] res = new double[nc, nc];
                double[,] v = new double[nc, nc];
                double[,] temp = new double[nf, nf];
                for (int jpre = 0; jpre < NPRE; jpre++)
                {
                    relax(u, rhs);
                }
                resid(temp, u, rhs);
                rstrct(res, temp);
                mg(j - 1, v, res);
                addint(u, v, temp);
                for (int jpost = 0; jpost < NPOST; jpost++)
                {
                    relax(u, rhs);
                }
            }
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/117642.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【原创】java+swing+mysql校园共享单车管理系统设计与实现

摘要&#xff1a; 校园共享单车作为一种绿色、便捷的出行方式&#xff0c;在校园内得到了广泛的应用。然而&#xff0c;随着单车数量的增加&#xff0c;管理难度也不断加大。如何提高单车的利用率和管理效率&#xff0c;成为校园共享单车发展面临的重要问题。本文针对这一问题…

苹果手机微信照片过期怎么恢复?分享3个实用技巧!

很多时候&#xff0c;我们会在微信中给家人和朋友分享一些有趣的照片。但有时会因为各种原因&#xff0c;比如还没有来得及查看或保存&#xff0c;从而导致这些照片过期失效。 如果这些图片我们还需要用到&#xff0c;那么将它们找回来是很有必要的事情。微信照片过期怎么恢复…

LeetCode.6 N字形变换

一开始想的是真的创建一个数组 去按照题目所给的要求填入数据 最后输出不为空的数组项 但是不仅时间复杂度高 而且错误频繁出现 最终也没有提交成功 查阅题解后发现数组并不重要 假设我们忽略掉数组中的那些空白项 最终输出的结果就是numRows行的字符串的拼接 string conver…

记一次pdjs时安装glob出现,npm ERR! code ETARGET和npm ERR! code ELIFECYCLE

如往常一样&#xff0c;我使用pdjs来编译proto文件&#xff0c;但出现了以下报错&#xff1a; 大致就是pdjs的util在尝试执行npm install glob^7.2.1 escodegen^1.13.0时出错了 尝试手动执行安装&#xff0c;escodegen被正确安装&#xff0c;但glob^7.2.1出错 npm ERR! code E…

一个JS版寻路的实现

js版的寻路的测试 20231104_161146 path get_v8: function (x_inc, y_inc) {if (x_inc 0) {if (y_inc < 0) {return [[0, -1], [-1, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [1, 1], [0, 1]];} else if (y_inc > 0) {return [[0, 1], [-1, 1], [1, 1], [-1, 0], [1, 0…

The service already exists! 安装mysql数据库错误!

当你输入mysql install命令时报The service already exists! 报错的原因是服务已经存在&#xff01; 说明你之前可能已经装过了。 解决方法&#xff1a; 输入sc delete mysql 提示DeleteService 成功,则表示删除成功&#xff0c;你就可以重新输入mysqld install了。 最后显…

CMU/MIT/清华/Umass提出生成式机器人智能体RoboGen

文章目录 导读1. Introduction2. 论文地址3. 项目主页4. 开源地址5. RoboGen Pipeline6. Experimental Results作者介绍Reference 导读 CMU/MIT/清华/Umass提出的全球首个生成式机器人智能体RoboGen&#xff0c;可以无限生成数据&#xff0c;让机器人7*24小时永不停歇地训练。…

Weblogic ssrf漏洞复现

文章目录 一、漏洞描述二、漏洞特征1.查看uddiexplorer应用2.漏洞点 三、漏洞复现1.获取容器内网ip2.VULHUB Weblogic SSRF漏洞 docker中 centos6 无法启动的解决办法3.准备payload4.反弹shell 一、漏洞描述 SSRF 服务端请求伪造(Server-Side Request Forgery),是一种由攻击者…

知乎日报第三周总结

这周主要完成了评论的加载和长评论的展开与收起&#xff0c;同时完善了前面的内容&#xff0c;文章内容cell的滑动刷新改为滑动一个加载一个&#xff0c;这样就更加流畅&#xff1b;还有就是首次点击只先加载当前cell内容&#xff0c;这样就不会卡顿加载过多内容&#xff0c;剩…

17. 机器学习 - 随机森林

Hi&#xff0c;你好。我是茶桁。 我们之前那一节课讲了决策树&#xff0c;说了决策树的优点&#xff0c;也说了其缺点。 决策树实现起来比较简单&#xff0c;解释解释性也比较强。但是它唯一的问题就是不能拟合比较复杂的关系。 后来人们为了解决这个问题&#xff0c;让其能…

Linux CentOS 8(HTTP的配置与管理)

Linux CentOS 8&#xff08;HTTP的配置与管理&#xff09; 目录 一、常见的 HTTP&#xff08;web&#xff09;服务软件二、基本的 Apache httpd 配置三、httpd.conf 配置文件详解案例1 四、配置虚拟主机&#xff08;在一台主机发布多个站点&#xff09;案例2 一、常见的 HTTP&a…

淘宝预定商品收不到尾款通知 - 解决方案

问题 用户在使用淘宝购买预定商品后&#xff0c;待补尾款时&#xff0c;无法收到尾款通知&#xff0c;从而导致错过补齐尾款无法购买预定商品&#xff0c;下文介绍解决方案。 解决方案 进入淘宝后&#xff0c;购买预定商品时&#xff0c;在提交订单页面时&#xff0c;取消勾…

基于单片机的智能饮水机系统

收藏和点赞&#xff0c;您的关注是我创作的动力 文章目录 概要 一、系统设计方案分析2.1 设计功能及性能分析2.2设计方案分析 二、系统的硬件设计3.1 系统设计框图系统软件设计4.1 总体介绍原理图 四、 结论 概要 现在很多学校以及家庭使用的饮水机的功能都是比较单一的&#…

当pytest遇上poium会擦出什么火花

首先&#xff0c;创建一个test_sample/test_demo.py 文件&#xff0c;写入下面三行代码。 def test_bing(page):page.get("https://www.bing.com")assert page.get_title "必应"不要问题 page 从哪里来&#xff0c;打开终端进入test_sample/目录&#xf…

MySQL---搜索引擎

MySQL的存储引擎是什么 MySQL当中数据用各种不同的技术存储在文件中&#xff0c;每一种技术都使用不同的存储机制&#xff0c;索引技巧 锁定水平&#xff0c;以及最终提供的不同的功能和能力&#xff0c;这些就是我们说的存储引擎。 MySQL存储引擎的功能 1.MySQL将数据存储在文…

软件测试面试最经典的5个问题

软件测试面试灵魂五问&#xff01; 请做一下自我介绍&#xff1f;你为什么从上家公司离职&#xff1f;为什么转行做测试? 你对测试行业的认识&#xff1f;你的期望薪资是多少&#xff1f;最后&#xff0c;你要问我什么&#xff1f; 一、请做一下自我介绍 简历上有的可以一两…

C++ Concurrency in Action 2nd Edition

《C Concurrency in Action - SECOND EDITION》的中文翻译-面圈网 (mianshigee.com) C/C 学习教程源码-C/C源码推荐-面试哥 (mianshigee.com) 作者正是为C11标准引入线程库的C标准委员会成员本人&#xff01;并且本书作者还编写了众多构成C标准的多线程和并发相关的提案、制定…

Apache Flink 1.12.0 on Yarn(3.1.1) 所遇到的問題

Apache Flink 1.12.0 on Yarn(3.1.1) 所遇到的問題 新搭建的FLINK集群出现的问题汇总 1.新搭建的Flink集群和Hadoop集群无法正常启动Flink任务 查看这个提交任务的日志无法发现有用的错误信息。 进一步查看yarn日志&#xff1a; 发现只有JobManager的错误日志出现了如下的…

试利用栈的基本操作写出先序遍历二叉树的非递归形式的算法

试利用栈的基本操作写出先序遍历二叉树的非递归形式的算法 代码思路&#xff1a; 要用栈解决先序遍历&#xff0c;我们首先要知道栈的性质和二叉树先序遍历的规则 栈最基本的就是先进后出 而二叉树先序遍历就是“根左右” 利用这两个性质&#xff0c;我们可以先将根结点入队…
最新文章