How would I get the number of stress periods in a MODFLOW 6 simulation? - flopy

I can read in an existing MODFLOW 6 simulation using flopy.mf6.MFSimulation.load. Now I want to find out how many stress periods it has, as an integer, as defined by nper in the tdis package. What's the easiest way to do this?

So here is the trick, in flopy classes for MODFLOW 6, all of the information is stored as objects, including integers, arrays, floats, etc. This gives us some nice advantages, but it also makes some of the syntax a little difficult, though we are working to improve that.
Here is a very simple model:
import flopy
sim = flopy.mf6.MFSimulation()
tdis = flopy.mf6.ModflowTdis(sim, nper=10)
gwf = flopy.mf6.ModflowGwf(sim)
dis = flopy.mf6.ModflowGwfdis(gwf)
If we try to get at nper like this:
nper = tdis.nper
print(nper)
then we get back the repr, which looks like this:
{internal}
(10)
The way we get the actual data is to append array:
nper = tdis.nper.array
print(nper)
print(type(nper))
In which case we get the desired information:
10
<class 'int'>
For scalars we are considering changing this behavior so that it behaves like you would think (returning the value directly), but we haven't implemented that yet.

Related

Is there any difference between using/not using "astype(np.float)" for narray?

I'm going to import the txt file which contains Numbers only, for some coding practice.
Noticed that i can get the same result with either code_1 or code_2:
code_1 = np.array(pd.read_csv('e:/data.txt', sep='\t', header=None)).astype(np.float)
code_2 = np.array(pd.read_csv('e:/data.txt', sep='\t', header=None))
So I wonder if there is any difference between using or not using .astype(np.float)?
please tell me if there is an similar question. thx a lot.
DataFrame.astype() method is used to cast a pandas object to a specified dtype. astype() function also provides the capability to convert any suitable existing column to categorical type.
The DataFrame.astype() function comes very handy when we want to case a particular column data type to another data type.
In your case, the file is loaded as a DataFrame. The numbers will be loaded as integers or floats depending on the numbers. The astype(np.float) method converts the numbers to floats. On the other hand if the numbers are already of float type, then as you saw there will not be any difference between the two.

Python DEAP: Fitness seems to go down over time - how do I fix this?

My individual is a concatenation of the five parameters needed to tweak a multi-stage process. The first population is created by randomly assigning those parameters a value within their respective ranges and converting that into a binary string. A graph of the first 25 generations typically looks like this:
The DEAP code looks like this:
# Create the individuals
creator.create('FitnessMax', base.Fitness, weights=(1.0, ))
creator.create('Individual', list, fitness=creator.FitnessMax)
# Initialize the containers
toolbox = base.Toolbox()
toolbox.register('individual', get_random_contour_ind)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)
# Initialize the operators
toolbox.register('evaluate', get_total_max_ratio)
toolbox.register('mate', tools.cxUniform, indpb=0.10)
toolbox.register('mutate', tools.mutFlipBit, indpb=0.10)
toolbox.register('select', tools.selBest)
It takes a long time to run, but I've run the same DEAP code on other evaluators and am noticing the same pattern. How do I fix this?
The problem was that the evaluator is non-deterministic, so I took paddymccrudden's advice and added a decorator to enforce stochastic fitness. Now the fitness graph looks like this:
As you can see the average fitness is increasing over the generations.

Apriori Algorithm for text

