A pre-processing method for finite element program generator

By automating the translation and processing of the finite element program generator's preprocessing methods, the errors and inefficiencies caused by users manually converting physical problems are solved, achieving efficient and accurate code generation.

CN122152290APending Publication Date: 2026-06-05INST OF ROCK & SOIL MECHANICS CHINESE ACAD OF SCI

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
INST OF ROCK & SOIL MECHANICS CHINESE ACAD OF SCI
Filing Date
2026-02-26
Publication Date
2026-06-05

Smart Images

  • Figure CN122152290A_ABST
    Figure CN122152290A_ABST
Patent Text Reader

Abstract

The application discloses a preprocessing method for a finite element program generator, which is used for automatically translating and converting a high-level XML format physical problem description file, i.e., an.flc file, written by a user into underlying C language source code and a series of input scripts required by a finite element program generator (FEPG) solver, and specifically includes a parsing mode and a modification mode; and the application can automatically translate a high-level physical problem description into underlying source code and input scripts.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer-aided engineering (CAE) technology, and in particular to a preprocessing method for a finite element program generator. Background Technology

[0002] Finite element analysis (FEA) is a powerful numerical technique for solving complex engineering and physical problems. FEPG (Finite Element Program Generation System) is a tool for automatically generating dedicated finite element solvers. It allows users to input partial differential equations and algorithm scripts, automatically generating corresponding Fortran or C language source code, which can then be compiled and executed. Its core value lies in providing researchers and developers with highly flexible solutions, rather than directly performing engineering analysis. However, when using finite element program generators like FEPG, users still need to manually convert the governing equations, material parameters, boundary conditions, and other concepts of the physical problem into low-level input files and scripts that FEPG can understand and process. This conversion process not only requires users to be familiar with FEPG's specific syntax and structure but is also fraught with potential sources of error. For example, user-defined variable names may conflict with FEPG internal variables or C language keywords; complex dependencies between parameters, if not defined in the correct order, will lead to compilation errors; boundary conditions containing complex functions (such as if-else logic) are difficult to express directly in standard formats. Existing toolchains often lack an intelligent front-end to systematically solve these problems, resulting in low development efficiency and a high risk of introducing difficult-to-detect errors. Therefore, a powerful, automated preprocessing system is needed specifically for FEPG, freeing users from the tedious and error-prone task of writing low-level files. Summary of the Invention

[0003] To address the problems existing in the prior art, the purpose of this invention is to provide a preprocessing method for finite element program generators. This invention can automatically translate high-level physical problem descriptions into low-level source code and input scripts.

[0004] To achieve the above objectives, the technical solution adopted by this invention is: a preprocessing method for a finite element program generator, used to automatically translate and convert user-written high-level XML format physical problem description files, i.e., .flc files, into the underlying C language source code and a series of input scripts required by the finite element program generator (FEPG) solver, specifically including:

[0005] (1) Parsing mode: Parse the high-level XML format physics problem description file, create an in-memory object model about the physics problem, the in-memory object model includes user-defined parameters and mathematical expressions; analyze the interdependencies between the user-defined parameters, and determine a hierarchical definition priority for each parameter; translate the user-defined mathematical expressions into a C code compatible format, wherein the translation process includes steps for expanding the complex function processing inside the mathematical expressions; based on the in-memory object model and the translated expressions, generate a series of source code and script files required by FEPG, wherein the definition order of user-defined parameters in the generated files is arranged according to the determined hierarchical priority;

[0006] (2) Modification mode: After the script generated by FEPG compiles the initial C language source files, it automatically opens one or more of them; and in a programmatic manner, according to a set of predefined instructions, it performs code injection, replacement or deletion operations in the opened files.

[0007] As a further improvement to the present invention, in (1), it further includes:

[0008] A hash function is used to convert the names defined by the user for parameters and variables into unique internal identifiers to prevent naming conflicts with FEPG internal variables or C language keywords; and a conflict check is performed on the generated internal identifiers to ensure their uniqueness.

[0009] As a further improvement of the present invention, in (1), the interdependencies between the user-defined parameters are analyzed, and a hierarchical definition priority is determined for each parameter as follows:

[0010] Assign a priority of 0 to any parameter that does not depend on other user-defined parameters; recursively or iteratively assign a priority of N+1 to a parameter, where N is the highest priority among all other user-defined parameters that the parameter depends on.

[0011] As a further improvement of the present invention, in (1), the steps for processing the complex function inside the mathematical expression are as follows:

