Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://www.mooseframework.org 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "MooseApp.h" 11 : #include "CSGOnlyAction.h" 12 : #include "CSGBase.h" 13 : #include "MeshGenerator.h" 14 : 15 : registerMooseAction("MooseApp", CSGOnlyAction, "csg_only"); 16 : registerMooseAction("MooseApp", CSGOnlyAction, "setup_mesh"); 17 : registerMooseAction("MooseApp", CSGOnlyAction, "execute_csg_generators"); 18 : 19 : InputParameters 20 67 : CSGOnlyAction::validParams() 21 : { 22 67 : return Action::validParams(); 23 : } 24 : 25 67 : CSGOnlyAction::CSGOnlyAction(const InputParameters & params) : Action(params) {} 26 : 27 : void 28 198 : CSGOnlyAction::act() 29 : { 30 198 : if (_current_task == "setup_mesh") 31 : { 32 67 : if (!_mesh) 33 0 : mooseError("Cannot generate CSG object without a [Mesh] block in the input file"); 34 67 : _app.getMeshGeneratorSystem().setCSGOnly(); 35 : } 36 131 : else if (_current_task == "execute_csg_generators") 37 : { 38 67 : const auto final_mg_name = _app.getMeshGeneratorSystem().getFinalMeshGeneratorName(); 39 67 : const auto ordered_mg = _app.getMeshGeneratorSystem().getOrderedMeshGenerators(); 40 : 41 174 : for (const auto & generator_set : ordered_mg) 42 305 : for (const auto & generator : generator_set) 43 : { 44 198 : auto csg_obj = generator->generateInternalCSG(); 45 195 : const auto & name = generator->name(); 46 : // If we are running generateCSG for final generator, store CSG object for use in csg_only 47 : // task 48 195 : if (name == final_mg_name) 49 : { 50 64 : _csg_obj = std::move(csg_obj); 51 64 : return; 52 : } 53 : else 54 : { 55 : // Store CSG object created by mesh generator for use by downstream MG's 56 131 : _app.getMeshGeneratorSystem().saveOutputCSGBase(name, csg_obj); 57 : } 58 195 : } 59 128 : } 60 64 : else if (_current_task == "csg_only") 61 : { 62 64 : const auto final_mg_name = _app.getMeshGeneratorSystem().getFinalMeshGeneratorName(); 63 64 : if (!_csg_obj) 64 0 : mooseError("Expecting final generator with name " + final_mg_name + 65 : " but not found in mesh generator tree"); 66 : 67 64 : Moose::out << "Outputting CSGBase object for " + final_mg_name + "\n"; 68 : 69 64 : auto csg_json = _csg_obj->generateOutput(); 70 : 71 : // write generated json to file. Filename will be based on optional argument to 72 : // --csg-only. If not provided, the output name will be based on the filename 73 64 : std::string json_out = _app.parameters().get<std::string>("csg_only"); 74 64 : if (json_out.empty()) 75 48 : json_out = _app.getOutputFileBase() + "_csg.json"; 76 : 77 64 : std::ofstream csg_file; 78 64 : csg_file.open(json_out); 79 64 : csg_file << csg_json.dump(/*indent=*/2); 80 64 : csg_file.close(); 81 64 : } 82 : }