-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaa_aafspecial.cpp
More file actions
1342 lines (1123 loc) · 33.1 KB
/
aa_aafspecial.cpp
File metadata and controls
1342 lines (1123 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* aa_aafspecial.c -- Special operations
* Copyright (C) 2009 LUH (Leibniz Universitaet Hannover)
*
* This file is part of aaflib.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libaa; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Class: AAF
* This class includes special operations on affine forms
* used for analog circuit simulation with parameter uncertainties
*
* @author Darius Grabowski <darius.grabowski@ims.uni-hannover.de>
* @date 2009-04-01
* @version 1
*
* @see http://www.ims.uni-hannover.de/
*
* (c) 2008-2009
* Institute of Microelectronic Systems (IMS)
* Leibniz Universitaet Hannover
*
* This program comes without any warranty;
* without even the implied warranty of
* merchantability or fitness for a particular purpose.
* Proprietary and confidential.
* Distribution only by express authority of the IMS.
*/
#include "aa.h"
#ifdef USE_QPF
#include "qpf.h"
#endif
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define AAF_THRESHOLD 1E-15
#ifdef USE_AAF_EXTENSIONS
#ifdef USE_QPF
/************************************************************
* Method: =
* Author & Date: Darius Grabowski - 23.08.2006
* Description:
* Affectation operator: AAF = QPF
*
* Input : QPF
* Output : this
************************************************************/
AAF& AAF::operator=(const QPF& P)
{
cvalue = P.getCenter();
if (size) {
delete[] deviations;
delete[] indexes;
}
length = P.getAAFLength();
size = length;
if (size) {
indexes = new unsigned[size];
deviations = new double[size];
P.getAAF(indexes, deviations);
// check last index
if (indexes[length - 1] > last)
last = indexes[length - 1];
#ifdef FAST_RAD
radius = P.rad();
#endif
} else {
deviations = NULL;
indexes = NULL;
#ifdef FAST_RAD
radius = 0.0;
#endif
}
return *this;
}
#endif
/************************************************************
* Method: submul
* Author & Date: Darius Grabowski - 05/2008
* Description:
* Affine substraction with scaling: this = this - d*P
*
* Input : const AAF : AAF to be subtracted
* : double : scaling
* Output : -
************************************************************/
void AAF::submul(const AAF& P, double d)
{
unsigned l1 = length;
unsigned l2 = P.length;
if (l1 + l2 == 0) {
cvalue -= d * P.cvalue;
return;
}
if (l1 == 0) {
double c = cvalue;
*this = -P;
*this *= d;
cvalue += c;
return;
}
if (l2 == 0) {
cvalue -= d * P.cvalue;
return;
}
unsigned* id1 = indexes;
unsigned* id2 = P.indexes;
double* va1 = deviations;
double* va2 = P.deviations;
unsigned* pu1 = id1;
unsigned* pu2 = id2;
unsigned* tempIndexes = NULL;
double* tempDeviations = NULL;
if (l1 + l2)
tempIndexes = new unsigned[l1 + l2]; // the indexes of the result
unsigned* idtemp = tempIndexes;
// Fill the resulting indexes array
// by merging the 2 input indexes array
unsigned* fin = std::set_union(id1, id1 + l1, id2, id2 + l2, idtemp);
unsigned ltemp = fin - idtemp;
if (ltemp)
tempDeviations = new double[ltemp];
double* vatempg = tempDeviations;
// Fill the deviations array
// of the resulting AAF
for (unsigned i = 0; i < ltemp; i++) {
unsigned a = pu1 - id1;
unsigned b = pu2 - id2;
if (a == l1 || id1[a] != idtemp[i]) {
vatempg[i] = -d * va2[b]; // -va2[b]+0
pu2++;
continue;
}
if (b == l2 || id2[b] != idtemp[i]) {
vatempg[i] = va1[a]; // va1[a]-0
pu1++;
continue;
}
vatempg[i] = va1[a] - d * va2[b];
pu1++;
pu2++;
}
// set new properties
length = ltemp;
size = ltemp;
delete[] deviations;
delete[] indexes;
deviations = tempDeviations;
indexes = tempIndexes;
cvalue -= d * P.cvalue;
#ifdef FAST_RAD
radius = 0.0;
for (unsigned i = 0; i < ltemp; i++)
radius += fabs(vatempg[i]);
#endif
return;
}
/************************************************************
* Method: getData
* Author & Date: Darius Grabowski - 05/2005
* Description:
* Get the total data of the AAF
*
* Input : unsigned * : pointer to allocated index array
* double * : pointer to allocated deviation array
* Output : unsigned * : filled index array
* double * : filled deviation array
* unsigned : array length
************************************************************/
unsigned AAF::getData(unsigned* destIndexes, double* destDeviations) const
{
if (length == 0)
return length;
for (unsigned i = 0; i < length; i++) {
destIndexes[i] = indexes[i];
destDeviations[i] = deviations[i];
}
return length;
}
/************************************************************
* Method: get
* Author & Date: Darius Grabowski - 01/2008
* Description:
* get a part of the AAF
*
* Input : double * : pointer to allocated deviation array
* unsigned : number of deviations to be stored
* unsigned : increment
* Output : double * : pointer to allocated deviation array
************************************************************/
void AAF::get(double* data, unsigned n, unsigned npp)
{
if (length + 1 < n)
return;
unsigned index = 0;
#ifdef FAST_RAD
double dev = 0.;
#endif
data[index] = cvalue;
cvalue = 0.;
index += npp;
for (unsigned i = 0; i < n - 1; i++) {
#ifdef FAST_RAD
dev += fabs(deviations[i]);
#endif
data[index] = deviations[i];
deviations[i] = 0.;
index += npp;
}
#ifdef FAST_RAD
radius -= dev;
#endif
return;
}
/************************************************************
* Method: getPD
* Author & Date: Darius Grabowski - 11/2008
* Description:
* get partial deviations of the AAF
*
* Input : double * : pointer to allocated deviation array
* unsigned : first PD to be stored
* unsigned : number of deviations to be stored
* unsigned : increment
* Output : double * : pointer to allocated deviation array
************************************************************/
void AAF::getPD(double* data, unsigned first, unsigned n, unsigned npp)
{
if (length < n + first)
return;
unsigned index = 0;
for (unsigned i = first; i < first + n; i++) {
data[index] = deviations[i];
index += npp;
}
return;
}
/************************************************************
* Method: getAt
* Author & Date: Darius Grabowski - 01/2008
* Description:
* get a part of the AAF
*
* Input : double * : pointer to allocated deviation array
* unsigned : first epsilon to be stored
* unsigned : number of deviations to be stored
* unsigned : increment
* Output : double * : pointer to allocated deviation array
************************************************************/
void AAF::getAt(double* data, unsigned ind, unsigned n, unsigned npp)
{
if (length == 0)
return;
unsigned iter;
unsigned index = 0;
unsigned min = 0;
unsigned max = length - 1;
unsigned mid;
#ifdef FAST_RAD
double rad_ = 0.0;
#endif
// do a binary search for the epsilon given by ind
while (max > min + 1) {
mid = (max + min) / 2;
if (indexes[mid] > ind)
max = mid;
else
min = mid;
}
if (indexes[min] >= ind)
iter = min;
else
iter = max;
for (unsigned i = 0; i < n; i++) {
if (indexes[iter] == ind) {
data[index] = deviations[iter];
#ifdef FAST_RAD
rad_ += fabs(deviations[iter]);
#endif
deviations[iter] = 0.0;
if (iter < length - 1)
iter++;
} else {
data[index] = 0.0;
}
index += npp;
ind++;
}
#ifdef FAST_RAD
radius -= rad_;
#endif
return;
}
/************************************************************
* Method: update
* Author & Date: Darius Grabowski - 01/2008
* Description:
* update a part of the AAF
*
* Input : double * : pointer to allocated deviation array
* unsigned : number of deviations to be stored
* unsigned : increment
* Output : -
************************************************************/
void AAF::update(double* data, unsigned n, unsigned npp)
{
if (length + 1 < n)
return;
#ifdef FAST_RAD
double radOld = 0.0;
double radNew = 0.0;
#endif
unsigned index = 0;
cvalue -= data[index];
index += npp;
for (unsigned i = 0; i < n - 1; i++) {
#ifdef FAST_RAD
radOld += fabs(deviations[i]);
#endif
deviations[i] -= data[index];
#ifdef FAST_RAD
radNew += fabs(deviations[i]);
#endif
index += npp;
}
#ifdef FAST_RAD
radius += radNew - radOld;
#endif
return;
}
/************************************************************
* Method: at
* Author & Date: ??? - ???
* Description:
* Get the deviation symbol at "index"
*
* Input : unsigned : index
* Output : double : deviation symbol at "index"
************************************************************/
#ifdef LINEAR_SEARCH
double AAF::at(unsigned index) const
{
for (unsigned i = 0; i < length; i++) {
if (indexes[i] == index)
return (deviations[i]);
}
return 0.0;
}
#else
double AAF::at(unsigned index) const
{
if (length == 0)
return 0.0;
unsigned min = 0;
unsigned max = length - 1;
unsigned mid;
while (max > min + 1) {
mid = (max + min) / 2;
if (indexes[mid] > index)
max = mid;
else
min = mid;
}
if (indexes[min] == index)
return deviations[min];
if (indexes[max] == index)
return deviations[max];
return 0.0;
}
#endif
/************************************************************
* Method: set
* Author & Date: Darius Grabowski - 05/2005
* Description:
* Set the deviation symbol at "index"
*
* Input : unsigned : index
* double : deviation symbol at "index"
* Output : -
************************************************************/
#ifdef LINEAR_SEARCH
void AAF::set(unsigned index, double value)
{
for (unsigned i = 0; i < length; i++) {
if (indexes[i] == index) {
#ifdef FAST_RAD
radius -= fabs(deviations[i]) - fabs(value);
#endif
deviations[i] = value;
break;
}
}
}
#else
void AAF::set(unsigned index, double value)
{
unsigned min = 0;
unsigned max = length - 1;
unsigned mid;
while (max > min + 1) {
mid = (max + min) / 2;
if (indexes[mid] > index)
max = mid;
else
min = mid;
}
if (indexes[min] == index) {
#ifdef FAST_RAD
radius -= fabs(deviations[min]) - fabs(value);
#endif
deviations[min] = value;
return;
}
if (indexes[max] == index) {
#ifdef FAST_RAD
radius -= fabs(deviations[max]) - fabs(value);
#endif
deviations[max] = value;
return;
}
std::cout << "Warning <AAF::set>: Could not find index " << index << "\n";
}
#endif
/************************************************************
* Method: setLast
* Author & Date: Darius Grabowski - 08/2006
* Description:
* Set the deviation symbol at last index
*
* Input : double : deviation symbol at "index"
* Output : -
************************************************************/
void AAF::setLast(double value)
{
if (length) {
#ifdef FAST_RAD
radius -= fabs(deviations[length - 1]) - fabs(value);
#endif
deviations[length - 1] = value;
}
}
/************************************************************
* Method: sumup
* Author & Date: Darius Grabowski - 07/2005
* Description:
* Sum up all partial deviations with index higher than ind
*
* Input : unsigned int : sumup index
* Output : new highest index
************************************************************/
unsigned AAF::sumup(unsigned ind)
{
if (ind == 0)
return 0;
if (length == 0)
return length;
if (indexes[length - 1] <= ind)
return indexes[length - 1];
double sumUpDeviation = 0.0;
unsigned nIndex;
// sum up deviations until index falls below ind
for (nIndex = length - 1; nIndex >= 0; nIndex--) {
if (indexes[nIndex] <= ind) {
nIndex++;
break;
}
sumUpDeviation += fabs(deviations[nIndex]);
}
if (nIndex == length - 1)
return indexes[length - 1];
length = nIndex + 1;
// replace last deviation
deviations[nIndex] = sumUpDeviation;
// replace last index
indexes[nIndex] = inclast();
return indexes[nIndex];
}
/************************************************************
* Method: sumup
* Author & Date: Darius Grabowski - 08/2005
* Description:
* sums up "small" deviations depending on "level", which is
* scaled by the total deviation (radius)
*
* Input : double : sumup level
* Output : -
************************************************************/
void AAF::sumup(double level)
{
if (!length)
return;
int nSumUpTerms = 0;
int nIndex = 0;
double sumUpDeviation = 0.0;
double dThreshold = fabs(level * rad());
if (dThreshold < AAF_THRESHOLD)
dThreshold = AAF_THRESHOLD;
for (unsigned int i = 0; i < length; i++) {
if (fabs(deviations[i]) >= dThreshold) {
// deviation
deviations[nIndex] = deviations[i];
indexes[nIndex] = indexes[i];
nIndex++;
} else {
sumUpDeviation += fabs(deviations[i]);
nSumUpTerms++;
}
}
if (nSumUpTerms) {
length = nIndex + 1;
indexes[length - 1] = inclast();
deviations[length - 1] = sumUpDeviation;
}
return;
}
/************************************************************
* Method: sumupall
* Author & Date: Michael Kaergel - 09/2009
* Description:
* Sum up all partial deviations
*
* Input :
* Output :
************************************************************/
void AAF::sumupall(void)
{
if (length == 0)
return;
double sumUpDeviation = 0.0;
// sum up deviations until index falls below ind
for (unsigned int nIndex = 0; nIndex < length; nIndex++) {
sumUpDeviation += fabs(deviations[nIndex]);
}
length = 1;
// replace last deviation
deviations[length - 1] = sumUpDeviation;
// replace last index
indexes[length - 1] = inclast();
return;
}
/************************************************************
* Method: compact
* Author & Date: Darius Grabowski - 05/2008
* Description:
* removes zero deviations
*
* Input : -
* Output : -
************************************************************/
void AAF::compact(void)
{
if (!length)
return;
int newLength = 0;
for (unsigned int i = 0; i < length; i++) {
if (deviations[i] != 0.) {
deviations[newLength] = deviations[i];
indexes[newLength] = indexes[i];
newLength++;
}
}
length = newLength;
}
/************************************************************
* Method: resize
* Author & Date: Darius Grabowski - 04/2007
* Description:
* Resizes the AAF by distributing all PDs with index > ind
* on PDs with index <= ind
*
* Input : unsigned int : marginal index
* Output : -
************************************************************/
void AAF::resize(unsigned int ind)
{
double dDeviation = 0.0;
double dScale;
int i;
if (!length)
return;
if ((ind == 0) || (ind > indexes[length - 1]))
return;
// get total deviation of indexes <= ind
for (i = (int)length - 1; i >= 0; i--) {
if (indexes[i] > ind)
dDeviation += fabs(deviations[i]);
else
break;
}
// set new length
length = unsigned(i + 1);
// set scaling factor --> radius remains the same
dScale = rad() / (rad() - dDeviation + AAF_THRESHOLD);
// update deviations
for (i = 0; i < (int)length; i++) {
deviations[i] *= dScale;
}
}
/************************************************************
* Method: ResizeNewSymbol
* Author & Date: Michael Kaergel - 09/2009
* Description:
* Resizes the AAF by putting the Sum of all PDs with
* index > ind on new PD
* This is needed for FFT, else the result is wrong, because
* of wrong correlation
* ! Should be used with extreme care, dirty implementation !!
* ! there is no new Array for deviations and index created !!
* ! so memory is not freed !!
*
* Input : unsigned int : marginal index
* Output : -
************************************************************/
void AAF::ResizeNewSymbol(unsigned int ind)
{
//std::cout << "Starting Resize of AAF" << std::endl;
double dDeviation = 0.0;
unsigned int newLength = 0;
if (!length)
return;
if ((ind == 0) || (ind > indexes[length - 1]))
return;
// get total deviation of indexes <= ind
for (int i = (int)length - 1; i >= 0; i--) {
if (indexes[i] > ind)
dDeviation += fabs(deviations[i]);
else {
newLength = (unsigned int)(i);
break;
}
}
//std::cout << "Done Calculating Sum of Deviations to remove" << std::endl;
// set new length, +1 because of array starting at 0, +1 because of new PD
//length += 2;
//indexes[length - 1] = inclast();
//deviations[length - 1] = dDeviation;
//increase length by one for new Symbol
if (dDeviation != 0)
newLength += 1;
//std::cout << "New Length is: "<< newLength << ", Old Length was: "<< length <<std::endl;
// create new arrays
double* tempDeviations = new double[newLength];
unsigned* tempIndexes = new unsigned[newLength];
// copy deviations and indexes
for (unsigned int i = 0; i < newLength - 1; i++) {
tempDeviations[i] = deviations[i];
tempIndexes[i] = indexes[i];
}
// add new deviation and index
if (dDeviation != 0) {
tempIndexes[newLength - 1] = inclast();
tempDeviations[newLength - 1] = dDeviation;
}
if (length > 1) {
delete[] deviations;
delete[] indexes;
} else if (length == 1) {
delete deviations;
delete indexes;
}
// update AAF data
deviations = tempDeviations;
indexes = tempIndexes;
length = newLength;
//std::cout << "Done Calculating new reduced AAF" << std::endl;
}
/************************************************************
* Method: resize
* Author & Date: Darius Grabowski - 04/2007
* Description:
* Resizes the AAF by distributing all PDs with index > ind
* on PDs with index <= ind
*
* Input : unsigned int : marginal index
* Output : -
************************************************************/
void AAF::resize(unsigned int ind, unsigned int n)
{
double dDeviation = 0.0;
double dScale;
unsigned int i;
if (!length)
return;
if (ind + n > indexes[length - 1])
return;
// get total deviation of indexes <= ind
for (i = ind; i < ind + n; i++) {
dDeviation += fabs(deviations[i]);
}
// set scaling factor --> radius remains the same
dScale = rad() / (rad() - dDeviation + AAF_THRESHOLD);
// update deviations with index smaller than ind
for (i = 0; i < ind; i++) {
deviations[i] *= dScale;
}
// udate deviations with index larger than ind+n
for (i = ind + n; i < length; i++) {
deviations[i - n] = dScale * deviations[i];
indexes[i - n] = indexes[i];
}
// set new length
length -= n;
}
/************************************************************
* Method: addPD
* Author & Date: Darius Grabowski - 04/2007
* Description:
* Adds a new partial deviation to the AAF
*
* Input : double : new partial deviation
* Output : -
************************************************************/
void AAF::addPD(double pd)
{
#ifdef FAST_RAD
radius += fabs(pd);
#endif
if (size > length) {
// array size > array length: just add new deviation
deviations[length] = pd;
indexes[length] = inclast();
length++;
return;
}
// resize the arrays
size = length + 1;
// create new arrays
double* tempDeviations = new double[size];
unsigned* tempIndexes = new unsigned[size];
// copy deviations and indexes
for (unsigned int i = 0; i < length; i++) {
tempDeviations[i] = deviations[i];
tempIndexes[i] = indexes[i];
}
// add new deviation and index
tempIndexes[length] = inclast();
tempDeviations[length] = pd;
if (length > 1) {
delete[] deviations;
delete[] indexes;
}
// update AAF data
deviations = tempDeviations;
indexes = tempIndexes;
length++;
}
/************************************************************
* Method: ID
* Author & Date: Darius Grabowski - 10.04.2007
* Description:
* computes simplified drain current
*
* Input : double : VGS
* double : VDS
* Output : double : ID
************************************************************/
double ID(double VGS, double VDS)
{
if (VDS > VGS)
return (0.5 * VGS * VGS);
else
return ((VGS - 0.5 * VDS) * VDS);
}
/************************************************************
* Method: mos
* Author & Date: Darius Grabowski - 10.04.2007
* Description:
* computes simplified drain current
*
* Input : AAF : VGS
* AAF : VDS
* Output : AAF : ID
************************************************************/
AAF mos(AAF& VGS, AAF& VDS)
{
AAF dV = VDS - VGS;
#ifdef MOSDEBUG
fprintf(stdout, "dv: %f | [%f..%f]\n", dV.getcenter(), dV.getMin(), dV.getMax());
// dV.aafprint();
#endif
if (dV.getMin() > 0.0) {
#ifdef MOSDEBUG
fprintf(stdout, "Saturation\n");
#endif
// saturation
AAF temp = 0.5 * (VGS ^ 2);
return (temp);
}
if (dV.getMax() < 0.0) {
#ifdef MOSDEBUG
fprintf(stdout, "Active region\n");
#endif
// active region
AAF temp = (VGS - 0.5 * VDS) * VDS;
return (temp);
}
//#ifdef MOSDEBUG
fprintf(stdout, "Saturation <--> Active region\n");
//#endif
double VDSmin = VDS.getMin();
double VDSmax = VDS.getMax();
double VGSmin = VGS.getMin();
double VGSmax = VGS.getMax();
double I_VGSmin_VDSmin = ID(VGSmin, VDSmin);
double I_VGSmax_VDSmin = ID(VGSmax, VDSmin);
double I_VGSmin_VDSmax = ID(VGSmin, VDSmax);
double I_VGSmax_VDSmax = ID(VGSmax, VDSmax);
double dIDdVGS = 0.5 * (I_VGSmax_VDSmin - I_VGSmin_VDSmin + I_VGSmax_VDSmax - I_VGSmin_VDSmax) / (VGSmax - VGSmin);
double dIDdVDS = 0.5 * (I_VGSmin_VDSmax - I_VGSmin_VDSmin + I_VGSmax_VDSmax - I_VGSmax_VDSmin) / (VDSmax - VDSmin);
// double dMin = -0.5*dIDdVGS*dIDdVGS - dIDdVDS*VDSmax;
// double dMax = 0.5*(VGSmax*VGSmax + dIDdVDS*dIDdVDS) - (dIDdVGS + dIDdVDS)*VGSmax;
// double dOffs = -0.5*(dMin + dMax);
// double dDelta = 0.5*(dMax - dMin);
#ifdef MOSDEBUG
fprintf(stdout, "dIDdVGS: %f, dIDdVDS: %f\n", dIDdVGS, dIDdVDS);
// fprintf(stdout, "dMin: %f, dMax: %f\n", dMin, dMax);
fprintf(stdout, "offset: %f, delta: %f\n", dOffs, dDelta);
#endif
// compute id_min and id_max at central value
double IDmin = ID(dIDdVGS, VDSmax);
double IDmax = ID(VGSmax, -dIDdVDS + VGSmax);
IDmin += dIDdVGS * (VGS.getcenter() - dIDdVGS) + dIDdVDS * (VDS.getcenter() - VDSmax);
IDmax += dIDdVGS * (VGS.getcenter() - VGSmax) + dIDdVDS * (VDS.getcenter() + dIDdVDS - VGSmax);
double dOffs = 0.5 * (IDmin + IDmax);
double dDelta = 0.5 * (IDmax - IDmin);
#ifdef MOSDEBUG
fprintf(stdout, "Corrected:\n");
fprintf(stdout, "IDmin: %f, IDmax: %f\n", IDmin, IDmax);
fprintf(stdout, "Offset: %f, Delta: %f\n", 0.5 * (IDmin + IDmax), 0.5 * (IDmax - IDmin));
#endif
AAF Temp1(dOffs);
Temp1.length = VGS.length + 1;
Temp1.size = Temp1.length;
Temp1.deviations = new double[Temp1.size];
Temp1.indexes = new unsigned[Temp1.size];
for (unsigned i = 0; i < VGS.length; i++) {
Temp1.indexes[i] = VGS.indexes[i];
Temp1.deviations[i] = dIDdVGS * (VGS.deviations[i]);
}
// Compute the error in a new deviation symbol
// zk = delta
Temp1.indexes[Temp1.length - 1] = Temp1.inclast();
Temp1.deviations[Temp1.length - 1] = dDelta;
AAF Temp2(0.0);
Temp2.length = VDS.length;
Temp2.size = Temp2.length;