[0012] Identify a complex function call within a mathematical expression, replace the entire function call with a unique temporary variable within the expression, and generate a separate C code block containing conditional statements that calculates the value of the temporary variable, with logic consistent with the complex function.

[0013] As a further improvement to the present invention, in (1), it further includes:

[0014] Perform symbolic differentiation on a user-defined mathematical expression to generate a derivative expression. If the mathematical expression contains a complex function, the symbolic differentiation is performed as follows: represent the expression as a binary tree, where leaf nodes correspond to consecutive function segments; calculate the derivative of the expression at each leaf node; and recursively reassemble the derivative results from the leaf nodes upwards to construct a single, complete expression representing the derivative of the entire complex function.

[0015] As a further improvement to the present invention, in (2), it further includes:

[0016] Save the modified source file back to disk to enable functionality that the standard FEPG process cannot support.

[0017] The beneficial effects of this invention are:

[0018] The core task of this invention is to automatically translate and convert user-written high-level XML format physics problem description files into the underlying C language source code and a series of input scripts required by FEPG. It operates in two main modes: parsing mode and modification mode. In parsing mode, the system avoids naming conflicts through name hashing, ensures the correct order of parameter definitions through dependency analysis and sorting, and handles complex mathematical expressions containing complex functions such as if, max, and min using an innovative function expansion technique. Furthermore, it can perform symbolic differentiation on expressions containing complex functions. In modification mode, as a post-processor, it performs deep customization modifications on the C code generated by FEPG. This invention ultimately outputs a complete set of FEPG computational project files and generates a master control script to automate the entire compilation and computation process. Attached Figure Description

[0019] Figure 1 This is an overall workflow diagram of an embodiment of the present invention;

[0020] Figure 2 This is a schematic diagram of the name hashing process in an embodiment of the present invention;

[0021] Figure 3 This is a schematic diagram illustrating the process of performing dependency analysis and priority (rank) sorting on a user-defined parameter in an embodiment of the present invention;

[0022] Figure 4 This is a schematic diagram of the complex expression expansion process in an embodiment of the present invention. Detailed Implementation

[0023] The embodiments of the present invention will now be described in detail with reference to the accompanying drawings.

[0024] Example

[0025] like Figure 1 As shown, a preprocessing method for a finite element program generator (FEPG) is presented, serving as an advanced preprocessor tailored for FEPG. Its core task is to automatically translate and convert user-written high-level XML-formatted physical problem description files (.flc files) into the underlying C language source code and a series of input scripts required by the FEPG solver. The core contribution of this embodiment lies in a processing flow comprising two main modes:

[0026] 1. Parse Mode: This is the core mode, responsible for generating all the files required for calculations from the .flc file, including:

[0027] (1) Name hashing: To avoid naming conflicts, the system uses the MD5 hash algorithm to systematically convert user-defined parameter and variable names into unique, prefixed internal identifiers and includes a conflict detection mechanism.

[0028] (2) Dependency Analysis and Ranking: The system analyzes the dependencies between user-defined parameters and assigns a "Rank" priority to each parameter through topological sorting. This ensures that all parameters are defined before they are used in the generated code.

[0029] (3) Advanced Expression Processing: The system can parse complex mathematical expressions containing various functions (such as piecewise functions, trigonometric functions, power and logarithmic functions, and user-defined functions). It uniquely decomposes the expressions by replacing these function calls with temporary variables and generating corresponding C code logic separately.

[0030] (4) Symbolic differentiation: For a specific expression (such as the relevant terms in the SUPG method), the system can perform symbolic differentiation on expressions containing piecewise functions. It converts the expression into a binary tree data structure, differentiates the continuous function segments at the leaf nodes, and then recursively reassembles the derivatives of each segment to finally form the complete derivative expression of the entire piecewise function.

[0031] 2. Modify Mode: This mode acts as a post-processor. After FEPG compiles the initial C code based on the scripts generated by this system, this mode programmatically modifies this C code to achieve advanced functions that the standard FEPG process cannot support. This system outputs a complete set of computational project files that can be directly compiled and run by FEPG and subsequent C compilers.

[0032] The following is a further explanation of this embodiment:

[0033] The process begins at the program entry point, main.cpp, which selects one of two operation modes based on the parameters passed by the user via the command line: parsing mode or modification mode.

[0034] 1. Parse Mode: Generates FEPG input files and is the core workflow. It converts .flc files into a complete FEPG project.

[0035] 1.1. Initialization and XML Data Parsing:

