使用javaScript脚本生成openFoam网格

news2024/9/19 10:48:21

简介

OpenFoam的首选网格生成器是blockMesh。blockMesh可以根据blockMeshDict这个字典中的信息生成openFoam网格。但是有时候需要修改网格,而网格中的几何点之间又存在约束关系,如果手动修改blockMeshDict那么工作量将是巨大的,所以有必要使用javaScript脚本直接生成blockMeshDict文件

代码与解释

在OpenFoam案例的system文件夹中新建一个空白文件,名称为meshGenerator.js。我们将编写一个javaScript脚本来生成blockMeshDict文件。

若生成一个简易固体火箭发动机的网格,代码如下:

//定义数据字典字典的头
var blockMeshDict =
    `/*--------------------------------*- C++ -*----------------------------------*\\
=========                 |
\\\\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 \\\\    /   O peration     | Website:  https://openfoam.org
  \\\\  /    A nd           | Version:  8
   \\\\/     M anipulation  |
\\*---------------------------------------------------------------------------*/
FoamFile
{
  version     2.0;
  format      ascii;
  class       dictionary;
  object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.0254;
`
//注意:楔形角度为2度
const tanTheta = Math.tan(Math.PI / 180.0)
//网格细化倍数
const finer = 1.0;
//注意:长度单位都是inch,1 inch = 2.54 cm = 0.0254 m
const R = 6, RT = 1, L = 20, dL = 1//发动机内半径,喉部半径,装药长度,装药与前封头之间的间隙
const RA = RB = 2.0 * RT//过渡圆弧的半径,分别是收敛段和扩张段的
const alpha = 45.0 * Math.PI / 180.0, beta = 15.0 * Math.PI / 180.0//收敛段和扩张段的半角(弧度制)
const epson = 9.5//喷管的扩张比
const y1 = RA * (1 - Math.cos(alpha)) + RT
const x1 = (R - y1)/ Math.tan(alpha)
const y2 = RT
const x2 = RA * Math.sin(alpha)
const y3 = RB * (1 - Math.cos(beta)) + RT
const x3 = RB * Math.sin(beta)
const y4 = Math.sqrt(epson) * RT
const x4 = (y4 - y3) / Math.tan(beta)

//定义vertices字段
var vertices = []
vertices.push({ x: -dL, y: 0, z: 0 })
vertices.push({ x: -dL, y: R, z: -R * tanTheta })
vertices.push({ x: -dL, y: R, z: R * tanTheta })
vertices.push({ x: L, y: 0, z: 0 })
vertices.push({ x: L, y: R, z: -R * tanTheta })
vertices.push({ x: L, y: R, z: R * tanTheta })
vertices.push({ x: L + x1, y: 0, z: 0 })
vertices.push({ x: L + x1, y: y1, z: -y1 * tanTheta })
vertices.push({ x: L + x1, y: y1, z: y1 * tanTheta })
vertices.push({ x: L + x1 + x2 + x3, y: 0, z: 0 })
vertices.push({ x: L + x1 + x2 + x3, y: y3, z: -y3 * tanTheta })
vertices.push({ x: L + x1 + x2 + x3, y: y3, z: y3 * tanTheta })
vertices.push({ x: L + x1 + x2 + x3 + x4, y: 0, z: 0 })
vertices.push({ x: L + x1 + x2 + x3 + x4, y: y4, z: -y4 * tanTheta })
vertices.push({ x: L + x1 + x2 + x3 + x4, y: y4, z: y4 * tanTheta })
blockMeshDict += verticesToString(vertices)

//定义blocks字段
var blocks = []
blocks.push({ type: "hex", topology: [0, 3, 4, 1, 0, 3, 5, 2], celNum: { x: 150 * finer, y: 50 * finer, z: 1 }, simpleGrading: { x: 1, y: 0.2, z: 1 } })
blocks.push({ type: "hex", topology: [3, 6, 7, 4, 3, 6, 8, 5], celNum: { x: 30 * finer, y: 50 * finer, z: 1 }, simpleGrading: { x: 1, y: 0.2, z: 1 } })
blocks.push({ type: "hex", topology: [6, 9, 10, 7, 6, 9, 11, 8], celNum: { x: 20 * finer, y: 50 * finer, z: 1 }, simpleGrading: { x: 1, y: 0.2, z: 1 } })
blocks.push({ type: "hex", topology: [9, 12, 13, 10, 9, 12, 14, 11], celNum: { x: 50 * finer, y: 50 * finer, z: 1 }, simpleGrading: { x: 1, y: 0.2, z: 1 } })
blockMeshDict += blocksToString(blocks)

