首页 > 其他分享 >OFtutorial06_customClasses 解析

OFtutorial06_customClasses 解析

时间:2024-08-13 10:50:48浏览次数:4  
标签:IOobject include customClass customClasses OFtutorial06 const 解析 myDict class

组成

如图

customClass.H

#include "fvCFD.H"

class customClass
{
private:
    label myInt_;

public:
    customClass();
    ~customClass();

    // Access operators - allow the internal value to be set or retrieved.
    // Defined as inline to make the code run faster by avoiding expensive linking.
    // .set() method defined as const as it is not allowed to modify any of the class fields.
    inline label get() const {return myInt_;};
    inline void set(label newInt) {myInt_=newInt;};

    // A basic function
    label basicFunction() const;//basicFunction()函数声明

    // A function which accepts a reference to the mesh and does some operations
    // on it but without modifying it.
    // Passing a reference rather than the entire mesh object instance by value
    // is much more memory efficient - in short, only one mesh object exists
    // and this method only learns to talk to it rather than make a new, separate copy.
    void meshOpFunction(fvMesh& mesh);//meshOpFunction函数声明
};

customClass.C

#include "customClass.H"

customClass::customClass()
{
    myInt_= 0;
}

customClass::~customClass()
{}
//basicFunction()函数定义,返回myInt_*2
label customClass::basicFunction() const
{
    Info << "Calling customClass::basicFunction()" << endl;
    return myInt_*2;
}
//meshOpFunction函数定义,为myInt_赋值mesh.C().size()
void customClass::meshOpFunction(fvMesh& mesh)
{
    Info << "Custom class got a mesh with " << mesh.C().size() << " cells" << endl;
    myInt_ = mesh.C().size();
}

derivedClass.H

#include "fvCFD.H"

// use a standard C++ stringstream object
#include <sstream>

// This class is derived from the IOdictionary, declared in
// $FOAM_SRC/OpenFOAM/db/IOobjects/IOdictionary .
// We derive all the methods by setting maximum access level as public, thus allowing
// all the inherited methods to be accessed in the same way as they were for
// the base class.
class myDict : public IOdictionary
{
public:
    // Main constructor which accepts an IOobject (a file)
    myDict(const IOobject& ioObj);
    ~myDict();

    // custom method which prints the tokens (variables) defined in the file
    void printTokensInTheDict() const;//printTokensInTheDict()函数声明
};

derivedClass.C

#include "derivedClass.H"

myDict::myDict(const IOobject& ioObj)
:
    // Call the base class constructor to make sure all inherited members get
    // initialised as required
    IOdictionary(ioObj)//继承父类
{
// do nothing since we do not have any bespoke fields
}

myDict::~myDict()
{}

void myDict::printTokensInTheDict() const
{
    // retrieve the list of non-space characters in the file using the
    // method defined in dictionary.H, from which the IOdictionary object itself
    // is derived.
    List<token> characters(this->tokens());//获取包含 token 类型对象的列表

    // Create a stream which will hold the message to be printed out.
    // Important to remember about the namespace.
    std::stringstream ss;
    ss << "Tokens in the file:";

    // go over each token in the file
    forAll(characters,i)
        // if the entry is a word, add it to the message
        if (characters[i].isWord())
            ss << "\n" << tab << characters[i].wordToken();

    // print the message - convert to a C-style char array to make sure the
    // printout looks good
    Info << ss.str().c_str() << endl;
}

OFtutorial6.C

源码

头文件

#include "fvCFD.H"

// Include the code for the custom classes declared in .H and defined
// in .C files.
// NOTE: check how the Make/files changed to make sure the additional code gets
// compiled before the main utility.
#include "customClass.H"
#include "derivedClass.H"

主函数

头文件

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

创建一个custom实例

    // Create a custom class instance
    customClass customInstance;
    Info << "Default value " << customInstance.get() << endl;

    // Set a new value
    customInstance.set(10);
    Info << "New value " << customInstance.get() << endl;

    // Call a basic function
    customInstance.basicFunction();

    // Pass a reference to the mesh to the custom class and let it do its things
    customInstance.meshOpFunction(mesh);
    Info << "Yet another value " << customInstance.get() << endl;

创建一个myDict实例

    // Now, create an instance of a derived class which inherits from an IOdictionary object
    myDict myTransportProperties
    (
        IOobject
        (
            "transportProperties",
            runTime.constant(),
            mesh,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    );
    // Create a viscosity scalar using our new class
    dimensionedScalar nu
    (
        "nu",
        dimViscosity,
        myTransportProperties.lookup("nu")
    );
    Info << "Created a viscosity scalar: " << nu << endl;

    // List the contents of the dictionary using the derived class method
    // implemented specifically for this purpose
    myTransportProperties.printTokensInTheDict();

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

    return 0;
}

