Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 : // STL includes 11 : #include <fstream> 12 : 13 : // MOOSE includes 14 : #include "CSVReaderVectorPostprocessor.h" 15 : #include "MooseUtils.h" 16 : 17 : registerMooseObject("MooseApp", CSVReaderVectorPostprocessor); 18 : registerMooseObjectRenamed("MooseApp", CSVReader, "06/30/2024 24:00", CSVReaderVectorPostprocessor); 19 : 20 : InputParameters 21 28926 : CSVReaderVectorPostprocessor::validParams() 22 : { 23 28926 : InputParameters params = GeneralVectorPostprocessor::validParams(); 24 28926 : params.addClassDescription( 25 : "Converts columns of a CSV file into vectors of a VectorPostprocessor."); 26 28926 : params.addRequiredParam<FileName>("csv_file", 27 : "The name of the CSV file to read. Currently, with " 28 : "the exception of the header row, only numeric " 29 : "values are supported."); 30 28926 : params.addParam<bool>("header", 31 : "When true it is assumed that the first row contains column headers, these " 32 : "headers are used as the VectorPostprocessor vector names. If false the " 33 : "file is assumed to contain only numbers and the vectors are named " 34 : "automatically based on the column number (e.g., 'column_0000', " 35 : "'column_0001'). If not supplied the reader attempts to auto detect the " 36 : "headers."); 37 28926 : params.addParam<std::string>("delimiter", 38 : "The column delimiter. Despite the name this can read files " 39 : "separated by delimiter other than a comma. If this options is " 40 : "omitted it will read comma or space separated files."); 41 86778 : params.addParam<bool>( 42 57852 : "ignore_empty_lines", true, "When true new empty lines in the file are ignored."); 43 28926 : params.set<bool>("contains_complete_history") = true; 44 28926 : params.suppressParameter<bool>("contains_complete_history"); 45 28926 : params.set<ExecFlagEnum>("execute_on", true) = EXEC_NONE; 46 28926 : params.suppressParameter<ExecFlagEnum>("execute_on"); 47 : 48 : // The value from this VPP is naturally already on every processor 49 : // TODO: Make this not the case! See #11415 50 28926 : params.set<bool>("_auto_broadcast") = false; 51 : 52 28926 : return params; 53 0 : } 54 : 55 198 : CSVReaderVectorPostprocessor::CSVReaderVectorPostprocessor(const InputParameters & params) 56 198 : : GeneralVectorPostprocessor(params) 57 : { 58 : /// The MOOSE delimited file reader. 59 198 : MooseUtils::DelimitedFileReader csv_reader(getParam<FileName>("csv_file"), &_communicator); 60 198 : csv_reader.setIgnoreEmptyLines(getParam<bool>("ignore_empty_lines")); 61 198 : if (isParamValid("header")) 62 34 : csv_reader.setHeaderFlag(getParam<bool>("header") 63 : ? MooseUtils::DelimitedFileReader::HeaderFlag::ON 64 : : MooseUtils::DelimitedFileReader::HeaderFlag::OFF); 65 198 : if (isParamValid("delimiter")) 66 0 : csv_reader.setDelimiter(getParam<std::string>("delimiter")); 67 : 68 198 : csv_reader.read(); 69 198 : const std::vector<std::string> & names = csv_reader.getNames(); 70 198 : const std::vector<std::vector<double>> & data = csv_reader.getData(); 71 1052 : for (std::size_t i = 0; i < data.size(); ++i) 72 : { 73 854 : _column_data[names[i]] = &declareVector(names[i]); 74 854 : _column_data[names[i]]->assign(data[i].begin(), data[i].end()); 75 : } 76 198 : }