//定义edges字段(用于描述哪些边是圆弧)
var edges = []
edges.push({ type: "arc", topology: { p1: 10, p2: 7 }, midPoint: { x: L + x1 + x2, y: y2, z: -y2 * tanTheta } })
edges.push({ type: "arc", topology: { p1: 11, p2: 8 }, midPoint: { x: L + x1 + x2, y: y2, z: y2 * tanTheta } })
blockMeshDict += edgesToString(edges)

//定义boundary字段
var boundary = []
boundary.push({ name: "upWall", type: "wall", faces: [[5, 4, 1, 2], [5, 8, 7, 4], [8, 11, 10, 7], [11, 14, 13, 10]] })
boundary.push({ name: "leftWall", type: "wall", faces: [[0, 0, 2, 1]] })
boundary.push({ name: "rightWall", type: "patch", faces: [[12, 12, 13, 14]] })
boundary.push({ name: "front", type: "wedge", faces: [[0, 3, 5, 2], [3, 6, 8, 5], [6, 9, 11, 8], [9, 12, 14, 11]] })
boundary.push({ name: "back", type: "wedge", faces: [[1, 4, 3, 0], [4, 7, 6, 3], [7, 10, 9, 6], [10, 13, 12, 9]] })

blockMeshDict += boundaryToString(boundary)

console.log(blockMeshDict)

function verticesToString(vertices) {
    let str = "\nvertices\n("
    vertices.forEach(ele => {
        str += `\n    ( ${ele.x} ${ele.y} ${ele.z} )`
    })
    str += "\n);\n"
    return str
}

function blocksToString(blocks) {
    let str = "\nblocks\n("
    blocks.forEach(ele => {
        str += `\n    ${ele.type} (`
        ele.topology.forEach(ele => {
            str += ` ${ele}`
        })
        str += `) (${ele.celNum.x} ${ele.celNum.y} ${ele.celNum.z})`
        str += `simpleGrading (${ele.simpleGrading.x} ${ele.simpleGrading.y} ${ele.simpleGrading.z})`
    })
    str += "\n);\n"
    return str
}

function edgesToString(edges) {
    let str = "\nedges\n("
    edges.forEach(ele => {
        str += `\n    ${ele.type} ${ele.topology.p1} ${ele.topology.p2} ( ${ele.midPoint.x} ${ele.midPoint.y} ${ele.midPoint.z} )`
    })
    str += "\n);\n"
    return str
}

function boundaryToString(boundary) {
    let str = "\nboundary\n("
    boundary.forEach(ele => {
        str += `\n    ${ele.name}\n    {\n        type ${ele.type};\n        faces\n        (`
        ele.faces.forEach(face => {
            str += `\n            ( `
            face.forEach(vertice => {
                str += `${vertice} `
            })
            str += ")"
        })
        str += "\n        );"
        str += "\n    }"
    })
    str += "\n);\n"
    return str
}

代码中依次生成了vertices字段、blocks字段、edges字段和boundary字段,并全部储存在blockMeshDict字符串里。

最终将blockMeshDict字符串输出到控制台上。输出为