Make

files

源码

值得注意的是,在OFtutorial6.C文件中并未includecustomClass.CderivedClass.C两个文件,这意味这调用函数只需引用其定义,但函数也需要编译

customClass.C
derivedClass.C
OFtutorial6.C

EXE = $(FOAM_USER_APPBIN)/ofTutorial6

options

与此前章节差别不大,不做赘述

Allwmake、Allwclean、testcase

与此前章节差别不大,不做赘述

标签:IOobject,include,customClass,customClasses,OFtutorial06,const,解析,myDict,class
From: https://www.cnblogs.com/ouqiyo/p/18355468

相关文章

  • 深入解析Node.js中的fs.watch:options与listener详解
    在Node.js中,fs.watch方法是一个功能强大的文件系统监控工具,它允许我们对文件或目录进行实时监控,并在文件或目录发生变化时触发相应的操作。在使用fs.watch时,两个关键的部分是options对象和listener回调函数。本文将详细讲解这两个部分,帮助读者更好地理解和使用fs.watch。一......
  • 如何解决因内存不足导致的 iPhone 白苹果问题:原因解析与修复教程
    引言在使用iPhone的过程中,许多用户可能遇到过“白苹果”现象,即设备启动时只显示白色的Apple标志,却无法进入系统。这种情况通常让人感到困惑和担忧,尤其是当重要数据似乎无法访问时。本文将探讨内存不足导致iPhone白苹果的原因,并提供详细的修复教程,帮助你恢复设备正常运行......
  • 全面解析Gerapy分布式部署:从环境搭建到定时任务,避开Crawlab的坑
    Gerapy分布式部署搭建远程服务器的环境装好带docker服务的系统Docker:容器可生成镜像,也可拉去镜像生成容器示例:将一个环境打包上传到云端(远程服务器),其他8个服务器需要这个环境直接向云端拉取镜像生成容器,进而使用该环境,比如有MYSQL的镜像、Redis的镜像备注:Linux系统下......
  • Scrapy框架进阶攻略:代理设置、请求优化及链家网实战项目全解析
    scrapy框架加代理付费代理IP池middlewares.py#代理IP池classProxyMiddleware(object):proxypool_url='http://127.0.0.1:5555/random'logger=logging.getLogger('middlewares.proxy')asyncdefprocess_request(self,request,spider):......
  • 一文读懂分布式爬虫利器Scrapy-Redis:源码解析、队列管理与去重策略
    分布式利器Scrapy-Redis原理Scrapy-Redis库已经为我们提供了Scrapy分布式的队列、调度器、去重等功能,其GitHub地址为:https://github.com/rmax/scrapy-redis。本节课我们深入掌握利用Redis实现Scrapy分布式的方法,并深入了解Scrapy-Redis的原理。1.获取源码......
  • 一文读懂分布式爬虫利器Scrapy-Redis:源码解析、队列管理与去重策略
    分布式利器Scrapy-Redis原理Scrapy-Redis库已经为我们提供了Scrapy分布式的队列、调度器、去重等功能,其GitHub地址为:https://github.com/rmax/scrapy-redis。本节课我们深入掌握利用Redis实现Scrapy分布式的方法,并深入了解Scrapy-Redis的原理。1.获取源码可以......
  • 跨境电商如何通过谷歌广告提升销量?SEO与SEM术语与策略解析
    在数字化浪潮席卷全球的今天,跨境电商正成为企业拓展国际市场的新引擎。然而,如何在激烈的市场竞争中脱颖而出,成为每个跨境电商企业必须面对的课题。本文将深入探讨如何利用谷歌广告这一强大的营销工具,结合SEO(搜索引擎优化)与SEM(搜索引擎营销)的策略,有效提升跨境电商的销量。SEO与SEM......
  • OFtutorial05_basicParallelComputing解析
    相较第四章增加了并行运算,多了createFields.H和system/decomposeParDict两个文件createFields.H用于定义场操作相关的内容,包括读取场数据decomposeParDict用于定义并行计算的网格和计算域分割相关的参数OFtutorial5.C源码OpenFOAM的并行计算将计算域分成数个小计算区域,......
  • 闲鱼功能全解析:闲置物品快速变现
    咸鱼(现已更名为闲鱼)作为一款闲置交易平台,其功能设计旨在提供一个方便、安全的环境,让用户能够轻松地买卖二手物品。以下是对咸鱼(闲鱼)功能的详细分析商品发布与管理商品发布:用户可以通过简单的操作流程,快速发布自己的闲置物品。发布时,用户可以添加商品图片、描述、价格等信息,以......
  • 自动驾驶系列—智能驾驶的泊车革命:APA自动泊车技术全解析
    ......