|
[Sponsors] |
Suggestion on writing read-input function/script/subroutine with many variables |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
October 25, 2019, 21:56 |
Suggestion on writing read-input function/script/subroutine with many variables
|
#1 |
Senior Member
|
Hi,
I am trying to optimize a MATLAB code which solves multiple 1D governing equations. As you guys may know, CFD codes generally have many many inputs/options/variables. For now I am just using a simple script, directly using the variables in the workspace and assign them to some structs, and then pass to functions. However, in this way, the script itself seems pretty messy and lengthy, not quite compact and elegant. Directly input like 30 variables into a function also doesn't look like a brilliant choice. I've been looking for alternative ways of reading/parsing multiple inputs, but I can't see any other smart ways. Maybe OOP? Do you guys have some suggestions in general? Any rule of thumb you guys follow when writing your own CFD codes? Thanks ahead! |
|
October 26, 2019, 05:41 |
|
#2 |
Senior Member
|
In general CFD you might have a GUI that sends your manual input to the real solver, probably using a client/server model with sockets or over MPI, or just plain files to be filled and used as input (the GUI itself might actually write such input files and inform the solver of them).
I don't think a GUI is what you are looking for, so I guess the input files might be the best route. Your script will read the input from a separate file instead of having it embedded (as typical of small matlab proof of concept scripts). You are free to design them however you want. Something simple that usually works is the following pattern: keyword_a value1 value2 ... keyword_b value etc. But there are cases where some more structure and flexibility is needed. In this case I suggest looking at the xml format (MATLAB has a reader). It is obviously more verbose and difficult to read, but only to a certain point. Probably an overkill for a 1D code, but worth learning it for the future. Of course, the way you store the information from the file and how you pass it around in the code is a totally different story. For a given kind of application, as soon as your code tries to achieve some sort of generality, a pattern will emerge that will make you think: "wow, if I could do this, I would write much less code in much less time and testing would also be much simpler". Then you try to achieve just that. But it depends from the overall pattern in the code. There is no recipe. For what concerns OOP, it also depends. There is a false myth that you can't write good code if you don't use OOP. In my opinion, algorithms will always have home in procedural programming. But, probably, you shouldn't be too much concerned about OOP in MATLAB. |
|
October 26, 2019, 19:26 |
|
#3 | |
Member
Mianzhi Wang
Join Date: Jan 2015
Location: Columbus, IN
Posts: 34
Rep Power: 11 |
Hi, TurbJet,
It feels that Matlab is limiting your development of software craftsmanship. There was a time that I write everything in Scilab and Matlab. Until Fortran opened me a door to a whole new world. Maybe you can try something like C/Fortran. They may give you a new opinion on programming and numerical analysis. As to the input, I have two personal preferences: 1. keep it pure text; 2. use un-formatted read/write. (I cannot imagine how tough my life would be without awk and sed) Have fun hacking! Mianzhi Quote:
|
||
October 26, 2019, 19:31 |
|
#4 | |
Senior Member
|
Quote:
The reason why I am asking how to deal with input is solely for an exercise purpose, and I'd like to make the code looks nicer and organized, just in case that in the future I have to pass it down to some other students in my group. |
||
October 26, 2019, 19:34 |
|
#5 | |
Senior Member
|
Quote:
|
||
Tags |
coding skills.., coding style, read fields |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Read properties per component from an input file dictionary | Cyp | OpenFOAM Programming & Development | 29 | November 1, 2015 22:41 |
External Input Data read as Boundary Conditions | uwsa | CFX | 2 | August 12, 2010 04:34 |
read user input value for post-processing utility | Pascal_doran | OpenFOAM Post-Processing | 2 | June 11, 2010 13:03 |
Phase locked average in run time | panara | OpenFOAM | 2 | February 20, 2008 15:37 |
PHI file structure | Eugene | Phoenics | 9 | November 2, 2001 23:00 |