/*--------------------------------*- C++ -*----------------------------------*\
=========                 |
\\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 \\    /   O peration     | Website:  https://openfoam.org
  \\  /    A nd           | Version:  8
   \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
  version     2.0;
  format      ascii;
  class       dictionary;
  object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.0254;

vertices
(
    ( -1 0 0 )
    ( -1 6 -0.10473038956930551 )
    ( -1 6 0.10473038956930551 )
    ( 20 0 0 )
    ( 20 6 -0.10473038956930551 )
    ( 20 6 0.10473038956930551 )
    ( 24.414213562373096 0 0 )
    ( 24.414213562373096 1.5857864376269049 -0.02768000523106449 )
    ( 24.414213562373096 1.5857864376269049 0.02768000523106449 )
    ( 26.34606521495123 0 0 )
    ( 26.34606521495123 1.0681483474218634 -0.01864459875721694 )
    ( 26.34606521495123 1.0681483474218634 0.01864459875721694 )
    ( 33.86263444133674 0 0 )
    ( 33.86263444133674 3.082207001484488 -0.05380012333311857 )
    ( 33.86263444133674 3.082207001484488 0.05380012333311857 )
);

blocks
(
    hex ( 0 3 4 1 0 3 5 2) (150 50 1)simpleGrading (1 0.2 1)
    hex ( 3 6 7 4 3 6 8 5) (30 50 1)simpleGrading (1 0.2 1)
    hex ( 6 9 10 7 6 9 11 8) (20 50 1)simpleGrading (1 0.2 1)
    hex ( 9 12 13 10 9 12 14 11) (50 50 1)simpleGrading (1 0.2 1)
);

edges
(
    arc 10 7 ( 25.82842712474619 1 -0.017455064928217585 )
    arc 11 8 ( 25.82842712474619 1 0.017455064928217585 )
);

boundary
(
    upWall
    {
        type wall;
        faces
        (
            ( 5 4 1 2 )
            ( 5 8 7 4 )
            ( 8 11 10 7 )
            ( 11 14 13 10 )
        );
    }
    leftWall
    {
        type wall;
        faces
        (
            ( 0 0 2 1 )
        );
    }
    rightWall
    {
        type patch;
        faces
        (
            ( 12 12 13 14 )
        );
    }
    front
    {
        type wedge;
        faces
        (
            ( 0 3 5 2 )
            ( 3 6 8 5 )
            ( 6 9 11 8 )
            ( 9 12 14 11 )
        );
    }
    back
    {
        type wedge;
        faces
        (
            ( 1 4 3 0 )
            ( 4 7 6 3 )
            ( 7 10 9 6 )
            ( 10 13 12 9 )
        );
    }
);

使用方法

环境需求:nodejs
在命令行运行

node meshGenerator.js > blockMeshDict

将结果重定向到blockMeshDict文件

然后转到openFoam案例目录下,运行blockMesh

blockMesh

网格划分完毕之后,可使用paraview查看网格,运行

paraFoam

效果如下
用户可以通过修改js代码来对其他问题进行网格划分

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

有没有免费提取音频的软件,分享几个给大家!

在日常生活中,我们经常遇到需要从视频中提取音频的情况,无论是为了制作音频片段、录制语音笔记还是进行后期编辑。本文将介绍三种免费提取音频的方法,分别是记灵在线工具、PR(Adobe Premiere Pro)和剪映。通过这些方法…

【Vue3】学习笔记-自定义hook函数

概念 什么是hook? 本质是一个函数,把setup函数中使用的Composition API进行了封装。 类似于vue2.x中的mixin。(但是mixins会组件的配置项覆盖。vue3使用了自定义hooks替代mixnins,hooks本质上是函数,引入调用。) 自定义hook的优势: 复用代…

PPU (power policy unit)

写在前边 最近在做低功耗验证,项目中涉及到PPU这一块儿,在家查了好久资料,发现能找到的有价值的文章真的好少,机缘巧合之下,让我找到下边总结,分享出来,希望对和我有相同境遇的小伙伴带来帮助&a…

每周学点数学 2:概率论基础1

泊松分布、正态分布、二项分布 文章目录 1.概率论学习中的重难点2.主要工具介绍1. Python2. MATLAB3. R4. Octave5. Microsoft Excel6. 统计软件 3.理论内容概览(前两点)1. 概率2. 概率分布 注:本文适用于在在数学建模的应用中,回…

牛客网基础语法101~110题

牛客网基础语法101~110题😘😘😘 💫前言:今天是咱们第十期刷牛客网上的题目。 💫目标:对打印图案做到有手就行。 💫鸡汤:与其花时间应付以后不理想的生活,不如…

学习c++ Part02

学习c Part02 前言1.函数注意点:全局函数(默认函数)静态函数 2.预处理2.1 变量 3.头文件4.宏函数5.指针5.1 普通变量与指针变量建立关系:5.2 指针初始化5.3 指针变量的注意事项5.3.1 void 不能定义普通变量,void * 可以定义指针变…

SpringBoot源码解析

1.Spring Boot介绍,源码阅读环境搭建,插件安装 2.spring boot 源码解析2-SpringApplication初始化 3.spring boot 源码解析3-SpringApplication#run 4.spring boot 源码解析4-SpringApplication#run第4步 5.spring boot 源码解析5-SpringApplication#run第5步 6.spring boot 源…

springboot医院挂号小程序

医院挂号系统 springboot医院挂号系统小程序 java医院挂号小程序 技术: 基于springbootvue小程序医院挂号系统的设计与实现 运行环境: JAVA版本:JDK1.8 IDE类型:IDEA、Eclipse都可运行 数据库类型:MySql(…

在线教育场景下客户端实践与优化——RTC服务在线教育

在线教育场景下对提供稳定、高质量的音视频服务提出了非常高的要求。而不断推陈出新的课堂形式以及新技术的应用,使得好未来自研音视频SDK面临更多的挑战。 LiveVideoStackCon 2022北京站邀请到好未来音视频开发高级专家郭晓明介绍好未来自研SDK在工程化上所做出的努…

【编译、链接、装载十五】系统调用与API——printf源码分析

【编译、链接、装载十五】系统调用与API——printf源码分析 一、系统调用介绍1、什么是系统调用2、Linux系统调用3、系统调用的弊端 二、系统调用原理1、中断 三、linux下系统调用实现1、 strace 查看可知,printf调用了系统函数write2、gdb调试查看——printf3、gdb…

实践|随机森林中缺失值的处理方法

动动发财的小手,点个赞吧! 除了在网上找到的一些过度清理的数据集之外,缺失值无处不在。事实上,数据集越复杂、越大,出现缺失值的可能性就越大。缺失值是统计研究的一个令人着迷的领域,但在实践中它们往往很…

并查集的讲解

什么是并查集? --是一个森林;(由多颗树构成的) 并查集原理 在一些应用问题中,需要 将 n 个不同的元素划分成一些不相交的集合 。 开始时,每个元素自成一个 单元素集合,然后按一定的规律将归于…

springboot开启热部署

第一步引入spring-boot-devtools依赖 <!--热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><!--默认false改依赖是否可以传递&…

【Servlet】如何使用 Servlet 编写第一个 helloword 程序

文章目录 前言一、创建 Maven 项目二、引入依赖三、创建目录四、编写代码五、打包项目六、部署程序七、运行程序总结 前言 各位读者好, 我是小陈, 这是我的个人主页, 希望我的专栏能够帮助到你: &#x1f4d5; JavaSE基础: 基础语法, 类和对象, 封装继承多态, 接口, 综合小练习…

transforms数据增强

在AI领域的模型训练中通常会遇到模型过拟合问题&#xff0c;通常采取的办法就是数据增强处理&#xff0c;例如在图像处理中&#xff0c;数据增强是指对原始图像进行旋转、缩放、剪切、翻转等操作&#xff0c;以扩大训练数据集的规模&#xff0c;提高模型泛化能力&#xff0c;降…

Gradio HTML组件详解

❤️觉得内容不错的话&#xff0c;欢迎点赞收藏加关注&#x1f60a;&#x1f60a;&#x1f60a;&#xff0c;后续会继续输入更多优质内容❤️ &#x1f449;有问题欢迎大家加关注私戳或者评论&#xff08;包括但不限于NLP算法相关&#xff0c;linux学习相关&#xff0c;读研读博…

【23-07-03:HTTP协议的结构学习】

目录 HTTP 请求的结构HTTP 请求的整体架构请求方法&#xff08;Method&#xff09;请求路径&#xff08;URI&#xff09;GET 参数&#xff08;Parameters for GET)协议说明&#xff08;Protocol&#xff09;头部字段&#xff08;Headers&#xff09;请求体&#xff08;Body&…

论文与专利查找和下载

例如我想查找和下载视频理解(video understanding)相关论文 路线大纲如下&#xff1a; 一、最主要方式&#xff1a; 大纲&#xff0c;蓝色都是有超级链接的可以直接打开: 第一步 谷歌搜索(英文) 学校的知网(中文)第二步 下载论文(谷歌学术--英文 学校的知网--中文)下载不了…

CMA

文章目录 前言概念功能启用CMA 内存的创建方式一、使用 cmdline方式二、使用 dts CMA 内存分配和释放实例&#xff08;dts 方式&#xff09; 前言 在嵌入式设备中&#xff0c;很多外设&#xff08;如摄像机、硬件视频解码器等&#xff09;需要较大的内存缓冲区&#xff0c;kma…

clickhouse日志表占用大量磁盘空间

clickhouse日志表占用大量磁盘空间 sql&#xff1a; SELECT sum(rows) AS 总行数, formatReadableSize(sum(data_uncompressed_bytes)) AS 原始大小, formatReadableSize(sum(data_compressed_bytes)) AS 压缩大小, round((sum(data_compressed_bytes) / sum(data_uncompresse…