Help ! Programming and Math Gurus !

Status
Not open for further replies.
I am working on a project from my university, and I need some ideas on how to approach this problem.
Basically it's a very rough model of a 2-D P-N diode (I shall not bore you with the details)
I'm coding in C and I must first say I'm not a very good programmer and I only know very basic C, but enough to complete this project.
The trick here is to calculate where the depletion layer edges are, and we do that by first assuming where the edges are.

Hence.. :
image-display


and using a certain formula, the program will calculate the individual potential points within the edges, then it will 'move' the edges based on certain rules and 'reshape' it if needed.

For example the actual depletion edges might look like this (But in reality it does not) :

image-display


For simpler models (for example a 1 dimension model) I've been using a 'for' loop to calculate the individual points in the array, with the edges acting as the initial and ending conditions of the 'for' loop.
This is easy as I can move the initial and ending conditions of the 'for' loop up or down but for the more complex model the edges can move in any direction. So my question is how do I change the initial and ending conditions of my 'for' loop based on where it is calculating ?

I know this might sound confusing but please do ask if any there's any part you do not understand.

p/s sorry for the ugly MSPaint pictures.. heh :D

edit : I just realized you might be wondering what the middle line is. It is actually the boundary between the p side and the n side, and is fixed. The upper boundary and lower boundary are the ones that move.
 

Citrus538

Ars Legatus Legionis
12,722
If I understand you correctly, you are currently doing something like:

for (Index = 0; Index <= 127; ++Index)
{ code;
}

You've been changing the 0 and 128 manually and recompiling every time you want to change the boundaries, right?

Just have variables like LowBound and HighBound do do this:

for (Index = LowBound; Index <= HighBound; ++Index)
{ code;
}

Of course, you can ignore all that if I misunderstood your question : )
 
You've been changing the 0 and 128 manually and recompiling every time you want to change the boundaries, right?

Just have variables like LowBound and HighBound do do this:

for (Index = LowBound; Index <= HighBound; ++Index)
{ code;
}

Yes, replacing literal constants is certainly a good idea, but that is not his problem. What you have still only works when the boundary is essentially extended across one dimension. The problem he needs to solve is, how to generalize to the case (pictured) where the boundary can wander arbitrarily in two dimensions.

I have to run to class, but I will try to suggest something when I get back.
 
for (Index = LowBound; Index <= HighBound; ++Index)
{ code;
}

No this wont work unless the model is such ie:

image-display


This is easier as it does not have a vertical boundary line, and I can easily represent the boundaries, where each 'vertical line' has it's own boundary which can be changed. (Multiple 1-D)

Hmmm, how are you representing the data? That's the $64 question.

In other words, how are the "edges" stored in your program, and how do you iterate through them?

I'm representing the whole block of the 'diode' as a 50x150 array. The upper and lower boundaries are represented as a 1-D array ie.
int up[151], down[151];
You can think of this as 151 1-D vertical lines.
I need to somehow have a method of representing the boundaries and using a 'for' loop that is not limited to 1-D.
 
As someone mentioned, it would be helpful if you could post a little more information. But if I undstand correctly, a few ideas come to mind.

First, you could use one two dimensional array to hold a "mask" of where the boundary area lies. So for your pictures, an array might look something like:

1 0 0 1 1 1 1
1 0 0 1 1 1 1
1 0 0 1 1 1 1
1 0 0 1 1 1 1
1 0 0 0 0 0 0
1 0 0 0 0 0 0
1 1 1 1 1 1 1

where a "0" indicates that grid point is inside the current boundary. This mask can then be used to control your calculation. That is, just iterate over the whole thing, only calculating the potential where there is a "0" (inside the boundary). Then you can use this calculation to modify the mask (reshape the boundary), or store the result in a separate array for later use, or both. If you do need the calculated values at each grid point, you could probably be clever and just use one two dimensional array to store the calculations and encode the boundary at the same time.

This idea, as is, is somewhat naive. With very small mesh sizes/large grids you end up with alot of wasted space. On the other hand, something like it should handle any arbitrary configuration.