[0036] When starting in parsing mode, the system first instantiates a Parser object, which then manages the entire parsing process. The Parser class constructor calls the QDomDocument module of the Qt framework to read and parse the user-specified .flc file. The system then reads the information from the XML and stores it into a series of C++ objects in memory by calling a series of internal read_* functions, forming a problem description model. This includes creating DefinedParameter objects, DefinedFunction objects, instances of FieldObject derived classes, and StudyObject and StepObject objects.

[0037] 1.2. Formula processing and dependency analysis:

[0038] After the object model is created, the system enters the core formula processing stage.

[0039] 1.2.1. Name hashing:

[0040] like Figure 2 As shown, to avoid conflicts between user-defined names and reserved keywords in C or FEPG, all usernames are converted to unique internal identifiers. This process is performed by the `utility::convert_user_defined_name` function:

[0041] Standardize: Remove all spaces from the name string.

[0042] Hash calculation: Call Qt's QCryptographicHash class to perform an MD5 hash operation on the name string.

[0043] Truncation and prefixing: Extract the first N characters of the hash result and add a prefix according to its type ('p' represents a parameter, 'v' represents a variable).

[0044] Conflict detection: The Parser::check_duplicate_hash_name() function sorts and checks all generated hash names for duplicates. If a collision is detected, it will exit with an error.

[0045] 1.2.2. Dependency Analysis and Parameter Ranking:

[0046] like Figure 3 As shown, to ensure that parameters are defined before use, the system uses the `Parser::rank_parameters` and `DefinedParameter::get_define_rank` methods to perform topological sorting of the parameter dependency network and assign a "Rank" priority to each parameter. A parameter that does not depend on any other custom parameters has a Rank of 0. A parameter that depends on other parameters has a Rank equal to the maximum Rank value among all its dependent parameters plus 1. This Rank value is the direct basis for arranging the parameter definition order when generating the .pde file.

[0047] 1.2.3. Expression Processing and Symbolic Differentiation:

[0048] This stage is uniformly scheduled by Parser::hash_expression_in_fields(). For example... Figure 4 As shown, expression preprocessing and hashing: The `hash_expression` function first standardizes the expression, such as converting `a^b` to `func.pow(a,b)` and integer division to floating-point division. Then, it breaks down the expression, hashes each user-defined parameter or variable name, and finally reassembles it into a new expression string containing the hash names.

[0049] Function expansion: The `parse_predefined_element_in_expression` function expands complex non-native C functions (such as `if`, `max`, `min`, etc.) in an expression into a series of simple C assignment statements. The algorithm parses the nested parentheses from the inside out. When a function call is found, it replaces the entire call with a unique temporary variable name (e.g., `elem1`), and simultaneously generates the C code defining this temporary variable (e.g., `double elem1; if(d>e) elem1 =f; else elem1 = g;`) and stores it in the code buffer.

[0050] Symbolic differentiation: The `Differentiator::differentiate` method is used to perform symbolic differentiation on a specific expression. Its process is as follows:

[0051] Preprocessing: First, the parameters are expanded, and then expand_piecewise_function() is called to parse the if, max, and min functions in the expression into a binary tree data structure, where each leaf node of the tree represents a continuous function segment.

[0052] Core differentiation: The program traverses all leaf nodes of the binary tree, passing the expression string of each node to the SymCpp::Symboliccpp_differentiate function, which internally calls the SymbolicC++ library to perform differentiation.

[0053] Post-processing and recombination: The combine_piecewise_function() function recursively traverses the binary tree in a depth-first manner, and uses the form func.if(condition, true_branch_result, false_branch_result) to recombine the derivatives of each segment, finally generating a single, complete derivative expression at the root node.

[0054] 1.3. FEPG file generation:

[0055] This process is dominated by the Parser::write_fepg_files() function, which materializes the memory model into all the disk files required for FEPG.

[0056] Create directory structure: Create corresponding folder structures based on the project, solution scheme, and solution step name.

[0057] File generation loop: The system iterates through all StudyObjects and their containing StepObjects using nested loops. For each solution step, a complete file generation process is executed once, generating files including:

[0058] .pde (partial differential equation file): The system retrieves the script template corresponding to the physics field and writes the C code definitions of all dependent parameters strictly in ascending order of the previously calculated Rank. All expressions are processed by function expansion.

[0059] .fbc (flux boundary condition file): For Neumann boundary conditions, a similar process to that used for generating .pde files is employed.

