首页 > 其他分享 >OFtutorial04_basicFieldOperations解析

OFtutorial04_basicFieldOperations解析

时间:2024-08-12 14:09:09浏览次数:10  
标签:basicFieldOperations include int OFtutorial04 scalar scale 解析

OFtutorial4.C

源码

#include "fvCFD.H"

// This is a function declaration; this method will calculate some scalar value
// given the current time, location in space x, and a reference point x0. The
// function also accepts a scaling factor, scale.
// The actual implementation, or definition, is below.
#声明自定义函数calculatePressure
scalar calculatePressure(scalar t, vector x, vector x0, scalar scale);

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"

	// This reads a dictionary file.
	Info << "Reading transportProperties\n" << endl;
#定义 IOdictionary 类
	IOdictionary transportProperties
	(
		IOobject
		(
		    "transportProperties", // name of the dictionary
		    runTime.constant(), // location in the case - this one is in constant
		    mesh, // needs the mesh object reference to do some voodoo - unimportant now
		    IOobject::MUST_READ_IF_MODIFIED, // the file will be re-read if it gets modified during time stepping
		    IOobject::NO_WRITE // read-only
		)
	);

	// Create a scalar constant for kinematic viscosity by reading the value from the dictionary.
#定义无量纲标量 nu
	dimensionedScalar nu
	(
		"nu", // name of the variable
		dimViscosity, // dimensions
		// TIP: to check how this is defined, run:
		// grep -r dimViscosity $FOAM_SRC/OpenFOAM/
		// This returns:
		/*/opt/openfoam30/src/OpenFOAM/dimensionSet/dimensionSets.C:const dimensionSet dimViscosity(dimArea/dimTime);
		/opt/openfoam30/src/OpenFOAM/dimensionSet/dimensionSets.C:const dimensionSet dimDynamicViscosity(dimDensity*dimViscosity);
		/opt/openfoam30/src/OpenFOAM/dimensionSet/dimensionSets.H:extern const dimensionSet dimViscosity;*/
		// So, it becomes apparent we should check dimensionSets.C, which contain:
		/*const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0);
		const dimensionSet dimTime(0, 0, 1, 0, 0, 0, 0);
		const dimensionSet dimArea(sqr(dimLength));
		const dimensionSet dimViscosity(dimArea/dimTime);*/
		// This is what gets used here. But, an alternative would be to type in the units directly:
		// dimensionSet(0,2,-1,0,0,0,0),
		transportProperties.lookup("nu") // this takes the value from the dictionary and returns it, passing it to the object constructor as an argument
	);

	// These read the fields p and U from the time folders, as specified in system/controlDict (i.e. latestTime, startTime, etc.)
	Info<< "Reading field p\n" << endl;
#读取压力标量场
	volScalarField p // note that pressure is a scalar field
	(
		IOobject
		(
		    "p", // name of the field
		    runTime.timeName(), // name of the current time, i.e. the time folder to read from
		    mesh,
		    IOobject::MUST_READ, // always gets imported, will throw an error if the field is missing
		    IOobject::AUTO_WRITE // will get saved automatically when the controlDict parameters will request it
		),
		mesh // initialises the field to match the size of the mesh with default (0) values
	);

	Info<< "Reading field U\n" << endl;
#读取速度矢量场
	volVectorField U // note that velocity is a vector field
	(
		IOobject
		(
		    "U",
		    runTime.timeName(),
		    mesh,
		    IOobject::MUST_READ,
		    IOobject::AUTO_WRITE
		),
		mesh
	);

	// Let us define a vector whose values will not change over the course of the program execution.
#定义原点坐标(参考位置)
	const vector originVector(0.05,0.05,0.005);

	// Calculate the distance from the origin to the cell centre furthest away.
	// In Python, this is equivalent to:
	// np.sqrt(np.sum((x0-x)**2))
	// The .value() method is called to convert from a dimensionedScalar to a regular scalar.
#计算流场中最远点到原点的距离
	const scalar rFarCell = max( // find the maximum value from all distances
		// compute distance of each cell centre from x0; units of mesh.C() are those of length, as this field
		// describes position in the Cartesian reference frame.
#mag函数用于计算向量的模
		mag(dimensionedVector("x0",dimLength,originVector)-mesh.C())
		).value(); // convert to dim-less scalar

	// This part of the code performs time stepping for as long as is required by the simulation.
	Info<< "\nStarting time loop\n" << endl;

	// This will increment the current time automatically
#对于每一个时刻,遍历所有cells,然后修改每一个cell的压力标量p[cellI],然后在统一修改速度矢量场U
    while (runTime.loop())
    {
        Info<< "Time = " << runTime.timeName() << nl << endl;

		// Loop over all cells in the mesh and calculate the pressure value.
		for (label cellI=0; cellI<mesh.C().size(); cellI++)
		{
			// cellI describes a series of integers, each corresponding to an index of an individual cell in the grid.

			// Call the method and compute p.
			// Note how mesh.C() and p elements are accessed by using the [] operators, as in a regular C array.
			// .value() is also called to convert the time to a dim-less scalar
			p[cellI] = calculatePressure(runTime.time().value(), mesh.C()[cellI], originVector, rFarCell);

            // NOTE: it is also possbile to interact with boundary face values, but
            // this will be addressed in a separate tutorial.
		}

		// Calculate the gradient of p and substitute for U. Note that the units of grad(p) are not m/s,
		// so we multiply by a unit time variable to make them so. This is just for illustration purposes, of course.
		// The result will point either towards or away from the origin, depending on the sign of the
		// time varying "pressure".
		U = fvc::grad(p)*dimensionedScalar("tmp",dimTime,1.);

		// If requested by controlDict, save the fields to files.
		runTime.write();

		// NOTE: a more appropriate way to calculate things in OpenFOAM is through performing
		// operations on field objects and not iterating cell-by-cell, where possible.
		// How to do this has been shown above, where rFarCell is being computed.
		// The iterative approach has been presented for completeness and to illustrate certain
		// basic features, but is, generally, discouraged, unless absolutely necessary.
	}

	Info << "Finished! Best to visualise the results by plotting p iso-contours with range (-10,10) and applying a glyph filter to the U field in Paraview." << endl;

    Info<< "End\n" << endl;

    return 0;
}

