Problem: Convection Term Finite Difference Scheme Implementation and Evaluation

The problem aims to implement upwind and centered finite difference schemes for 1D convection term calculation in C++, covering scheme structure design, parameter and boundary validity checks, and proper function packaging, to support subsequent numerical analysis including accuracy assessment, stability evaluation, dissipation analysis, boundary condition impact research, and mixed scheme development.


Input/Output: The problem receives five types of inputs: 1) a std::vector<double> storing discrete 1D advection quantity values, 2) an integer indicating the target grid node index for convection term calculation, 3) a double-precision floating point value representing advection velocity, 4) a double-precision floating point value representing uniform grid spacing, 5) pre-declared global double (**)(const std::vector<double>&, int, double, double) pointer variables upwind_pptr and centered_pptr for subproblem 6.1.
The problem generates two categories of outputs: 1) for subproblem 6.1, valid addresses of captureless lambda wrappers for upwind and centered difference schemes assigned to the input global pointer-to-pointer variables, 2) double-precision floating point calculation results of convection terms for both schemes, returning 0.0 for invalid inputs (empty vector, non-positive grid spacing, out-of-bound index, near-zero velocity) and boundary cases.
The problem first initializes the UpwindScheme and CenteredScheme structs inside the specified C++ function, invokes the corresponding compute method or wrapper with input parameters, executes validity checks, applies the matching finite difference formula, and outputs the result or assigns the wrapper address as required.

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please use C++ to generate a function named problem_6_01. Implement upwind and centered finite difference schemes for convection term calculation, create corresponding captureless lambda wrappers, and assign the wrapper function addresses to pre-declared global double function pointer-to-pointer variables upwind_pptr and centered_pptr.
function_header: void problem_06_01() 
test_cases: int main() {
    problem_6_01();
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 1: Multiple Numerical Schemes and Accuracy Assessment\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        std::vector<double> Q = {0.5, 1.2, 3.5, 2.8, 4.3};
        double u = 1.0, dx = 1.0;
        
        double result_upwind = (*upwind_pptr)(Q, 2, u, dx);
        double result_centered = (*centered_pptr)(Q, 2, u, dx);
        double expected_upwind = -u * (Q[2] - Q[1]) / dx;
        double expected_centered = -u * (Q[3] - Q[1]) / (2.0 * dx);
        
        test1_passed = (std::abs(result_upwind - expected_upwind) < 1e-12 && 
                       std::abs(result_centered - expected_centered) < 1e-12 &&
                       std::abs(result_upwind - result_centered) > 0.2);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q1 = {0.2, 1.1, 3.2, 4.1};
        double u1 = 1.0, dx1 = 1.0;
        
        double result1 = (*upwind_pptr)(Q1, 2, u1, dx1);
        
        std::vector<double> Q2 = {0.2, 0.65, 1.15, 2.15, 3.15, 4.1};
        double dx2 = 0.5;
        double result2 = (*upwind_pptr)(Q2, 3, u1, dx2);
        
        std::vector<double> Q3(13);
        for (int i = 0; i < 13; ++i) {
            Q3[i] = 0.2 + (4.1 - 0.2) * i / 12.0 + 0.1 * sin(i * 0.3);
        }
        double dx3 = 0.25;
        double result3 = (*upwind_pptr)(Q3, 6, u1, dx3);
        
        double expected1 = -u1 * (Q1[2] - Q1[1]) / dx1;
        double expected2 = -u1 * (Q2[3] - Q2[2]) / dx2;
        double expected3 = -u1 * (Q3[6] - Q3[5]) / dx3;
        
        bool results_different = (std::abs(result1 - result2) > 1e-10 || std::abs(result2 - result3) > 1e-10);
        
        test2_passed = (std::abs(result1 - expected1) < 1e-12 && 
                       std::abs(result2 - expected2) < 1e-12 &&
                       std::abs(result3 - expected3) < 1e-12 &&
                       std::isfinite(result1) && std::isfinite(result2) && std::isfinite(result3) &&
                       results_different);
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q_test = {0.2, 1.5, 3.8, 2.1, 4.3};
        double u = 0.8, dx = 1.0;
        
        double result_upwind = (*upwind_pptr)(Q_test, 2, u, dx);
        double result_centered = (*centered_pptr)(Q_test, 2, u, dx);
        double expected_upwind = -u * (Q_test[2] - Q_test[1]) / dx;
        double expected_centered = -u * (Q_test[3] - Q_test[1]) / (2.0 * dx);
        double diff = std::abs(result_upwind - result_centered);
        
        test3_passed = (std::abs(result_upwind - expected_upwind) < 1e-12 &&
                       std::abs(result_centered - expected_centered) < 1e-12 &&
                       diff > 0.1);
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

step_number: 2
step_description_prompt: Please use C++ to write a function named problem_6_02. Define two structs UpwindScheme and CenteredScheme inside the function. Each has a double-type compute method for 1D advection flux calculation: Upwind uses direction-based one-sided difference, Centered uses second-order central difference, add necessary input validity checks.
function_header: void problem_06_02() 
test_cases: int main() {
    problem_6_02();
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 2: Stability Assessment and Computational Efficiency Evaluation\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        auto calculate_cfl = [](double u, double dt, double dx) -> double {
            if (dx <= 0.0) return 0.0;
            return std::abs(u) * dt / dx;
        };
        
        double cfl1 = calculate_cfl(1.0, 0.5, 1.0);
        double cfl2 = calculate_cfl(1.0, 1.0, 1.0);
        double cfl3 = calculate_cfl(2.0, 0.75, 1.0);
        
        double expected_cfl1 = 0.5;
        double expected_cfl2 = 1.0;
        double expected_cfl3 = 1.5;
        
        bool monotonic = (cfl1 < cfl2 && cfl2 < cfl3);
        double avg_cfl = (cfl1 + cfl2 + cfl3) / 3.0;
        
        test1_passed = (std::abs(cfl1 - expected_cfl1) < 1e-12 && 
                       std::abs(cfl2 - expected_cfl2) < 1e-12 &&
                       std::abs(cfl3 - expected_cfl3) < 1e-12 &&
                       monotonic && avg_cfl > 0.95);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q(1000, 1.0);
        double u = 1.0, dx = 1.0;
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        
        auto start1 = std::chrono::high_resolution_clock::now();
        for (int i = 0; i < 1000; ++i) {
            upwind.compute(Q, 500, u, dx);
        }
        auto end1 = std::chrono::high_resolution_clock::now();
        auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start1).count();
        
        auto start2 = std::chrono::high_resolution_clock::now();
        for (int i = 0; i < 1000; ++i) {
            centered.compute(Q, 500, u, dx);
        }
        auto end2 = std::chrono::high_resolution_clock::now();
        auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start2).count();
        
        double efficiency_ratio = static_cast<double>(duration1) / static_cast<double>(duration2);
        
        double result_upwind = upwind.compute(Q, 500, u, dx);
        double result_centered = centered.compute(Q, 500, u, dx);
        double expected_upwind = 0.0;
        double expected_centered = 0.0;
        
        test2_passed = (std::abs(result_upwind - expected_upwind) < 1e-12 && 
                       std::abs(result_centered - expected_centered) < 1e-12 &&
                       duration1 > 0 && duration2 > 0 && efficiency_ratio >= 0.5 && efficiency_ratio <= 3.0);
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q = {0.1, 1.5, 3.2, 2.8, 4.5};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        
        double result_upwind = upwind.compute(Q, 2, 1.0, 1.0);
        double result_centered = centered.compute(Q, 2, 1.0, 1.0);
        double expected_upwind = -1.0 * (Q[2] - Q[1]) / 1.0;
        double expected_centered = -1.0 * (Q[3] - Q[1]) / (2.0 * 1.0);
        
        double diff = std::abs(result_upwind - result_centered);
        
        test3_passed = (std::abs(result_upwind - expected_upwind) < 1e-12 && 
                       std::abs(result_centered - expected_centered) < 1e-12 &&
                       diff > 0.15);
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

