To start, I'm new to Julia and I'm trying things out to test Julia out for
some scientific/engineering applications. In particular, I'm working on
moderate size projects, where they're big enough that I'll need more than
one file with code in it to stay organized (let's say anywhere from 5-50
files, 10s-100s of functions. However, I'm struggling to figure out an
appropriate way to organize my code and ensure that the proper variables
are in scope where they are needed.
To start, I come from a Matlab/C#/C++/Fortran/Basic/Pascal/etc. background
and have been coding for long time, so I'm a little baffled by Julia's
structure.
Here's the basic description of my problem. I've got a collection of
(sometimes large) 1D arrays. I'll define some starting values for these
arrays and then I simply iterate on them and update the values in the
arrays (basically I'm solving unsteady PDE problems). A very simple
program structure would look something like this (thinking in terms of a
functional programming approach in Matlab/C/Fortran,):
--------------------------------------------------------------------------------------------
module MyProjectModule
# Include some files that have functions I need
include("SetSomeProblemParameters.jl");
include("DeclareASetOfArrays.jl");
include("AssignInitialValuesToArrays.jl");
include("CheckSomeValuesInSomeArrays.jl");
include("CalculateSomeValuesBasedUponArrayValues.jl");
include("UpdateValuesOfArrays.jl");
include("WriteResultsToDisk.jl");
# Make main() visible
export main
main(NumberOfIterations) # This is the main entry point of the code that
performs a lengthy numerical calculation
SetSomeProblemParameters()
DeclareASetOfArrays()
AssignInitialValuesToArrays()
while (NumberOfIterations not reached)
CheckSomeValuesInSomeArrays()
CalculateSomeValuesBasedUponArrayValues()
UpdateValuesOfArrays()
NumberOfIterations++
end #while
WriteResultsToDisk()
end #main
end MyProjectModule
--------------------------------------------------------------------------------------------
Simple, right?
Question #1: If main entry point to run a calculation "main()" is a
function, it gets its own variable workspace, right? Now, if I write a
script (not a function) and use include("some_script.jl") with main(), does
Julia just inline that code within main()? In terms of scope, should the
script file be able to see all of the variables in the scope of main()? In
Matlab that would be true. In Fortran/C that wouldn't. I guess, I'm not
sure what scope implications there are for Julia script files.
Question #2: If I've defined a bunch of functions as shown in the
pseudocode above, what is the most performant way to have the large 1D
arrays accessible within the scope of each function. As you can tell, I'm
trying to avoid writing functions that accept a long list of input
parameters. The old Fortran solution is to simply make the arrays global,
so that each function can access them as needed. How terrible is that idea
within the Julia framework? Also, how can I even do that? I've tried
writing a script (not a function) to declare a long list of global
variables and then used include("DeclareGlobalVariables,jl) within my main.
But, when I return to main(), those variables do not show up in the
workspace for main??? What am I missing?
Question #3: I come from a VisualStudio IDE background, so I'm having
trouble figuring out how to organize a Juila project. I'm trying out Atom
for my first Julia tests. For a project that's bigger than just a script
or a few functions, should I be defining a defining main entry point
function within a module? Why Does Julia force modules to be added as
packages so they can be loaded with the "using" command? That seems
strange. Or, should I just write everything as a collection of files with
functions in them and not worry about modules? Simple REPL and one file
Julia examples are everywhere. There are also large coding
projects/libraries/utilities on github as examples, but I'm having trouble
figuring out the structure of these larger projects. I guess, I'm
somewhere in between these two cases, where I'm just want to crunch some
numbers, but I'm a little more complicated/sophisticated than the single
file examples. What's the best way to proceed with such a project/file
structure?
Thanks in advance for any help.
Nick