// definition of the custom function
#定义自定义函数calculatePressure
scalar calculatePressure(scalar t, vector x, vector x0, scalar scale)
{
	// Calculates the distance between the base point and x, which is given by the magnitude (hence the name mag)
	// of the vector between x0 and x. The value is scaled by the passed factor, with the intention of making
	// it vary between 0 and 1.
	scalar r (mag(x-x0)/scale);

	// Calculate the inverse of r and apply a limiter to avoid dividing by zero.
	scalar rR (1./(r+1e-12));

	// definition of a frequency
	scalar f (1.);

	// Return a sinusoidally varying pressure with maximum at x0.
	// Note how we call the OpenFOAM sin method by referring to the Foam namespace.
	// This is done to differentiate between the native C++ implementation of a method with the same name
	// and thus avoid an ambiguous expression.
	return Foam::sin(2.*Foam::constant::mathematical::pi*f*t)*rR;
}

标签:basicFieldOperations,include,int,OFtutorial04,scalar,scale,解析
From: https://www.cnblogs.com/ouqiyo/p/18354828

相关文章

  • 【Rust光年纪】Rust数据结构库全方位解析:从核心功能到API概览
    提升Rust项目效率的利器:六款优秀数据结构库详解前言随着Rust编程语言的不断发展和普及,开发者们对于高效的数据结构库需求日益增长。在本文中,我们将介绍一些优秀的Rust数据结构库,它们分别为heapless、arrayvec、smallvec、evmap、hashbrown和generic-array。这些库提供了各......
  • 咪咕视频m3u8地址解析及ddCalcu参数加密逆向
    咪咕视频m3u8地址解析及ddCalcu参数加密逆向概述本文主要讲述咪咕视频m3u8地址的解析以及使用Wasm对视频的m3u8地址进行加密得到ddCalcu参数的方法。使用视频ID获取未加密的视频URL对咪咕视频进行抓包发现,通过接口https://webapi.miguvideo.com/gateway/playurl/v3/play/pla......
  • OFtutorial03_understandingTheMesh解析
    OFtutorial3.C#include"fvCFD.H"intmain(intargc,char*argv[]){#include"setRootCase.H" //Thesetwocreatethetimesystem(instancecalledrunTime)andfvMesh(instancecalledmesh).#include"createTime.H"......
  • 中药配方颗粒行业报告:市场规模、趋势与机遇解析
    一、行业简述(一)行业概念中药配方颗粒,是指将传统中药饮片通过现代制药技术,经水提取、浓缩、干燥、制粒等步骤制成的颗粒状制剂。这种制剂保持了中药饮片的性味归经和主治功效,同时具有安全、有效、方便、质量稳定可控等优点。中药配方颗粒无需像传统饮片一样煎煮,可直接冲泡服......
  • 0218-地址解析协议
    环境Time2022-11-20VirtualBox7.0.2Rust1.65.0pnet0.31.0CentOS7前言说明参考:https://docs.rs/pnet_packet/latest/pnet_packet/index.html目标使用两台虚拟机,通过IP地址,获取到目标主机的MAC地址。日常使用的时候,都是使用IP连接服务器,需要使用地址解析协......
  • PrimeFaces SelectOneMenu 与 Ajax 集成实例解析
    ======在现代Web开发中,用户界面的交互性是至关重要的。PrimeFaces作为JavaServerFaces(JSF)的一个流行UI组件库,提供了丰富的组件来增强用户界面。本文将通过一个具体实例,详细介绍如何使用PrimeFaces的SelectOneMenu组件与Ajax技术相结合,实现在选择事件时通过Ajax提交数据。......
  • Java动态代理与方法拦截实战解析
    Java动态代理与方法拦截实战解析在Java编程中,动态代理是一种强大的技术,它允许我们在运行时创建接口的代理实例,并且可以拦截方法调用。本文将通过一个具体的实例,详细解析如何使用JDK的动态代理机制来实现方法拦截,以及如何编写通用的方法拦截器。实现InvocationHandler首先......
  • 深入解析@JsonValue注解在Java序列化中的应用
    深入解析@JsonValue注解在Java序列化中的应用在Java开发中,对象序列化是一个常见的需求,尤其是在进行网络通信或者数据持久化时。Jackson库作为Java领域内一个非常流行的JSON处理库,提供了强大的序列化和反序列化功能。在Jackson2.9版本之后,@JsonValue注解的引入,为开发者提供......
  • Python数据科学的秘密武器:Pandas库的深度解析
    标题:Python数据科学的秘密武器:Pandas库的深度解析Python作为数据科学领域的宠儿,其强大的数据处理能力离不开Pandas库的加持。Pandas是一个开源的数据分析和操作库,它提供了快速、灵活和表达力强的数据结构,旨在使数据清洗和分析工作变得更加简单易行。本文将深入探讨Pandas库......
  • 云中韧性:Spring Cloud服务调用重试机制深度解析
    标题:云中韧性:SpringCloud服务调用重试机制深度解析在微服务架构中,服务间的调用可能会因为网络问题、服务不可达、资源竞争等原因失败。SpringCloud作为微服务架构的主流实现框架,提供了一套完整的服务调用重试机制,以增强系统的健壮性和可靠性。本文将详细探讨SpringCloud......