The other idea that comes to mind is to store only the coordinates of some fixed mesh points lying inside the initial boundary in a two dimensional array. (So the array would contain structures, one for each coordinate) Then iterate over the entire array, calculating as you go using the values of the coordinates at each location. To change the shape of the boundary, you change the coordinates stored in the array.

In the first method, array locations themselves implicity define the geometry. So if the boundary changes, then after the change, more or less points are "in the boundary". In the second method, there is always a fixed number of points "in the boundary", but the geometric location of these points is stored explicity and can be changed.

I'm not an expert on this sort of thing, this is just to give you some ideas. What you are needing to do is generally called Finite Element Modeling, or FEM. I would search around for those terms if you need more detailed information.

There are also surely other ways to do this. Depending on what all you need to do and the assumptions you can make, you could probably store just the edges of the boundary as parametized curves, and then determine if a calculation point lies inside, or maybe break the boundary area up into tight bounding boxes.
 
First, you could use one two dimensional array to hold a "mask" of where the boundary area lies. So for your pictures, an array might look something like:

1 0 0 1 1 1 1
1 0 0 1 1 1 1
1 0 0 1 1 1 1
1 0 0 1 1 1 1
1 0 0 0 0 0 0
1 0 0 0 0 0 0
1 1 1 1 1 1 1

where a "0" indicates that grid point is inside the current boundary. This mask can then be used to control your calculation. That is, just iterate over the whole thing, only calculating the potential where there is a "0" (inside the boundary). Then you can use this calculation to modify the mask (reshape the boundary), or store the result in a separate array for later use, or both. If you do need the calculated values at each grid point, you could probably be clever and just use one two dimensional array to store the calculations and encode the boundary at the same time.