step_number: 3
step_description_prompt: Please write a C++ function named problem_6_03. Inside it, define two structs UpwindScheme and CenteredScheme, each with a compute method taking related parameters to calculate convection term finite differences, plus proper invalid input and boundary checks.
function_header: void problem_06_03() 
test_cases: int main() {
    problem_6_03();
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 3: Numerical Dissipation and Dispersion Analysis\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        std::vector<double> Q = {0.3, 1.2, 2.5, 3.8, 4.1};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        UpwindScheme upwind;
        double result = upwind.compute(Q, 2, 1.0, 1.0);
        double expected = -1.0 * (Q[2] - Q[1]) / 1.0;
        
        test1_passed = (std::abs(result - expected) < 1e-12 && std::abs(result) > 0.9);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q = {0.3, 1.2, 2.5, 3.8, 4.1};
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        CenteredScheme centered;
        double result = centered.compute(Q, 2, 1.0, 1.0);
        double expected = -1.0 * (Q[3] - Q[1]) / (2.0 * 1.0);
        
        test2_passed = (std::abs(result - expected) < 1e-12 && std::abs(result) > 0.9);
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q = {0.1, 1.5, 3.2, 2.8, 4.5};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        double result_upwind = upwind.compute(Q, 2, 1.0, 1.0);
        double result_centered = centered.compute(Q, 2, 1.0, 1.0);
        double expected_upwind = -1.0 * (Q[2] - Q[1]) / 1.0;
        double expected_centered = -1.0 * (Q[3] - Q[1]) / (2.0 * 1.0);
        double diff = std::abs(result_upwind - result_centered);
        
        test3_passed = (std::abs(result_upwind - expected_upwind) < 1e-12 && 
                       std::abs(result_centered - expected_centered) < 1e-12 &&
                       diff > 0.15);
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

step_number: 4
step_description_prompt: Please write a C++ function named problem_6_04. Define two structs inside this function: UpwindScheme and CenteredScheme, each has a compute method to calculate numerical convection terms with their corresponding schemes, add necessary parameter and boundary validity checks, follow standard C++ syntax requirements.
function_header: void problem_06_04() 
test_cases: int main() {
    problem_6_04();
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 4: Boundary Condition Impact Analysis and Adaptive Scheme Selection\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        std::vector<double> Q = {0.2, 1.3, 2.7, 3.9, 4.5};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        UpwindScheme upwind;
        double result1 = upwind.compute(Q, 2, 1.0, 1.0);
        
        std::vector<double> Q2 = {4.5, 3.9, 2.7, 1.3, 0.2};
        double result2 = upwind.compute(Q2, 2, 1.0, 1.0);
        
        double expected1 = -1.0 * (Q[2] - Q[1]) / 1.0;
        double expected2 = -1.0 * (Q2[2] - Q2[1]) / 1.0;
        
        test1_passed = (std::abs(result1 - expected1) < 1e-12 && std::abs(result2 - expected2) < 1e-12 &&
                       std::abs(result1 - result2) > 1.3);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q = {0.3, 1.2, 2.5, 3.8, 4.1};
        
        auto calculate_cfl = [](double u, double dt, double dx) -> double {
            if (dx <= 0.0) return 0.0;
            return std::abs(u) * dt / dx;
        };
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        
        double cfl = calculate_cfl(1.0, 0.5, 1.0);
        double expected_cfl = 0.5;
        double result = (cfl < 0.5) ? centered.compute(Q, 2, 1.0, 1.0) : upwind.compute(Q, 2, 1.0, 1.0);
        double expected_result = -1.0 * (Q[2] - Q[1]) / 1.0;
        
        test2_passed = (std::abs(cfl - expected_cfl) < 1e-12 && std::abs(result - expected_result) < 1e-12);
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q = {0.2, 1.3, 2.7, 3.9, 4.5};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        UpwindScheme upwind;
        
        double result1 = upwind.compute(Q, 2, 1.0, 1.0);
        double result2 = upwind.compute(Q, 2, 1.0, 1.0);
        double expected = -1.0 * (Q[2] - Q[1]) / 1.0;
        double consistency = std::abs(result1 - result2);
        
        test3_passed = (consistency < 1e-12 && std::abs(result1 - expected) < 1e-12);
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

step_number: 5
step_description_prompt: Please use C++ to generate a function named problem_6_05. Inside the function, define two structs UpwindScheme and CenteredScheme, each with a compute method to calculate numerical discrete values, handle invalid parameters and boundary cases, follow the corresponding upwind and centered difference scheme rules respectively.
function_header: void problem_06_05() 
test_cases: int main() {
    problem_6_05();
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 5: Mixed Scheme Implementation and Parallelization\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        std::vector<double> Q = {0.2, 1.5, 3.8, 2.1, 4.3};
        double u = 1.0, dx = 1.0;
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        
        double result_upwind = upwind.compute(Q, 2, u, dx);
        double result_centered = centered.compute(Q, 2, u, dx);
        
        double expected_upwind = -u * (Q[2] - Q[1]) / dx;
        double expected_centered = -u * (Q[3] - Q[1]) / (2.0 * dx);
        
        double error_upwind = std::abs(result_upwind - expected_upwind);
        double error_centered = std::abs(result_centered - expected_centered);
        double format_diff = std::abs(result_upwind - result_centered);
        
        test1_passed = (error_upwind < 1e-12 && error_centered < 1e-12 && format_diff > 0.15);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q = {0.2, 1.3, 2.7, 3.9, 4.5};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        UpwindScheme upwind;
        
        double result1 = upwind.compute(Q, 2, 1.0, 1.0);
        double result2 = upwind.compute(Q, 2, 1.0, 1.0);
        double expected = -1.0 * (Q[2] - Q[1]) / 1.0;
        double consistency = std::abs(result1 - result2);
        
        test2_passed = (consistency < 1e-12 && std::abs(result1 - expected) < 1e-12);
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q1 = {0.3, 1.2, 2.5, 3.8, 4.1};
        std::vector<double> Q2 = {0.1, 0.5, 5.2, 0.4, 0.2};
        std::vector<double> Q3 = {1.0, 2.0, 3.0, 4.0, 5.0};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 0 || i >= static_cast<int>(Q.size())) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                if (u >= 0.0) {
                    if (i >= 1) return -u * (Q[i] - Q[i-1]) / dx;
                    return 0.0;
                } else {
                    if (i < static_cast<int>(Q.size()) - 1) return -u * (Q[i+1] - Q[i]) / dx;
                    return 0.0;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0 || i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                if (std::abs(u) < 1e-15) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        double u = 1.0, dx = 1.0;
        
        double r1_up = upwind.compute(Q1, 2, u, dx);
        double r1_cen = centered.compute(Q1, 2, u, dx);
        double r2_up = upwind.compute(Q2, 2, u, dx);
        double r2_cen = centered.compute(Q2, 2, u, dx);
        double r3_up = upwind.compute(Q3, 2, u, dx);
        double r3_cen = centered.compute(Q3, 2, u, dx);
        
        double expected1_up = -u * (Q1[2] - Q1[1]) / dx;
        double expected1_cen = -u * (Q1[3] - Q1[1]) / (2.0 * dx);
        double expected2_up = -u * (Q2[2] - Q2[1]) / dx;
        double expected2_cen = -u * (Q2[3] - Q2[1]) / (2.0 * dx);
        double expected3_up = -u * (Q3[2] - Q3[1]) / dx;
        double expected3_cen = -u * (Q3[3] - Q3[1]) / (2.0 * dx);
        
        test3_passed = (std::abs(r1_up - expected1_up) < 1e-12 &&
                       std::abs(r1_cen - expected1_cen) < 1e-12 &&
                       std::abs(r2_up - expected2_up) < 1e-12 &&
                       std::abs(r2_cen - expected2_cen) < 1e-12 &&
                       std::abs(r3_up - expected3_up) < 1e-12 &&
                       std::abs(r3_cen - expected3_cen) < 1e-12);
        
        double total_diff = std::abs(r1_up - r1_cen) + std::abs(r2_up - r2_cen) + std::abs(r3_up - r3_cen);
        
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

step_number: 6
step_description_prompt: Please write C++ code to implement a function named problem_6_06.
Inside this function, define two structs: UpwindScheme and CenteredScheme. Each struct has a compute method to calculate 1D advection numerical flux with proper boundary checks and input validation. Upwind selects difference direction per velocity sign, while Centered adopts second-order central difference scheme.
function_header: void problem_06_06() 
test_cases: int main() {
    problem_6_06();
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 6: Result Validation System and Report Generation\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        std::vector<double> Q = {0.2, 1.5, 3.8, 2.1, 4.3};
        double u = 1.0, dx = 1.0;
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0) return 0.0;
                if (u >= 0) {
                    if (i < 1 || i >= static_cast<int>(Q.size())) return 0.0;
                    return -u * (Q[i] - Q[i-1]) / dx;
                } else {
                    if (i < 0 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                    return -u * (Q[i+1] - Q[i]) / dx;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0) return 0.0;
                if (i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        
        double result_upwind = upwind.compute(Q, 2, u, dx);
        double result_centered = centered.compute(Q, 2, u, dx);
        
        double expected_upwind = -u * (Q[2] - Q[1]) / dx;
        double expected_centered = -u * (Q[3] - Q[1]) / (2.0 * dx);
        
        double error_upwind = std::abs(result_upwind - expected_upwind);
        double error_centered = std::abs(result_centered - expected_centered);
        double format_diff = std::abs(result_upwind - result_centered);
        
        test1_passed = (error_upwind < 1e-12 && error_centered < 1e-12 && format_diff > 0.15);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<std::vector<double>> test_cases = {
            {0.3, 1.2, 2.5, 3.8, 4.1},
            {0.1, 0.5, 5.2, 0.4, 0.2},
            {1.0, 2.0, 3.0, 4.0, 5.0}
        };
        std::vector<double> u_values = {0.5, 1.0, 2.0};
        std::vector<double> dx_values = {0.5, 1.0, 2.0};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0) return 0.0;
                if (u >= 0) {
                    if (i < 1 || i >= static_cast<int>(Q.size())) return 0.0;
                    return -u * (Q[i] - Q[i-1]) / dx;
                } else {
                    if (i < 0 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                    return -u * (Q[i+1] - Q[i]) / dx;
                }
            }
        };
        
        struct CenteredScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0) return 0.0;
                if (i < 1 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                return -u * (Q[i+1] - Q[i-1]) / (2.0 * dx);
            }
        };
        
        UpwindScheme upwind;
        CenteredScheme centered;
        
        double total_upwind_sum = 0.0;
        double total_centered_sum = 0.0;
        
        for (size_t i = 0; i < test_cases.size(); ++i) {
            double r_up = upwind.compute(test_cases[i], 2, u_values[i], dx_values[i]);
            double r_cen = centered.compute(test_cases[i], 2, u_values[i], dx_values[i]);
            total_upwind_sum += r_up;
            total_centered_sum += r_cen;
        }
        
        double avg_upwind = total_upwind_sum / test_cases.size();
        double avg_centered = total_centered_sum / test_cases.size();
        double performance_diff = std::abs(avg_upwind - avg_centered);
        
        test2_passed = (performance_diff > 0.15 && std::isfinite(avg_upwind) && std::isfinite(avg_centered));
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        std::vector<double> Q_coarse = {0.2, 1.1, 3.2, 4.1};
        std::vector<double> Q_fine = {0.2, 0.65, 1.15, 2.15, 3.15, 4.1};
        
        struct UpwindScheme {
            double compute(const std::vector<double>& Q, int i, double u, double dx) {
                if (Q.empty() || dx <= 0.0) return 0.0;
                if (u >= 0) {
                    if (i < 1 || i >= static_cast<int>(Q.size())) return 0.0;
                    return -u * (Q[i] - Q[i-1]) / dx;
                } else {
                    if (i < 0 || i >= static_cast<int>(Q.size()) - 1) return 0.0;
                    return -u * (Q[i+1] - Q[i]) / dx;
                }
            }
        };
        
        UpwindScheme upwind;
        double u = 1.0;
        
        double result_coarse = upwind.compute(Q_coarse, 2, u, 1.0);
        double result_fine = upwind.compute(Q_fine, 3, u, 0.5);
        
        double expected_coarse = -u * (Q_coarse[2] - Q_coarse[1]) / 1.0;
        double expected_fine = -u * (Q_fine[3] - Q_fine[2]) / 0.5;
        
        double error_coarse = std::abs(result_coarse - expected_coarse);
        double error_fine = std::abs(result_fine - expected_fine);
        
        test3_passed = (error_coarse < 1e-12 && error_fine < 1e-12 && 
                       std::abs(result_coarse - result_fine) > 0.05);
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

step_number: 7
step_description_prompt: Please use C++ to generate a function named problem_6_07(). First define a ComparisonResults struct storing scheme name list and three score vectors for accuracy, stability, efficiency. Then define a ReportGenerator struct with three methods to generate HTML, plain text PDF, Markdown format numerical scheme comparison reports respectively, save output to specified files, handle missing score entries properly.
function_header: void problem_06_07() 
test_cases: int main() {
    
    std::cout << "========================================\n";
    std::cout << "Problem 6-Subproblem 7: Convergence Analysis and Grid Refinement Study\n";
    std::cout << "========================================\n";
    
    bool test1_passed = false, test2_passed = false, test3_passed = false;
    
    {
        struct ComparisonResults {
            std::vector<std::string> scheme_names;
            std::vector<double> accuracy_scores;
            std::vector<double> stability_scores;
            std::vector<double> efficiency_scores;
        };
        
        struct ReportGenerator {
            void generate_html_report(const ComparisonResults& results, const std::string& filename) {
                std::ofstream file(filename);
                if (!file.is_open()) return;
                file << "<html><head><title>Comparison Report</title></head><body>\n";
                file << "<h1>Numerical Scheme Comparison Report</h1>\n";
                file << "<table border='1'>\n";
                file << "<tr><th>Scheme</th><th>Accuracy</th><th>Stability</th><th>Efficiency</th></tr>\n";
                for (size_t i = 0; i < results.scheme_names.size(); ++i) {
                    file << "<tr><td>" << results.scheme_names[i] << "</td>";
                    if (i < results.accuracy_scores.size()) {
                        file << "<td>" << results.accuracy_scores[i] << "</td>";
                    }
                    if (i < results.stability_scores.size()) {
                        file << "<td>" << results.stability_scores[i] << "</td>";
                    }
                    if (i < results.efficiency_scores.size()) {
                        file << "<td>" << results.efficiency_scores[i] << "</td>";
                    }
                    file << "</tr>\n";
                }
                file << "</table></body></html>\n";
                file.close();
            }
        };
        
        ComparisonResults results;
        results.scheme_names = {"Upwind", "Centered", "MUSCL"};
        results.accuracy_scores = {0.8, 0.9, 0.95};
        results.stability_scores = {0.9, 0.7, 0.85};
        results.efficiency_scores = {0.9, 0.8, 0.7};
        
        ReportGenerator generator;
        generator.generate_html_report(results, "test_report.html");
        
        size_t expected_num_schemes = 3;
        double actual_accuracy_sum = 0.0;
        for (double acc : results.accuracy_scores) actual_accuracy_sum += acc;
        double expected_accuracy_sum = 2.65;
        
        test1_passed = (results.scheme_names.size() == expected_num_schemes && 
                       std::abs(actual_accuracy_sum - expected_accuracy_sum) < 1e-12);
        if (test1_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        struct ComparisonResults {
            std::vector<std::string> scheme_names;
            std::vector<double> accuracy_scores;
            std::vector<double> stability_scores;
            std::vector<double> efficiency_scores;
        };
        
        struct ReportGenerator {
            void generate_pdf_report(const ComparisonResults& results, const std::string& filename) {
                std::ofstream file(filename);
                if (!file.is_open()) return;
                file << "Numerical Scheme Comparison Report\n";
                file << "===================================\n\n";
                for (size_t i = 0; i < results.scheme_names.size(); ++i) {
                    file << "Scheme: " << results.scheme_names[i] << "\n";
                    if (i < results.accuracy_scores.size()) {
                        file << "  Accuracy: " << results.accuracy_scores[i] << "\n";
                    }
                    if (i < results.stability_scores.size()) {
                        file << "  Stability: " << results.stability_scores[i] << "\n";
                    }
                    if (i < results.efficiency_scores.size()) {
                        file << "  Efficiency: " << results.efficiency_scores[i] << "\n";
                    }
                    file << "\n";
                }
                file.close();
            }
            void generate_markdown_report(const ComparisonResults& results, const std::string& filename) {
                std::ofstream file(filename);
                if (!file.is_open()) return;
                file << "# Numerical Scheme Comparison Report\n\n";
                file << "| Scheme | Accuracy | Stability | Efficiency |\n";
                file << "|--------|----------|-----------|------------|\n";
                for (size_t i = 0; i < results.scheme_names.size(); ++i) {
                    file << "| " << results.scheme_names[i] << " | ";
                    if (i < results.accuracy_scores.size()) {
                        file << results.accuracy_scores[i] << " | ";
                    } else {
                        file << "N/A | ";
                    }
                    if (i < results.stability_scores.size()) {
                        file << results.stability_scores[i] << " | ";
                    } else {
                        file << "N/A | ";
                    }
                    if (i < results.efficiency_scores.size()) {
                        file << results.efficiency_scores[i] << " |\n";
                    } else {
                        file << "N/A |\n";
                    }
                }
                file.close();
            }
        };
        
        ComparisonResults results;
        results.scheme_names = {"Upwind", "Centered"};
        results.accuracy_scores = {0.8, 0.9};
        results.stability_scores = {0.9, 0.7};
        results.efficiency_scores = {0.9, 0.8};
        
        ReportGenerator generator;
        generator.generate_pdf_report(results, "test_report.txt");
        generator.generate_markdown_report(results, "test_report.md");
        
        size_t expected_num_schemes = 2;
        double actual_accuracy_sum = 0.0;
        for (double acc : results.accuracy_scores) actual_accuracy_sum += acc;
        double expected_accuracy_sum = 1.7;
        
        test2_passed = (results.scheme_names.size() == expected_num_schemes && 
                       std::abs(actual_accuracy_sum - expected_accuracy_sum) < 1e-12);
        if (test2_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    {
        struct ComparisonResults {
            std::vector<std::string> scheme_names;
            std::vector<double> accuracy_scores;
            std::vector<double> stability_scores;
            std::vector<double> efficiency_scores;
        };
        
        struct ReportGenerator {
            void generate_html_report(const ComparisonResults& results, const std::string& filename) {
                std::ofstream file(filename);
                if (!file.is_open()) return;
                file << "<html><head><title>Comparison Report</title></head><body>\n";
                file << "<h1>Numerical Scheme Comparison Report</h1>\n";
                file << "<table border='1'>\n";
                file << "<tr><th>Scheme</th><th>Accuracy</th><th>Stability</th><th>Efficiency</th></tr>\n";
                for (size_t i = 0; i < results.scheme_names.size(); ++i) {
                    file << "<tr><td>" << results.scheme_names[i] << "</td>";
                    if (i < results.accuracy_scores.size()) {
                        file << "<td>" << results.accuracy_scores[i] << "</td>";
                    }
                    if (i < results.stability_scores.size()) {
                        file << "<td>" << results.stability_scores[i] << "</td>";
                    }
                    if (i < results.efficiency_scores.size()) {
                        file << "<td>" << results.efficiency_scores[i] << "</td>";
                    }
                    file << "</tr>\n";
                }
                file << "</table></body></html>\n";
                file.close();
            }
            void generate_pdf_report(const ComparisonResults& results, const std::string& filename) {
                std::ofstream file(filename);
                if (!file.is_open()) return;
                file << "Numerical Scheme Comparison Report\n";
                file << "===================================\n\n";
                for (size_t i = 0; i < results.scheme_names.size(); ++i) {
                    file << "Scheme: " << results.scheme_names[i] << "\n";
                    if (i < results.accuracy_scores.size()) {
                        file << "  Accuracy: " << results.accuracy_scores[i] << "\n";
                    }
                    if (i < results.stability_scores.size()) {
                        file << "  Stability: " << results.stability_scores[i] << "\n";
                    }
                    if (i < results.efficiency_scores.size()) {
                        file << "  Efficiency: " << results.efficiency_scores[i] << "\n";
                    }
                    file << "\n";
                }
                file.close();
            }
            void generate_markdown_report(const ComparisonResults& results, const std::string& filename) {
                std::ofstream file(filename);
                if (!file.is_open()) return;
                file << "# Numerical Scheme Comparison Report\n\n";
                file << "| Scheme | Accuracy | Stability | Efficiency |\n";
                file << "|--------|----------|-----------|------------|\n";
                for (size_t i = 0; i < results.scheme_names.size(); ++i) {
                    file << "| " << results.scheme_names[i] << " | ";
                    if (i < results.accuracy_scores.size()) {
                        file << results.accuracy_scores[i] << " | ";
                    } else {
                        file << "N/A | ";
                    }
                    if (i < results.stability_scores.size()) {
                        file << results.stability_scores[i] << " | ";
                    } else {
                        file << "N/A | ";
                    }
                    if (i < results.efficiency_scores.size()) {
                        file << results.efficiency_scores[i] << " |\n";
                    } else {
                        file << "N/A |\n";
                    }
                }
                file.close();
            }
        };
        
        ComparisonResults results;
        results.scheme_names = {"Upwind"};
        results.accuracy_scores = {0.8};
        results.stability_scores = {0.9};
        results.efficiency_scores = {0.9};
        
        ReportGenerator generator;
        generator.generate_html_report(results, "test1.html");
        generator.generate_pdf_report(results, "test2.txt");
        generator.generate_markdown_report(results, "test3.md");
        
        size_t expected_num_schemes = 1;
        double expected_accuracy = 0.8;
        double expected_stability = 0.9;
        double expected_efficiency = 0.9;
        
        test3_passed = (results.scheme_names.size() == expected_num_schemes &&
                       std::abs(results.accuracy_scores[0] - expected_accuracy) < 1e-12 &&
                       std::abs(results.stability_scores[0] - expected_stability) < 1e-12 &&
                       std::abs(results.efficiency_scores[0] - expected_efficiency) < 1e-12);
        if (test3_passed) {
            std::cout << "correct\n";
        } else {
            std::cout << "wrong\n";
        }
    }
    
    std::cout << "\n========================================\n";
    if (test1_passed && test2_passed && test3_passed) {
        std::cout << "Completely correct!\n";
    }
    std::cout << "========================================\n";
    
    return 0;
}

General solution:

General tests: