Actual source code: ex36A.c
2: static char help[] = "Transistor amplifier (autonomous).\n";
4: /*F
5: M y'=f(y)
7: Useful options: -ts_monitor_lg_solution -ts_monitor_lg_timestep -lg_indicate_data_points 0
8: F*/
10: /*
11: Include "petscts.h" so that we can use TS solvers. Note that this
12: file automatically includes:
13: petscsys.h - base PETSc routines petscvec.h - vectors
14: petscmat.h - matrices
15: petscis.h - index sets petscksp.h - Krylov subspace methods
16: petscviewer.h - viewers petscpc.h - preconditioners
17: petscksp.h - linear solvers
18: */
19: #include <petscts.h>
21: FILE *gfilepointer_data, *gfilepointer_info;
23: /* Defines the source */
24: PetscErrorCode Ue(PetscScalar t, PetscScalar *U)
25: {
27: U = 0.4 * sin(200 * pi * t);
28: return 0;
29: }
30: * /
32: /*
33: Defines the DAE passed to the time solver
34: */
35: static PetscErrorCode IFunctionImplicit(TS ts, PetscReal t, Vec Y, Vec Ydot, Vec F, void *ctx)
36: {
37: const PetscScalar *y, *ydot;
38: PetscScalar *f;
41: /* The next three lines allow us to access the entries of the vectors directly */
42: VecGetArrayRead(Y, &y);
43: VecGetArrayRead(Ydot, &ydot);
44: VecGetArray(F, &f);
46: f[0] = PetscSinReal(200 * PETSC_PI * y[5]) / 2500. - y[0] / 1000. - ydot[0] / 1.e6 + ydot[1] / 1.e6;
47: f[1] = 0.0006666766666666667 - PetscExpReal((500 * (y[1] - y[2])) / 13.) / 1.e8 - y[1] / 4500. + ydot[0] / 1.e6 - ydot[1] / 1.e6;
48: f[2] = -1.e-6 + PetscExpReal((500 * (y[1] - y[2])) / 13.) / 1.e6 - y[2] / 9000. - ydot[2] / 500000.;
49: f[3] = 0.0006676566666666666 - (99 * PetscExpReal((500 * (y[1] - y[2])) / 13.)) / 1.e8 - y[3] / 9000. - (3 * ydot[3]) / 1.e6 + (3 * ydot[4]) / 1.e6;
50: f[4] = -y[4] / 9000. + (3 * ydot[3]) / 1.e6 - (3 * ydot[4]) / 1.e6;
51: f[5] = -1 + ydot[5];
53: VecRestoreArrayRead(Y, &y);
54: VecRestoreArrayRead(Ydot, &ydot);
55: VecRestoreArray(F, &f);
56: return 0;
57: }
59: /*
60: Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian.
61: */
62: static PetscErrorCode IJacobianImplicit(TS ts, PetscReal t, Vec Y, Vec Ydot, PetscReal a, Mat A, Mat B, void *ctx)
63: {
64: PetscInt rowcol[] = {0, 1, 2, 3, 4, 5};
65: const PetscScalar *y, *ydot;
66: PetscScalar J[6][6];
69: VecGetArrayRead(Y, &y);
70: VecGetArrayRead(Ydot, &ydot);
72: PetscMemzero(J, sizeof(J));
74: J[0][0] = -0.001 - a / 1.e6;
75: J[0][1] = a / 1.e6;
76: J[0][5] = (2 * PETSC_PI * PetscCosReal(200 * PETSC_PI * y[5])) / 25.;
77: J[1][0] = a / 1.e6;
78: J[1][1] = -0.00022222222222222223 - a / 1.e6 - PetscExpReal((500 * (y[1] - y[2])) / 13.) / 2.6e6;
79: J[1][2] = PetscExpReal((500 * (y[1] - y[2])) / 13.) / 2.6e6;
80: J[2][1] = PetscExpReal((500 * (y[1] - y[2])) / 13.) / 26000.;
81: J[2][2] = -0.00011111111111111112 - a / 500000. - PetscExpReal((500 * (y[1] - y[2])) / 13.) / 26000.;
82: J[3][1] = (-99 * PetscExpReal((500 * (y[1] - y[2])) / 13.)) / 2.6e6;
83: J[3][2] = (99 * PetscExpReal((500 * (y[1] - y[2])) / 13.)) / 2.6e6;
84: J[3][3] = -0.00011111111111111112 - (3 * a) / 1.e6;
85: J[3][4] = (3 * a) / 1.e6;
86: J[4][3] = (3 * a) / 1.e6;
87: J[4][4] = -0.00011111111111111112 - (3 * a) / 1.e6;
88: J[5][5] = a;
90: MatSetValues(B, 6, rowcol, 6, rowcol, &J[0][0], INSERT_VALUES);
92: VecRestoreArrayRead(Y, &y);
93: VecRestoreArrayRead(Ydot, &ydot);
95: MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
96: MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
97: if (A != B) {
98: MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
99: MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
100: }
101: return 0;
102: }
104: int main(int argc, char **argv)
105: {
106: TS ts; /* ODE integrator */
107: Vec Y; /* solution will be stored here */
108: Mat A; /* Jacobian matrix */
109: PetscMPIInt size;
110: PetscInt n = 6;
111: PetscScalar *y;
113: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114: Initialize program
115: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
117: PetscInitialize(&argc, &argv, (char *)0, help);
118: MPI_Comm_size(PETSC_COMM_WORLD, &size);
121: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122: Create necessary matrix and vectors
123: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
124: MatCreate(PETSC_COMM_WORLD, &A);
125: MatSetSizes(A, n, n, PETSC_DETERMINE, PETSC_DETERMINE);
126: MatSetFromOptions(A);
127: MatSetUp(A);
129: MatCreateVecs(A, &Y, NULL);
131: VecGetArray(Y, &y);
132: y[0] = 0.0;
133: y[1] = 3.0;
134: y[2] = y[1];
135: y[3] = 6.0;
136: y[4] = 0.0;
137: y[5] = 0.0;
138: VecRestoreArray(Y, &y);
140: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141: Create timestepping solver context
142: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
143: TSCreate(PETSC_COMM_WORLD, &ts);
144: TSSetProblemType(ts, TS_NONLINEAR);
145: TSSetType(ts, TSARKIMEX);
146: TSSetEquationType(ts, TS_EQ_DAE_IMPLICIT_INDEX1);
147: TSARKIMEXSetFullyImplicit(ts, PETSC_TRUE);
148: /*TSSetType(ts,TSROSW);*/
149: TSSetIFunction(ts, NULL, IFunctionImplicit, NULL);
150: TSSetIJacobian(ts, A, A, IJacobianImplicit, NULL);
152: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153: Set initial conditions
154: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155: TSSetSolution(ts, Y);
157: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158: Set solver options
159: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
160: TSSetMaxTime(ts, 0.15);
161: TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER);
162: TSSetTimeStep(ts, .001);
163: TSSetFromOptions(ts);
165: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
166: Do Time stepping
167: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
168: TSSolve(ts, Y);
170: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171: Free work space. All PETSc objects should be destroyed when they are no longer needed.
172: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
173: MatDestroy(&A);
174: VecDestroy(&Y);
175: TSDestroy(&ts);
176: PetscFinalize();
177: return 0;
178: }