I have taken a data mining course and we have to run an apriori algorithm on a data set with text , ie strings.
['acornSquash', 'cottageCheese', 'laundryDetergent', 'oatmeal', 'onions', 'pizza', 'tomatoes', 'yogurt']
['bread', 'cinnamon', 'grapefruit', 'juiceBoxes', 'mayo', 'pastaSauce', 'pepper', 'waterBottles', 'yogurt']
Can i get any code or help to run the apriori algorithm?
Thanks in advance
Below link contains source code for basic apriori implementation.
https://github.com/ak94/Apriori/
Go through readme file.
By basic implementation I mean to say , it do not implement any efficient algorithm like Hash-based technique , partitioning technique , sampling , transaction reduction or dynamic itemset counting.
The code scans the whole dataset every time.But it is memory efficient as it always read input from file rather than storing in memory.
As you are currently on this course I assume this code will be the first you will like to write own your own.
To read more about apriori algorithm I would recommend you to read http://www3.cs.stonybrook.edu/~cse634/lecture_notes/07apriori.pdf
Read , understand and try to implement on your own.
Now,lets talk about how to implement .
When you go through the code from link I posted , It implements on numbers i.e., its input file contains itemset as number instead of text (as in your case)
what you can simply do is , write a program to map each text with a particular number.
For e.g.
Suppose your data set contained
[ 'oatmeal', 'onions', 'pizza', 'tomatoes', 'yogurt']
[ 'tomatoes', 'pepper', 'waterBottles', 'yogurt']
So it would look like
1 2 3 4 5 -1
4 6 7 5 -1
(-1 to represent end of particular transaction ,as in code )
then you use this input file for your code (either same as in link or your own in different language)
and after you get the frequent item set after execution of program , you can transform it back using map you used earlier.

Modeling z on a real in system verilog

I am making a very simple model of a an output driver which can have 3 output levels. It can also be turned off. What I want to do is model this as a real so I can assign different numbers for the three levels. I am unsure to do to model the 'off' state. I found some discussion about using inf or NaN to model z in a real number, but it doesn't seem like this is a settled argument. So this is a 2 part question:
1) is NaN the right way to model z on a real in system verilog?
2) how do I assign NaN in system verilog? This works:
realnum = 0.0/0.0;
But it seems like a bit of a kludge to do this. Is there already a defined way to get NaN?
The right way to do what I want is probably to use verilog-AMS but I want to avoid using a license for a simulator that supports it for such a simple model.
Edit: To clarify why I want to do this: this model will be a cell view in the top level schematic that I will netlist. I can't do a 2 bit output since the schematic has only one wire.
A new feature 6.6.7 User-defined nettypes was added to the 1800-2012 LRM, but I do not think it has been implemented by most vendors yet. In the meantime, you can create your own resolution functions, and manually insert them where needed to convert your real to a 4-state value.

Vectorize string concatenation in matlab

I am doing a project where I would like to vectorize (if possible), these lines of code in Matlab:
for j=1:length(image_feature(i,:))
string1b=strcat(num2str(j),':',num2str(image_feature(i,j)));
write_file1b=[write_file1b string1b ' '];
end
Basically, what I want to get is an output string in the following way:
1:Value1 2:Value2 3:Value3 ....
Note that ValueX is a number so a real example would be an output like this:
1:23.2 2:34.3 3:110.8
Is this possible? I was thinking about doing something like creating another vector with values from 1 to j, and another j-long vector with only ":", and a num2str(image_feature(i,:)), and then hopefully there is a function f (like a vectorized strcat) that if I do:
f(num2str(1:j),colon_vector,num2str(image_feature(i,:)))
will give me the output I mention above.
Im not sure I understood your question but perhaps this might help
val=[23.2 34.3 110.8]
output = [1:length(val); val]
sprintf('%i: %f ',output)
As output I get
1: 23.200000 2: 34.300000 3: 110.800000
You can vectorize all your array operations to create an array or matrix of numbers very efficiently, but by the very nature of strings in MATLAB, you cannot vectorize the creation of your output string. Theoretically, if you have a fixed length string like in C++, you could concurrently write to different memory locations within the string, but that is not something that is supported by MATLAB. Even if it were, it looks like you have variable-length numbers, so even that would be difficult (unless you were to allocate a specific amount of space per number pair, leading to variable-length spaces between number pairs. It doesn't look like you want to do that, since your examples have exactly one space between all number pairs).
If you'd be interested in efficiently creating a vector, the answer provided by twerdster would accomplish that, but even in that code, the sprintf statement is not concurrent. His code does get rid of the for-loop, which improves efficiency, so I prefer his code to yours.

Resources