[0060] boundbft.c (Boundary Condition C Code): This file is entirely programmatically generated to handle complex initial and Dirichlet boundary conditions. The system generates a separate, unique C function for each degree of freedom of each condition, and generates the FEPG-compliant main functions fbound() and ibound(), which internally contain a large if-else structure for precisely calling the corresponding evaluation function based on the runtime ID.

[0061] Control and definition files (.gcn, .mdi, .pos): These files are generated to control the solution algorithm, associate the physics field with the .pde file, and declare post-processing output variables.

[0062] xmldat.txt (Geometry and Condition Association File): This file is generated to associate a geometric entity with the physical fields, initial conditions, or boundary conditions applied to it.

[0063] Generate the project master control file (.bat): Finally, the system generates a batch file that defines the complete calculation process: calling the FEPG compiler, then calling the modification mode of this program, then calling the C compiler, and finally running the executable file.

[0064] 2. Modify Mode: Post-Generation Code Customization. When the batch file executes to the point where it invokes the modify mode, a Modifier object is created. This mode performs a series of precise modification tasks on the C source files just generated by FEPG. It reads instruction code blocks wrapped in specific keywords from the supplementary.txt file. Next, it opens the object files (such as bft.c, gidpre.c) and reads their contents into memory. Based on the read instructions, it calls functions such as replace_line and insert_line_after to precisely replace, insert, or delete lines of code in memory. Finally, it writes the modified code back to disk, overwriting the original file. This process enables the program to achieve advanced functions that the standard FEPG process cannot support.

[0065] The embodiments described above are merely illustrative of specific implementations of the present invention, and while the descriptions are detailed, they should not be construed as limiting the scope of the present invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these modifications and improvements all fall within the scope of protection of the present invention.

Claims

1. A preprocessing method for a finite element program generator, characterized in that, This is used to automatically translate and convert user-written high-level XML format physical problem description files, i.e., .flc files, into the underlying C language source code and a series of input scripts required by the finite element program generator FEPG solver, specifically including: (1) Parsing mode: Parse the high-level XML format physics problem description file, create an in-memory object model about the physics problem, the in-memory object model includes user-defined parameters and mathematical expressions; analyze the interdependencies between the user-defined parameters, and determine a hierarchical definition priority for each parameter; translate the user-defined mathematical expressions into a C code compatible format, wherein the translation process includes steps for expanding the complex function processing inside the mathematical expressions; based on the in-memory object model and the translated expressions, generate a series of source code and script files required by FEPG, wherein the definition order of user-defined parameters in the generated files is arranged according to the determined hierarchical priority; (2) Modification mode: After the script generated by FEPG compiles the initial C language source files, it automatically opens one or more of them; and in a programmatic manner, according to a set of predefined instructions, it performs code injection, replacement or deletion operations in the opened files.

2. The preprocessing method for a finite element program generator according to claim 1, characterized in that, In (1), it also includes: A hash function is used to convert the names defined by the user for parameters and variables into unique internal identifiers to prevent naming conflicts with FEPG internal variables or C language keywords; and a conflict check is performed on the generated internal identifiers to ensure their uniqueness.

3. The preprocessing method for a finite element program generator according to claim 1, characterized in that, In (1), the interdependencies between the user-defined parameters are analyzed, and a hierarchical definition priority is determined for each parameter as follows: Assign a priority of 0 to any parameter that does not depend on other user-defined parameters; recursively or iteratively assign a priority of N+1 to a parameter, where N is the highest priority among all other user-defined parameters that the parameter depends on.

4. The preprocessing method for a finite element program generator according to claim 1, characterized in that, In (1), the specific steps for processing complex functions inside the mathematical expression are as follows: Identify a complex function call within a mathematical expression, replace the entire function call with a unique temporary variable within the expression, and generate a separate C code block containing conditional statements that calculates the value of the temporary variable, with logic consistent with the complex function.

5. The preprocessing method for a finite element program generator according to claim 4, characterized in that, In (1), it also includes: Perform symbolic differentiation on a user-defined mathematical expression to generate a derivative expression. If the mathematical expression contains a complex function, the symbolic differentiation is performed as follows: represent the expression as a binary tree, where leaf nodes correspond to consecutive function segments; calculate the derivative of the expression at each leaf node; and recursively reassemble the derivative results from the leaf nodes upwards to construct a single, complete expression representing the derivative of the entire complex function.

6. The preprocessing method for a finite element program generator according to claim 1, characterized in that, In (2), it also includes: Save the modified source file back to disk to enable functionality that the standard FEPG process cannot support.