-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.f90old
1153 lines (868 loc) · 35.4 KB
/
main.f90old
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
program main
use dimpce
implicit none
INCLUDE "collsub.h"
include 'mpif.h'
integer :: DIM
parameter (DIM=2)
!indices
integer :: i,j,k,ii,jj,kk,fuct
!Monte Carlo
integer::nmcs,readMCsamples
!flags
integer :: stat,makesamples,solver,ierr
!PC vitals
integer::DIMPC,numpc,npts,nterms
real*8 :: coll(MAXPTS,DIM),par(DIM,MAXPAR)
real*8 :: RN(DIM,MAXPTS),DS(2,DIM)
real*8 :: fpcb(MAXPTS),gpcb(DIM,MAXPTS),xcof(MAXTRM),hpcb(dim,dim,maxpts)
integer :: ipar(MAXVAR)
!RandomNumber
integer:: seed
!LU Decomposition
integer :: indx(MAXDAT)
real*8 :: W(MAXDAT),V(MAXDAT,MAXDAT),rhsF(MAXTRM),z(MAXDAT),tmp(MAXTRM)
!Orthogonal Polynomial related
integer::mreg(MAXDAT,DIM)
real*8 :: PL(DIM,0:MAXTRM),DPL(DIM,0:MAXTRM),xmat(MAXDAT,MAXDAT)
!Name related declarations
character*60 :: filename
character*2 :: dimnumber,fctnumber
! Cost func evals
integer::fct
real*8:: fv,gv(DIM),hv(DIM,DIM)
real*8 :: x(DIM)
!!Dynamic samples
real*8::xvec(maxdat,dim)
integer::Randomini,maxorderwant
integer::index,evalfunction
integer::nptsold,ntermsold
integer:: nptstoaddpercyc
integer::ctest
call MPI_START
mainprog=.true.
ndimt=dim ! If all are assumed to be aleatory variables
if (dim.ne.ndimt) OUUflag=1 ! mixed uncertainties, need to call optimization at the end
!Settings
filenum=6 ! 6 for screen, any other number for fort.x
if(id_proc.eq.0) then
write(filenum,*)
write(filenum,*) '======================================================='
write(filenum,*) 'Non-Intrusive Polynomial Chaos (Stochastic Collocation)'
write(filenum,*) '======================================================='
write(filenum,*) 'Author: Komahan Boopathy, University of Dayton, OH'
write(filenum,*) 'Email : komahan.cool@gmail.com'
write(filenum,*) '======================================================='
write(filenum,*)
! write(filenum,'(x,a,i3)')'>> Number of Processors = ',num_proc
end if !master
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! Non-parallel region !
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
!============================================================
! Initial settings
!============================================================
makesamples=1 ! 0=read, 1= Make via LHS for building surrogate
! Choice of orthogonal basis
! 1=Legendre
! 2=Hermite
do j=1,DIM
ipar(j)=1
end do
casemode=0 !0=RMSE only, 1=Stats+RMSE
if (casemode.eq.1) then
! This file is read again in montecarlo subroutine. Here it is needed to define the domain size when doing casemode=1(Stats)
open(10,file='MC.inp',form='formatted',status='old')
read(10,*) (xavg(i),i=1,dim)
read(10,*) (xstd(i),i=1,dim)
read(10,*)
read(10,*)
read(10,*) NMCS!,ndimtmp
read(10,*) !npdf
read(10,*) readMCsamples
read(10,*) evlfnc
close(10)
end if
!!$ ! Mean setup
!!$ do i =1,dim
!!$ xavg(i)=1.0d0
!!$ end do
probtype(:)=1
do i=1,ndimt
if (probtype(i).eq.1) then
xstdt(i)=xstd(i)
else if (probtype(i).eq.2) then
xstdt(i)=xavg(i)*xstd(i)
else
stop"Wrong problem type"
end if
end do
xavg(1:DIM)=xavgt(ndimt-DIM+1:ndimt)
xstd(1:DIM)=xstdt(ndimt-DIM+1:ndimt)
! do dynamics=1,1
do ctest=1,3
if (ctest.eq.1) dynamics=0
if (ctest.eq.2) then
dynamics=1
lhsdyn=.true.
end if
if (ctest.eq.3) then
dynamics=1
lhsdyn=.false. !mirdyn is true
end if
!===============================
! Main Program
!===============================
DO OS=2,2 ! Ratio of Over Sampling ratio 1 or 2 (2 is recommended)
do stat=0,2
!0= Function only
!1= Function + Gradient
!2= Function +Gradient +Hessian
call solvertype(stat,os,solver)
fctindx=0
do fct=1,1,1
!!$ if (fuct.eq.1) fct=4
!!$ if (fuct.eq.2) fct =2
!!$ if (fuct.eq.3) fct =6
!!$ if (fuct.eq.4) fct =10
!1 : cos(x+y) (Nd)
!2 : 1.0/(1.0+x**2+y**2) (Nd)
!3 : x**2+y**2 (Nd)
!4 : exp(x+y) (Nd)
!5 : x**3+y**3 (Nd)
!6 : Rosenbrock (Nd)
!7 : sin(3x-1.5)+cos(3y-3) (Nd)
!8 : Two bar truss design (3d)
!9 : Short tubular column (3d)
!10: Cantilever beam (2d)
!11: Three bar truss (3d)
!12: Threebar truss (6d)
!20: CFD
!>20: Mixed Uncertainties, calling suboptimization program to find the worst and best case scenarios to fix the corresponsing epistemic vars at extrema, whereas the aleatory vars are sampled within the space spanned by the mean and 3*SD. The surrogate is built on aleatory vars only, with epistemic vars fixed at extrema. F(B*,A_i) is what is given to the surrogate for each corresponding training point location A_i.
!Domain size
if (casemode.eq.0) then !RMSE comparisons only
if (fct.lt.7)then ! Analytic functions
DO j = 1, DIM
par(j,1)=-2.0
par(j,2)=2.0
END DO
else if (Fct.eq.8) then ! Truss design
if (fctindx.gt.3.or.dim.ne.3) stop'Wrong Function Index or Dimension for this Truss test case. Please check'
!0=obj, 1,2,3= constraints
DO j = 1, DIM
par(j,1)=1.0d0
par(j,2)=3.0d0
end do
else if (Fct.eq.9) then ! Short Column
if (fctindx.gt.2.or.dim.ne.3) stop'Wrong Function Index or Dimension for this Truss test case. Please check'
!0=obj, 1,2,3= constraints
DO j = 1, DIM ! Figure out a best range
par(j,1)=1.0d0
par(j,2)=3.0d0
end do
else if (Fct.eq.8) then ! Cantilever beam problem
if (fctindx.gt.3.or.dim.ne.3) stop'Wrong Function Index or Dimension for this Truss test case. Please check'
!0=obj, 1,2,3= constraints
DO j = 1, DIM
par(j,1)=1.0d0
par(j,2)=3.0d0
end do
else if (fct.eq.20) then
call cfdparams(par) !? on all threads?
end if
else if (Casemode.eq.1) then ! stats + rmse domain
! statistics--> construct PC surrogate between mean and 3 SD's
do i=1,dim
par(i,1)=xavg(i)-3.0*xstd(i)
par(i,2)=xavg(i)+3.0*xstd(i)
end do
else
print *, casemode
stop 'Wrong Casemode'
end if
dyncyccnt=0
do DIMPC =2,10 !order 5D requires 3003 terms
dyncyccnt=dyncyccnt+1
! Get number of terms in the expansion
call combination(DIM+DIMPC,DIM,nterms)
! Get number of points based on stat,solver,oversamp ratios
call getnpts(solver,stat,dim,nterms,OS,npts)
if (dyncyccnt.eq.1) then
if(id_proc.eq.0) call sampdist(stat,DIM,DIMPC,ipar,par,makesamples,nterms,npts,fct,RN)
else
if (dynamics.eq.1) then
nptstoaddpercyc=abs(npts-nptsold)
! Trick to force a least number of points
!!$ if (nptstoaddpercyc.le.dim) then
!!$ npts=npts+dim
!!$ nptstoaddpercyc=abs(npts-nptsold)
!!$ end if
! Call the dynamic sample routine
if (id_proc.eq.0) then
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' Dynamic Point Selection '
write(filenum,*) '================================================='
write(filenum,*)
write(filenum,*)' >> NPTS to add this cycle:',nptstoaddpercyc
end if
call dynsampdist(stat,DIM,DIMPC,ipar,par,makesamples,ntermsold,nterms,nptsold,npts,nptstoaddpercyc,fct,fpcb,gpcb,hpcb,xcof,RN)
else ! random points again call the same routine
if(id_proc.eq.0) call sampdist(stat,DIM,DIMPC,ipar,par,makesamples,nterms,npts,fct,RN)
end if !dynamics
end if ! dyncyccnt
call MPI_Barrier(MPI_COMM_WORLD,ierr) ! All wait until master gets here
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! Non-parallel region !
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
if (id_proc.eq.0) then
!==================================================
! SETUP MATRIX
!==================================================
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' Matrix Setup & Pseudo-Inverse '
write(filenum,*) '================================================='
write(filenum,*)
call setupmat(stat,dim,dimpc,npts,ipar,RN,numpc,xmat)
write(filenum,*)' >> Number of columns in the matrix (nterms):',nterms
write(filenum,*)' >> Number of rows in the matrix (numpc):',numpc
if (solver.eq.1)then
write(filenum,*)' >> Solver : LU Decomposition'
else if (solver.eq.2) then
write(filenum,*)' >> Solver : SV Decomposition'
else
stop'Invalid Solver'
end if
!==============================================================
! Decomposing the matrix to enable to use it for multiple RHS's
!==============================================================
write(filenum,*)' >> Finding Pseudo-Inverse of the matrix . . . '
call pseudoinv(solver,numpc,nterms,xmat,indx,W,V)
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' RHS Setup '
write(filenum,*) '================================================='
write(filenum,*)
! Evaluates cost and grad and stores them as arrays fpcb(maxpts),gpcb(dim,maxpts)
if (dynamics.eq.0.or.dyncyccnt.eq.1) then
call gather_costfn(dim,fct,stat,npts,RN,fpcb,gpcb,hpcb)
! for further dyncyccnts they are automatically computed within dynsamp routine
end if
!=======================================================
! Setting up RHS
!=======================================================
write(filenum,*)'>> Cost Function Number :',fct
if (stat.eq.0) write(filenum,*)' >> Setting up the RHS with Function evals . . .'
if (stat.eq.1) write(filenum,*)' >> Setting up the RHS with Func+Grad evals . . .'
if (stat.eq.2) write(filenum,*)' >> Setting up the RHS with Func+Grad+Hess evals . . .'
! Put fpcb,gpcb,hpcb all into one rhsF
call setupRHS(stat,dim,npts,fpcb,gpcb,hpcb,numpc,rhsF)
write(filenum,*)' >> Number of RHS entries :',numpc ! Atleast equal to number of coeffients
write(filenum,*)
do i=1,numpc
write(filenum,*)i, rhsF(i)
end do
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' Solve Linear System '
write(filenum,*) '================================================='
write(filenum,*)
!=======================================================
! Backsubstitute for any RHS (like a black box approach)
!=======================================================
write(filenum,*)'>> Solving for the coefficients . . . '
call backsub(solver,xmat,numpc,nterms,indx,W,V,rhsF,XCOF)
write(filenum,*)'>> Coeffs are succesfully determined . . .'
write(filenum,*)'>> Number of coefficients : ',nterms
write(filenum,*)
end if !master
!-------------------------- Info Sharing -------------------------------!
call MPI_Barrier(MPI_COMM_WORLD,ierr) ! All wait until master gets the coeffs
call MPI_BCAST(npts,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(numpc,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(nterms,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
call MPI_BCAST(xcof,maxtrm,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
!-------------------------- End Sharing -------------------------------!
! if (id_proc.eq.0) call TimerStop('PC')
! if (id_proc.eq.0) call TimerReport(ttotal)
!++++++++++++++++
!Parallel Region!
!++++++++++++++++
!================================================================
! Post processing--> RMSE, Output statistics,Tecplot
!================================================================
if (casemode.eq.1) then
if(id_proc.eq.0) then
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' MONTE-CARLO on Surrogate '
write(filenum,*) '================================================='
write(filenum,*)
end if
call montecarlo(stat,fct,DIM,dimpc,nterms,npts,ipar,xcof)
call MPI_Barrier(MPI_COMM_WORLD,ierr)
end if
!=======================================================
! Calculate RMSE
!=======================================================
if(casemode.eq.0) then
if(id_proc.eq.0) then
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' RMSE on Surrogate '
write(filenum,*) '================================================='
write(filenum,*)
end if
! call MPI_Barrier(MPI_COMM_WORLD,ierr)
call RMSE_Higher(stat,dim,fct,npts,dimPC,ipar,par,xcof)
call MPI_Barrier(MPI_COMM_WORLD,ierr)
end if
!================================================================
! Tecplot output to file
!================================================================
if(id_proc.eq.0) then
if(dim.le.2) then
write(filenum,*)
write(filenum,*) '================================================='
write(filenum,*) ' Tecplot Output '
write(filenum,*) '================================================='
write(filenum,*)
write(filenum,*)'>> Writing Tecplot output to file . . .'
call tecplot(dim,dimpc,ipar,par,fct,npts,xcof)
end if
end if
nptsold=npts
ntermsold=nterms
enddo !order
enddo ! fct
enddo ! F or FG (Stat)
end do !Oversamp loop
end do !dynamics loop
if (id_proc.eq.0) then
write(filenum,*)
write(filenum,*)'>> Program terminated successfully'
write(filenum,*)
end if
!!$ if (id_proc.eq.0) then
!!$ print *, fmean,fmeanprime(1:dim)
!!$ print *,fvar,fvarprime(1:dim)
!!$ end if
call MPI_Barrier(MPI_COMM_WORLD,ierr)
call stop_all
end program main
!+======================================================================
function PHI(dim,x1,x2,scal)
implicit none
integer :: dim,j
real*8 :: x1(dim),x2(dim),phi,x(dim),xnorm,tmp,scal
x(:)=x1(:)-x2(:)
xnorm=0.0
do j=1,dim
xnorm=xnorm+x(j)**2
end do
xnorm=SQRT(xnorm)/scal
tmp=max(1.0-xnorm,0.0)
phi=xnorm**3
RETURN
end function phi
SUBROUTINE OTHPL(KF,N,X,PL,DPL)
!
! ==========================================================
! Purpose: Compute orthogonal polynomials: Hn(x) or Ln(x),
! or Pn(x), and their derivatives
! Input : KF --- Function code
! KF=1 for Legendre polynomials Pn(x)
! KF=2 for Hermite polynomial Hn(x)
! KF=5 for Laguerre polynomial Ln(x)
! n --- Order of orthogonal polynomials
! x --- Argument of orthogonal polynomials
! Output: PL(n) --- Hn(x) or Ln(x) or Pn(x)
! DPL(n)--- Hn'(x) or Ln'(x) or Pn'(x)
! =========================================================
!
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
DIMENSION PL(0:N),DPL(0:N)
Y0=1.0D0
DY0=0.0D0
PL(0)=1.0D0
DPL(0)=0.0D0
IF (KF.EQ.2) THEN
Y1=X
DY1=1.0D0
PL(1)=X
DPL(1)=1.0D0
ELSE IF (KF.EQ.5) THEN
Y1=1.0D0-X
DY1=-1.0D0
PL(1)=1.0D0-X
DPL(1)=-1.0D0
ELSE IF (KF.EQ.1) THEN
Y1=X
PL(1)=X
DPL(1)=1.0D0
ELSE
PRINT *, 'Invalid choice of orthogonal polynomial'
STOP
ENDIF
DO K=2,N
IF (KF.EQ.2) THEN
A=1.0D0
B=0.0D0
C=(K-1.0D0)
ELSE IF (KF.EQ.5) THEN
A=-1.0D0/K
B=2.0D0+A
C=1.0D0+A
ELSE IF (KF.EQ.1) THEN
A=2.0D0-1.0D0/K
B=0.D0
C=1.0D0-1.0D0/K
ENDIF
YN=(A*X+B)*Y1-C*Y0
PL(K)=YN
IF (KF.GE.2) THEN
DYN=A*Y1+(A*X+B)*DY1-C*DY0
DPL(K)=DYN
DY0=DY1
DY1=DYN
ELSE
IF (DABS(X).EQ.1.0D0) THEN
DPL(K)=0.5D0*X**(K+1)*K*(K+1.0D0)
ELSE
DPL(K)=K*(Y1-X*YN)/(1.0D0-X*X)
ENDIF
ENDIF
Y0=Y1
Y1=YN
END DO
RETURN
END SUBROUTINE OTHPL
subroutine i_to_s(intval,s)
use dimpce, only: OS,fctindx
implicit none
integer idig,intval,ipos,ival,i
character (len=*) s
s(:) = ' '
ival = intval
! Working from right to left, strip off the digits of the integer
! and place them into S(1:len ( s )).
!
ipos = len(s)
do while ( ival /= 0 )
idig = mod( ival, 10 )
ival = ival / 10
s(ipos:ipos) = char(idig + 48 )
ipos = ipos - 1
end do
!
! Fill the empties with zeroes.
!
do i = 1, ipos
s(i:i) = '0'
end do
return
end subroutine i_to_s
!=================================
subroutine getfilename(dim,fct,dimpc,stat,casemode,filename)
use dimpce,only:OS,fctindx,dynamics,lhsdyn
implicit none
character*2 :: dimnumber,fctnumber,ordnumber,OSnumber,fctindxnumber
integer ::lenc,fct,dim,stat,casemode,dimpc
character*60 :: filename
call i_to_s(fct,fctnumber)
call i_to_s(dim,dimnumber)
call i_to_s(dimpc,ordnumber)
call i_to_s(OS,OSnumber)
call i_to_s(fctindx,fctindxnumber)
if (casemode.eq.0) then
filename='norm/errornormdim'
lenc=12+5
else if (Casemode.eq.1) then
filename='stats/statsdim'
lenc=8+6
else if (casemode.eq.55)then !Storing samples
filename='samp/sampdim'
lenc=7+5
else
stop'Wrong flag in case mode'
end if
filename(lenc+1:lenc+2)=dimnumber
lenc=lenc+2
if (casemode.ne.55)then
filename(lenc+1:lenc+3)='fct'
lenc=lenc+3
filename(lenc+1:lenc+2)=fctnumber
lenc=lenc+2
if (fct.eq.20) then
filename(lenc+1:lenc+1)='i'
lenc=lenc+1
filename(lenc+1:lenc+2)=fctindxnumber
lenc=lenc+2
end if
else
filename(lenc+1:lenc+3)='ord'
lenc=lenc+3
filename(lenc+1:lenc+2)=ordnumber
lenc=lenc+2
end if
if (OS.gt.1) then
filename(lenc+1:lenc+3)='Rat'
lenc=lenc+3
filename(lenc+1:lenc+2)=OSnumber
lenc=lenc+2
else
end if
if (stat.eq.2) then
filename(lenc+1:lenc+3)='FGH'
lenc=lenc+3
else if (stat.eq.1) then
filename(lenc+1:lenc+2)='FG'
lenc=lenc+2
else if (stat.eq.0) then
filename(lenc+1:lenc+1)='F'
lenc=lenc+1
else
print *, 'Wrong value in hstat'
stop'Wrong value in stat'
end if
if (dynamics.eq.1) then
filename(lenc+1:lenc+6)='mirdyn'
if (lhsdyn) filename(lenc+1:lenc+6)='lhsdyn'
lenc=lenc+6
end if
return
end subroutine getfilename
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!!$
!!$subroutine evalPC(ndim,dimPC,ipar,xcof,x,yhat)
!!$
!!$ implicit none
!!$ include "collsub.h"
!!$
!!$ integer :: NDIM,DIMPC,nterms,mreg(maxdat,ndim) ! multi-index variables
!!$ integer :: k,j,jj,kk
!!$
!!$ integer :: ipar(MAXVAR)
!!$ double precision :: PL(NDIM,0:DIMPC),DPL(NDIM,0:DIMPC),DDPL(NDIM,0:dimpc)
!!$ double precision :: PL1(NDIM,0:DIMPC),DPL1(NDIM,0:DIMPC),DDPL1(NDIM,0:dimpc)
!!$
!!$ double precision :: x(ndim),yhat
!!$ double precision :: xcof(MAXTRM),xcoftmp
!!$ integer::ipartmp
!!$ real*8:: xtmp,PLtmp(0:dimpc),DPLtmp(0:dimpc),DDPLtmp(0:dimpc)
!!$
!!$ call multidx(MAXDAT,NDIM,DIMPC,mreg,nterms) ! get multiindex notation for tensor procduct
!!$
!!$ !Initialize for safety
!!$
!!$ dpltmp=0.0d0
!!$ ddpltmp=0.0d0
!!$ pltmp=0.0d0
!!$
!!$ yhat = 0.0d0
!!$ do kk=1,nterms
!!$ xcoftmp=xcof(kk)
!!$ do jj=1,nDIM
!!$
!!$ ipartmp=ipar(jj) ! Normal or uniform
!!$
!!$ xtmp=x(jj) !location to evalutuate the orthogonal polynomials
!!$
!!$ call ortho(ipartmp,DIMPC,xtmp,PLtmp,dpltmp,ddpltmp) !get values and derivatives
!!$
!!$ ! Store it in the way it is needed
!!$
!!$ PL(jj,0:dimpc)=pltmp(0:dimpc)
!!$ DPL(jj,0:dimpc)=Dpltmp(0:dimpc)
!!$ DDPL(jj,0:dimpc)=DDpltmp(0:dimpc)
!!$
!!$ xcoftmp=xcoftmp*PL(jj,mreg(kk,jj)) ! the derivatives are not used at all
!!$
!!$ enddo
!!$ yhat = yhat + xcoftmp ! PC value
!!$ end do
!!$
!!$ return
!!$
!!$end subroutine evalPC
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
subroutine ortho(dist,dimpc,x,pl,dpl,ddpl)
implicit none
integer :: dist,dimpc,i
real*8:: x,pl(0:dimpc),dpl(0:dimpc),ddpl(0:dimpc)
if (dist.eq.1) then
do i=0,dimpc
call LEGEN(i,X,pl(i),dpl(i),Ddpl(i))
end do
! call LEGENDRE(dimpc,X,PL,DPL,ddpl) !something wrong in DDPL, DPL
else if (dist.eq.2) then
call hermite(dimpc,X,PL,DPL,ddpl,2)
else
stop'Not implemented yet'
end if
end subroutine ortho
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
subroutine solvertype(stat,os,solver)
implicit none
integer,intent(in)::stat,os
integer,intent(out)::solver
if (stat.eq.0) then
if (OS.eq.1) then
solver=1
else
solver=2
end if
else
solver =2
end if
return
end subroutine solvertype
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
subroutine cfdparams(par)
implicit none
include "collsub.h"
integer,parameter::dim=2
integer::j
real*8,intent(out):: par(DIM,MAXPAR)
double precision :: Initialmach, Finalmach, InitialAOA,FinalAOA
InitialMach=0.5d0
FinalMach=1.5d0
InitialAOA=0.0d0 !degrees
FinalAOA=5.0d0 !degrees
!Domain size
DO j = 1, DIM
if (j.eq.1) then
par(j,1)=InitialAOA*4.0d0*atan(1.0)/180.0 ! in radian
par(j,2)=FinalAOA*4.0d0*atan(1.0)/180.0 ! in radian
else
par(j,1)=InitialMach !initial mach number
par(j,2)=FinalMach !final mach number
end if
END DO
end subroutine cfdparams
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
subroutine pseudoinv(solver,numpc,nterms,xmat,indx,W,V)
implicit none
include "collsub.h"
integer,intent(in)::solver,numpc,nterms
integer,intent(out)::indx(MAXDAT)
real*8,intent(in)::xmat(MAXDAT,MAXDAT)
real*8,intent(out)::W(MAXDAT),V(MAXDAT,MAXDAT)
real*8::dd
if (solver.eq.1) then
call ludcmp(xmat,numpc,MAXDAT,indx,dd)
W=0.0d0
V=0.0d0
else
call svdcmp(xmat,numpc,nterms,MAXDAT,MAXDAT,W,V)
indx=0
end if
end subroutine pseudoinv
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
subroutine backsub(solver,xmat,numpc,nterms,indx,W,V,rhsF,XCOF)
use dimpce,only:filenum
implicit none
include "collsub.h"
INTEGER::I,J,K
integer,intent(in)::solver,numpc,nterms
integer,intent(in)::indx(MAXDAT)
real*8,intent(in)::xmat(MAXDAT,MAXDAT),rhsf(MAXDAT)
real*8,intent(in)::W(MAXDAT),V(MAXDAT,MAXDAT)
real*8,intent(out)::XCOF(MAXDAT)
if (solver.eq.1) then
call lubksb(xmat,nterms,MAXDAT,indx,rhsF) !tmp=rhsfunc
xcof=rhsF
else
call svbksb(xmat,W,V,numpc,nterms,MAXDAT,MAXDAT,rhsF,xcof)
end if
do j=1,nterms
write(filenum,*) j,xcof(j)
enddo
end subroutine backsub
!Unused routines
SUBROUTINE HERM(N,X,Y,DY,D2Y)
! *************************************************************
! * COMPUTES THE VALUE OF THE HERMITE POLYNOMIAL OF DEGREE N
! * AND ITS FIRST AND SECOND DERIVATIVES AT A GIVEN POINT
! * N = DEGREE OF THE POLYNOMIAL
! * X = POINT IN WHICH THE COMPUTATION IS PERFORMED
! * Y = VALUE OF THE POLYNOMIAL IN X
! * DY = VALUE OF THE FIRST DERIVATIVE IN X
! * D2Y= VALUE OF THE SECOND DERIVATIVE IN X
! *************************************************************
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
Y = 1.D0
DY = 0.D0
D2Y = 0.D0
IF (N .EQ. 0) RETURN
Y = 2.D0*X
DY = 2.D0
D2Y = 0.D0
IF (N .EQ. 1) RETURN
YP=1.D0
DO 1 K=2,N
DK = DFLOAT(K-1)
YM = Y
Y = 2.D0*X*Y-2.D0*DK*YP
YPM = YP
YP = YM
1 CONTINUE
DN = 2.D0*DFLOAT(N)
DNN = 2.D0*DFLOAT(N-1)
DY = DN*YP
D2Y = DN*DNN*YPM
RETURN
END SUBROUTINE HERM
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SUBROUTINE LEGEN(N,X,Y,DY,D2Y)
! **************************************************************
! * COMPUTES THE VALUE OF THE LEGENDRE POLYNOMIAL OF DEGREE N
! * AND ITS FIRST AND SECOND DERIVATIVES AT A GIVEN POINT
! * N = DEGREE OF THE POLYNOMIAL
! * X = POINT IN WHICH THE COMPUTATION IS PERFORMED
! * Y = VALUE OF THE POLYNOMIAL IN X
! * DY = VALUE OF THE FIRST DERIVATIVE IN X
! * D2Y= VALUE OF THE SECOND DERIVATIVE IN X
! **************************************************************
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
Y = 1.D0
DY = 0.D0
D2Y = 0.D0
IF (N .EQ. 0) RETURN
Y = X
DY = 1.D0
D2Y = 0.D0
IF(N .EQ. 1) RETURN
YP = 1.D0
DYP = 0.D0
D2YP = 0.D0
DO 1 I=2,N
C1 = DFLOAT(I)
C2 = 2.D0*C1-1.D0
C4 = C1-1.D0
YM = Y
Y = (C2*X*Y-C4*YP)/C1
YP = YM
DYM = DY
DY = (C2*X*DY-C4*DYP+C2*YP)/C1
DYP = DYM
D2YM = D2Y
D2Y = (C2*X*D2Y-C4*D2YP+2.D0*C2*DYP)/C1
D2YP = D2YM
1 CONTINUE
RETURN