This method isn't efficient as it will need to pass through the whole block looking for 1's and 0's. At this point, the little program that I have already takes around 4 minutes to complete it's calculation on a TBird 900Mhz. The best way is to somehow define the boundaries that are not limited to the 1-D nature of the 'for' loops I am currently using.
After thinking through I could still represent boundaries that curve upwards and also to some extent, the vertical boundaries by having an ever increasing boundary in the vertical direction , (eg. the value of up[151] increases as we go from let's say vertical line 50 till 20 since it curves up from right to left)
However, it will be a real pain to move this boundary left or right as I haven't thought about it yet. This method also has a limitation where it won't be able to represent boundaries of this shape :

image-display


Thanks for the ideas... and if you do have any others please share !
 

puppybane

Wise, Aged Ars Veteran
119
This sounds a little like a regioning problem I remember from one of my classes. You could represent the region as an array of a list of coordinates. Each element in the array would be indexed by the y-coordinate. But then you have a list of coordinates which represent transitions between regions. So with a mask that would look like this:
0011000000
0011000000
0011000000
0011000000
0011000000
0011111111
0011111111

You would have an array like this:
2,4
2,4
2,4
2,4
2,4
2
2

This says that (assuming the y-coordinate increases in the downward direction, starting at 0) the first coordinate which is in the region is 2,0. 4,0 is the next coordinate NOT in the region.

This is not only more compact, in terms of space, but it also allows for easy bounds checking, and the like. Also, it allows for regions of arbitrary shape and size. This is, for any who care, the system that was used for Bill Atkinson's implementation of QuickDraw for the original Macintosh OS.

You might also want to put the bounds on that, so the first one would be: 0,2,4,10 OR -1,2,4,-1.

You could also have another array which was indexed based on x-coordinates
 
This method isn't efficient as it will need to pass through the whole block looking for 1's and 0's

Well, just looking for 1's and 0's is not going to cost you much. On a pipelined, branch predicting architecture, it will probably cost next to nothing, unless your mask is pathological. You would need to have enough memory to hold the entire array (you don't want disk swapping). *That* might be a problem if you have a very small mesh size or large grid. Otherwise, your real hit is in the calculations themselves, and there is no avoiding that.

In any case, pupybane's "regioning" scheme looks very reasonable, especially if the types of boundaries you have posted are typical cases. If they are slightly more complicated (say disconnected or not monotonic) then it could still probably work with some simple generalizations. The first suggestion I would make is that your "regions array" store triples of numbers:

{0, 2, 3},
{1, 2, 3),
{2, 2, 3},
.
.
{5, 2, 9},
{6, 2, 9}

(cf. with the mask puppybane posted) The first number is a row number, the second number is the first column coordinate of a region, the third number is the last column coordinate of a region. (Rather than the first coordinate not in the region). This method has the advantage of treating identically the cases where the end of a region does and does not extend all the way to the end of the grid. (Don't treat things differently unless they are different!) Also, it should handle non-monotonic cases (where, say, the edge of the boundary dips down then comes back up again) with relative ease.

In detail, you have one loop iterating over the regions array. and inside that loop, you have another loop (whose start and finish come from the current region record).

Now, as it stands, you still have to have to allocate a giant array (for the whole grid). The boundary can grow or shrink, and you have to be able to accomodate that. So if your constraint is memory, and you expect that that the number of boundary points is not a significant fraction of total grid points, then the next thing you might consider is to use a hash table to store points (or calculated potentials at boundary points, or whatever) using some functions of the region coordinates as a hash key. You'll still have the O(1) access, but you might not need to allocate nearly a much memory. On the other hand, if you have a huge grid, and you expect that the boundary could grow to take up almost all of the grid, you'll just need a huge hash table in place of a huge array.

The final suggestion, if you want to always have the boundary region represented by a constant (that you decide on) number of points, then you need some adaptive mesh method. But that is outside my realm of knowledge; again look for FEM or Finite Element Modeling.

[This message was edited by BigRedDot on February 22, 2002 at 17:19.]

[This message was edited by BigRedDot on February 22, 2002 at 17:19.]
 

dio82

Ars Legatus Legionis
10,407
Subscriptor
Unless the further above array representation does not work,
you could use the mathematical models behind adaptive grid generation in computational fluid dynamics.
I have to warn you though, this is extremely "meaty" mathematics and the implementation in a program is also not easy. International confereces have been held about this and many PhD research projects have this as thier subject...


Don't start bitching about your computational time. In computational research and modeling one only speaks in terms of days/CPU computation time.

[This message was edited by dio82 on February 22, 2002 at 19:38.]
 

Doomlord_uk

Ars Legatus Legionis
26,376
Subscriptor++
ideas from a non-programmer, non mathematician->

this looked kind of interesting, so I did some thinking... and here's my two cents...

Data representation-

Couldn't you represent your boundaries as a set (dim, array, whatever you call it) of vectors from some fixed line or surface (the n-p divider). Your boundary in your diagrams is a straight line. Extrapolating to 3-d this would be a plane. Surely it would be simple to state vectors from a plane, as integer values of normals to that surface/plane along each axis x,y and z which represented points on the surface of each boundary?

The reason I suggest this is that it seems that the actual Calculation (that you don't describe) is based on a relationship between the boundary and the n-p divider and surrounding (local) boundary points. So a point on the n-boundary might be (p,q,r) where p, q, and r are integer normals from the nearest n-p dividing plane along x, y, and z axes. P,q,r would be +ve for the 'inside' of the divider (by some arbitrary convention). Thus you'd only have a dataset as large as the problem - rather than a huge array or a coordinate shorthand that might struggle with some curves/angles etc. Of course your actual Calculation would have to be able to use these variables.

(I'm curious, but does this Calculation involve vector calculus - div, grad, curl? That's about as far as I got in maths before I quit)

Algorithm-

So, how do you sort through all these vectors and choose which ones you'll perform a given Calculation on? Well, as I'm guessing, you're only operation on a local set of vectors, so why not have a 'sieve' which scans each point on the np-divider set and pulls out the relavent boundary vectors (those nearest, or however the C. needs). In 2d you'd have a line, in 3-d a plane, so you'd need some arbitrary convention on where to start - eg one end or edge. The 'sieve' would pick each np-divider point and find the vectors off it. To make this easy, you would store each vector for each boundary as a list (for each axis/dimension) associating each np-divider point with a mapping back to it's nearest boundary vector vector (so you'd have some work cut out preparing your dataset in the first place... but presumably for 2d at least and for simple 3d you could do this fairly easily. You could use a graphical means to input the data.)

For each iteration of the sieve, you'd pull a set of vectors which you'd then re-parse as necessary for the Calculation.

Repeat.


OK, I'm stopping here. I was trying to break down the process algorythmically rather than mathematicaly or in terms of code which is why my tuppence-worth is written as a process. Even if it's no help, or wrong or daft, at least I had some fun thinking it through. HOpe it might help.

Good night!
 
I think this kind of problem is very specific and has only a few "right" way to solve them. I remember a few courses of CS for my Master's degree a few years ago where I had to solve similar problems. Modeling a physical phenomena with defined equation along some of it's edges. From what I remeber there is no guessing or no "easy to come up with" algorithm. You should get a good book on the subject and find solutions to similar problems. And remeber what most people whitout formal knowledge seem to forget: the way you represent data is as important, if not more, than the algorithm.

GloP :D
 

Cambrensis

Ars Scholae Palatinae
978
This is going slightly over my head, but the corner-detection problem can be solved by convolution, if you can design an appropriate kernel. I only know about this in the context of low-level visual processing, but presumably it generalises quite well.

Try this Google search : http://www.google.com/search?q=convolution+edge+detection it seems quite fruitful.

C.
 
This is going slightly over my head, but the corner-detection problem can be solved by convolution, if you can design an appropriate kernel.

I really don't think this is an edge-detection problem, per se. But it's conceivable that switching to the frequency domain might prove useful.

Ginglith:

The regioning scheme is really very simple. Instead of using the mask of "1"s and "0"s to control where you calculate, you instead use instead store triples of numbers. Imagine breaking the whole area into thin horizontal slices, one grid element high. The triples of numbers record: The row that a slice of the boundary is on, and the starting and ending columns of the boundary region slice. That way, you never have to "look for 1's and 0's". You use this information to only work "inside the region".

If you can throw away the results of your calculations immeadiately, then for many boundary regions, this probably will save you memory. (That is, storing the list of slices - the triples of numbers - requires less memory than storing an entire mask array). On the other hand, if you do need to store the intermediate results of your calculations at each point, then this wil not save you anything in terms of memeory, since you will still have to allocate an array that can hold the entire grid (plus an array for the slices on top of that).

Again, how big is your grid? I just wrote a little program that allocates a 10000 x 10000 element array, initializes it to a pattern similar to yours, and counts all the zeros. The result for all of that (on a system running a big gui, no less) was

real 0m44.416s
user 0m7.910s
sys 0m3.840s

Obviously this is a very rough observation, but suggests that just searching a mask is not so expensive, as long as you have enough memory to hold the entire array at once. Your calculations are what take up time. If you need to store intermediate results then you will need to allocate the array no matter what. Moral: get enough ram to handle the size of problem that you have, or put up with delays from disk swapping. If your problem is 1 million X 1 million, and you need to store results, you are going to have to resign yourself to eating the time cost of swapping, unless you really can afford all that ram.

As someone pointed out earlier, huge calculations (run on supercomputers) are often measured in days of computing time.


I'm sure there are better, less naive ways of doing this (computational fluids FEM methods, maybe some frequency domain gymnastics). But I am also sure they are much much more complicated. You need to investigate whether the payoff is worth the investment of significantly more complex code.

Edit: The only way I can think of that will give you absolute control over the amount of calculation you do is one where you store coordinates of the (fixed number of) points inside the boundary region, and move around the coordinates as necessary in order to change what the boundary region looks like. Imagine stretching out a piece of rubber: the you don't "add new atoms" to change the shape, you just move them around. I think this would fall under teh category of "Adaptive Mesh FEM" I also saw a seminar given by a theoretical chemist who was doing something very similar to this and I think he called it "stenciling". So you search around and look for these things.

The other alternative is not to write the code. Many commercial products are available for FEM type calculations. They probably work better than anything you or I could write, but unfortunately the cost usually reflects that as well.

[This message was edited by BigRedDot on February 26, 2002 at 09:45.]
 
Thanks for the replies guys. I apologise for my lack of updates.
(I'm curious, but does this Calculation involve vector calculus - div, grad, curl? That's about as far as I got in maths before I quit)

Nah the actual calculation is fairly simple. It's just a discretized Poisson's Equation (which consists of a 2nd order differential that's all.. ;) ) For those of you who are interested, it goes like this. The only information we have is the voltage bias and some physical characteristic of the diode. We know the doping concentration, shape and location of the p-n junction etc. However it is not possible to calculate the shape or location of the depletion layer edges (The freaking edges i've been trying to find a way to represent). The diode is subdivided in to mesh points (I use a small 101 by 51) array. We know the voltages of the mesh point OUTSIDE the edges. The individual mesh point voltages between the edges is calculated using poisson's equation, which is done by 'guessing' and improving. (The results converge). This is where it takes up lot's of CPU cycles.

Now the 'real' edge (remember we just guessed where the edges are and solve the potentials within it) is where the electric field is equal to zero. So the program calculates the electric field at the edge and moves it around till it gets the smallest field.

Now for the problem above, the field will be calculated at the edge and the edge must be able to move (but not disjoint) without much restriction.

Previously, when i modeled a planar diode (edges and junction are horizontal straight line) all i had to do was have 2 edge array, up[n] and down[n] which will represent the edge. up[0]=23 will mean upper edge at vertical line 0 is at point 23 and so on. This way I can create a for loop easily. something like:
for(x=up[n]; x<=down[n]; x++)
This is quite limited and so I hope from this you can see the problem i'm facing.
This is going slightly over my head, but the corner-detection problem can be solved by convolution, if you can design an appropriate kernel.
I'm not even going to attempt frequency domain calculation.. haha

I'll have a good long think about your suggested regioning method. As i understand, I'll need an array that can hold 3 variables ? One to represent the individual horizontal line and 2 more to represent starting and ending boundary ?
It seems possible but can a boundary from one horizontal line 'move' to another horizontal line ? ie go south westerly, north westerly etc. I think it should be able to.. i'll need to experiment more.

Once again thanks guys..
that's my longest post ! I hope you read it through !!! :D
 
diode is subdivided in to mesh points (I use a small 101 by 51) array.

Is this typical for your real problem? Do you need it to scale to much larger grids? If not, then I unequivocally support the simple mask method. The testing for "1"s and "0"s with that order of data points costs you next to nothing, and the method is conceptually simpler. Again, in this case, the calculation is where all the time is spent. Consider it this way, if it takes you week to code up the complicated algorithm and ten hours to run the numerics with it but would've only taken you a day to write the simple algorithm and 15 hours to run its numerics, then you've lost.
 

bugbum1

Ars Scholae Palatinae
893
the program will calculate the individual potential points within the edges, then it will 'move' the edges based on certain rules and 'reshape' it if needed.

i have no idea what the 'rules to reshape' are like... but if they are neighbor based, than you can use the optimization that conway used to speed up the old game of life. basically, you keep the points in a linked list, and then update the region by traversing the list to do the nearest neighbor calculation (eliminating empty grid points, and adding new points to the list, removing old so you only look at information, not space). this optimization is easy and works for certain rule sets - i don't know if your rule set will allow this approach?
 
Thanks for the idea. I think I can easily implement the 'mask' but I do have one problem which I can't figure out how to go around it.
How do I 'detect' the edge ? As I said earlier, the 'rules' for moving the edges depends on the electric field at the edge. So how do I program it just to calculate the field where the 'mask' boundaries where there's a change of 0 to 1 (or vice versa)
 

technophile

Ars Legatus Legionis
23,768
Originally posted by Ginglith:
Thanks for the idea. I think I can easily implement the 'mask' but I do have one problem which I can't figure out how to go around it.
How do I 'detect' the edge ? As I said earlier, the 'rules' for moving the edges depends on the electric field at the edge. So how do I program it just to calculate the field where the 'mask' boundaries where there's a change of 0 to 1 (or vice versa)
Check neighbors. If your current location is a 0, check each of the 8 (5 if you're on an edge of the whole array, 3 if you're in a corner) neighbor cells for 1s; if you're on a 1, check for 0s. That will tell you if there is any kind of an edge nearby.
 
Status
Not open for further replies.